Skip to content

Commit 951995e

Browse files
authored
Improve error handling and reporting in unpacking functions (#707)
1 parent 33fec11 commit 951995e

3 files changed

Lines changed: 4 additions & 32 deletions

File tree

msgpack/_unpacker.pyx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ cdef extern from "unpack.h":
4646
Py_ssize_t count
4747

4848
ctypedef int (*execute_fn)(unpack_context* ctx, const char* data,
49-
Py_ssize_t len, Py_ssize_t* off) except? -1
49+
Py_ssize_t len, Py_ssize_t* off) except -1
5050
execute_fn unpack_construct
5151
execute_fn unpack_skip
5252
execute_fn read_array_header
@@ -206,8 +206,6 @@ def unpackb(object packed, *, object object_hook=None, object list_hook=None,
206206
raise FormatError
207207
elif ret == -3:
208208
raise StackError
209-
elif PyErr_Occurred():
210-
raise
211209
else:
212210
raise ValueError("Unpack failed: error = %d" % (ret,))
213211

@@ -502,8 +500,6 @@ cdef class Unpacker:
502500
raise FormatError
503501
elif ret == -3:
504502
raise StackError
505-
elif PyErr_Occurred():
506-
raise
507503
else:
508504
raise ValueError("Unpack failed: error = %d" % (ret,))
509505
finally:

msgpack/unpack.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ struct unpack_context;
4040
typedef struct unpack_context unpack_context;
4141
typedef int (*execute_fn)(unpack_context *ctx, const char* data, Py_ssize_t len, Py_ssize_t* off);
4242

43-
static inline msgpack_unpack_object unpack_callback_root(unpack_user* u)
44-
{
45-
return NULL;
46-
}
47-
4843
static inline int unpack_callback_uint16(unpack_user* u, uint16_t d, msgpack_unpack_object* o)
4944
{
5045
PyObject *p = PyLong_FromLong((long)d);

msgpack/unpack_template.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ struct unpack_context {
3535
unsigned int cs;
3636
unsigned int trail;
3737
unsigned int top;
38-
/*
39-
unpack_stack* stack;
40-
unsigned int stack_size;
41-
unpack_stack embed_stack[MSGPACK_EMBED_STACK_SIZE];
42-
*/
4338
unpack_stack stack[MSGPACK_EMBED_STACK_SIZE];
4439
};
4540

@@ -49,22 +44,9 @@ static inline void unpack_init(unpack_context* ctx)
4944
ctx->cs = CS_HEADER;
5045
ctx->trail = 0;
5146
ctx->top = 0;
52-
/*
53-
ctx->stack = ctx->embed_stack;
54-
ctx->stack_size = MSGPACK_EMBED_STACK_SIZE;
55-
*/
56-
ctx->stack[0].obj = unpack_callback_root(&ctx->user);
47+
ctx->stack[0].obj = NULL;
5748
}
5849

59-
/*
60-
static inline void unpack_destroy(unpack_context* ctx)
61-
{
62-
if(ctx->stack_size != MSGPACK_EMBED_STACK_SIZE) {
63-
free(ctx->stack);
64-
}
65-
}
66-
*/
67-
6850
static inline PyObject* unpack_data(unpack_context* ctx)
6951
{
7052
return (ctx)->stack[0].obj;
@@ -94,9 +76,6 @@ static inline int unpack_execute(bool construct, unpack_context* ctx, const char
9476
unsigned int cs = ctx->cs;
9577
unsigned int top = ctx->top;
9678
unpack_stack* stack = ctx->stack;
97-
/*
98-
unsigned int stack_size = ctx->stack_size;
99-
*/
10079
unpack_user* user = &ctx->user;
10180

10281
PyObject* obj = NULL;
@@ -319,6 +298,7 @@ static inline int unpack_execute(bool construct, unpack_context* ctx, const char
319298
start_container(_map, _msgpack_load32(uint32_t,n), CT_MAP_KEY);
320299

321300
default:
301+
PyErr_Format(PyExc_RuntimeError, "Invalid state: %d", cs);
322302
goto _failed;
323303
}
324304
}
@@ -355,6 +335,7 @@ static inline int unpack_execute(bool construct, unpack_context* ctx, const char
355335
goto _header_again;
356336

357337
default:
338+
PyErr_Format(PyExc_RuntimeError, "Invalid container type: %u", c->ct);
358339
goto _failed;
359340
}
360341

0 commit comments

Comments
 (0)