Skip to content

Commit f24201f

Browse files
authored
Merge pull request #548 from redboltz/fix_c_error_code
Fixed C unpack return code.
2 parents 2674e34 + 9a113bb commit f24201f

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

include/msgpack/unpack_template.h

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,17 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
109109
int ret;
110110

111111
#define push_simple_value(func) \
112-
if(msgpack_unpack_callback(func)(user, &obj) < 0) { goto _failed; } \
112+
ret = msgpack_unpack_callback(func)(user, &obj); \
113+
if(ret < 0) { goto _failed; } \
113114
goto _push
114115
#define push_fixed_value(func, arg) \
115-
if(msgpack_unpack_callback(func)(user, arg, &obj) < 0) { goto _failed; } \
116+
ret = msgpack_unpack_callback(func)(user, arg, &obj); \
117+
if(ret < 0) { goto _failed; } \
116118
goto _push
117119
#define push_variable_value(func, base, pos, len) \
118-
if(msgpack_unpack_callback(func)(user, \
119-
(const char*)base, (const char*)pos, len, &obj) < 0) { goto _failed; } \
120+
ret = msgpack_unpack_callback(func)(user, \
121+
(const char*)base, (const char*)pos, len, &obj); \
122+
if(ret < 0) { goto _failed; } \
120123
goto _push
121124

122125
#define again_fixed_trail(_cs, trail_len) \
@@ -130,33 +133,16 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
130133
goto _fixed_trail_again
131134

132135
#define start_container(func, count_, ct_) \
133-
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ \
134-
if(msgpack_unpack_callback(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \
136+
if(top >= MSGPACK_EMBED_STACK_SIZE) { \
137+
ret = MSGPACK_UNPACK_NOMEM_ERROR; \
138+
goto _failed; \
139+
} /* FIXME */ \
140+
ret = msgpack_unpack_callback(func)(user, count_, &stack[top].obj); \
141+
if(ret < 0) { goto _failed; } \
135142
if((count_) == 0) { obj = stack[top].obj; goto _push; } \
136143
stack[top].ct = ct_; \
137144
stack[top].count = count_; \
138145
++top; \
139-
/*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \
140-
/*printf("stack push %d\n", top);*/ \
141-
/* FIXME \
142-
if(top >= stack_size) { \
143-
if(stack_size == MSGPACK_EMBED_STACK_SIZE) { \
144-
size_t csize = sizeof(msgpack_unpack_struct(_stack)) * MSGPACK_EMBED_STACK_SIZE; \
145-
size_t nsize = csize * 2; \
146-
msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)malloc(nsize); \
147-
if(tmp == NULL) { goto _failed; } \
148-
memcpy(tmp, ctx->stack, csize); \
149-
ctx->stack = stack = tmp; \
150-
ctx->stack_size = stack_size = MSGPACK_EMBED_STACK_SIZE * 2; \
151-
} else { \
152-
size_t nsize = sizeof(msgpack_unpack_struct(_stack)) * ctx->stack_size * 2; \
153-
msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)realloc(ctx->stack, nsize); \
154-
if(tmp == NULL) { goto _failed; } \
155-
ctx->stack = stack = tmp; \
156-
ctx->stack_size = stack_size = stack_size * 2; \
157-
} \
158-
} \
159-
*/ \
160146
goto _header_again
161147

162148
#define NEXT_CS(p) \
@@ -231,6 +217,7 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
231217
case 0xdf: // map 32
232218
again_fixed_trail(NEXT_CS(p), 2u << (((unsigned int)*p) & 0x01));
233219
default:
220+
ret = MSGPACK_UNPACK_PARSE_ERROR;
234221
goto _failed;
235222
}
236223
SWITCH_RANGE(0xa0, 0xbf) // FixStr
@@ -241,6 +228,7 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
241228
start_container(_map, ((unsigned int)*p) & 0x0f, MSGPACK_CT_MAP_KEY);
242229

243230
SWITCH_RANGE_DEFAULT
231+
ret = MSGPACK_UNPACK_PARSE_ERROR;
244232
goto _failed;
245233
SWITCH_RANGE_END
246234
// end MSGPACK_CS_HEADER
@@ -384,6 +372,7 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
384372
}
385373

386374
default:
375+
ret = MSGPACK_UNPACK_PARSE_ERROR;
387376
goto _failed;
388377
}
389378
}
@@ -393,7 +382,8 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
393382
c = &stack[top-1];
394383
switch(c->ct) {
395384
case MSGPACK_CT_ARRAY_ITEM:
396-
if(msgpack_unpack_callback(_array_item)(user, &c->obj, obj) < 0) { goto _failed; }
385+
ret = msgpack_unpack_callback(_array_item)(user, &c->obj, obj); \
386+
if(ret < 0) { goto _failed; }
397387
if(--c->count == 0) {
398388
obj = c->obj;
399389
--top;
@@ -406,7 +396,8 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
406396
c->ct = MSGPACK_CT_MAP_VALUE;
407397
goto _header_again;
408398
case MSGPACK_CT_MAP_VALUE:
409-
if(msgpack_unpack_callback(_map_item)(user, &c->obj, c->map_key, obj) < 0) { goto _failed; }
399+
ret = msgpack_unpack_callback(_map_item)(user, &c->obj, c->map_key, obj); \
400+
if(ret < 0) { goto _failed; }
410401
if(--c->count == 0) {
411402
obj = c->obj;
412403
--top;
@@ -417,6 +408,7 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
417408
goto _header_again;
418409

419410
default:
411+
ret = MSGPACK_UNPACK_PARSE_ERROR;
420412
goto _failed;
421413
}
422414

@@ -436,7 +428,6 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
436428

437429
_failed:
438430
/*printf("** FAILED **\n"); */
439-
ret = -1;
440431
goto _end;
441432

442433
_out:

src/unpack.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ static inline int template_callback_array(unpack_user* u, unsigned int n, msgpac
195195
size = n*sizeof(msgpack_object);
196196
if (size / sizeof(msgpack_object) != n) {
197197
// integer overflow
198-
return -1;
198+
return MSGPACK_UNPACK_NOMEM_ERROR;
199199
}
200200
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
201-
if(o->via.array.ptr == NULL) { return -1; }
201+
if(o->via.array.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; }
202202
return 0;
203203
}
204204

@@ -222,10 +222,10 @@ static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_
222222
size = n*sizeof(msgpack_object_kv);
223223
if (size / sizeof(msgpack_object_kv) != n) {
224224
// integer overflow
225-
return -1;
225+
return MSGPACK_UNPACK_NOMEM_ERROR;
226226
}
227227
o->via.map.ptr = (msgpack_object_kv*)msgpack_zone_malloc(u->z, size);
228-
if(o->via.map.ptr == NULL) { return -1; }
228+
if(o->via.map.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; }
229229
return 0;
230230
}
231231

@@ -537,7 +537,7 @@ static inline msgpack_unpack_return unpacker_next(msgpack_unpacker* mpac,
537537
if(ret < 0) {
538538
result->zone = NULL;
539539
memset(&result->data, 0, sizeof(msgpack_object));
540-
return MSGPACK_UNPACK_PARSE_ERROR;
540+
return ret;
541541
}
542542

543543
if(ret == 0) {
@@ -601,7 +601,7 @@ msgpack_unpack(const char* data, size_t len, size_t* off,
601601

602602
e = template_execute(&ctx, data, len, &noff);
603603
if(e < 0) {
604-
return MSGPACK_UNPACK_PARSE_ERROR;
604+
return e;
605605
}
606606

607607
if(off != NULL) { *off = noff; }
@@ -652,7 +652,7 @@ msgpack_unpack_next(msgpack_unpacked* result,
652652
if(e < 0) {
653653
msgpack_zone_free(result->zone);
654654
result->zone = NULL;
655-
return MSGPACK_UNPACK_PARSE_ERROR;
655+
return e;
656656
}
657657

658658

0 commit comments

Comments
 (0)