Improve error handling and reporting in unpacking functions (#707)

This commit is contained in:
Inada Naoki 2026-06-24 17:14:55 +09:00 committed by GitHub
parent 33fec11eb4
commit 951995ecca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 4 additions and 32 deletions

View file

@ -46,7 +46,7 @@ cdef extern from "unpack.h":
Py_ssize_t count
ctypedef int (*execute_fn)(unpack_context* ctx, const char* data,
Py_ssize_t len, Py_ssize_t* off) except? -1
Py_ssize_t len, Py_ssize_t* off) except -1
execute_fn unpack_construct
execute_fn unpack_skip
execute_fn read_array_header
@ -206,8 +206,6 @@ def unpackb(object packed, *, object object_hook=None, object list_hook=None,
raise FormatError
elif ret == -3:
raise StackError
elif PyErr_Occurred():
raise
else:
raise ValueError("Unpack failed: error = %d" % (ret,))
@ -502,8 +500,6 @@ cdef class Unpacker:
raise FormatError
elif ret == -3:
raise StackError
elif PyErr_Occurred():
raise
else:
raise ValueError("Unpack failed: error = %d" % (ret,))
finally:

View file

@ -40,11 +40,6 @@ struct unpack_context;
typedef struct unpack_context unpack_context;
typedef int (*execute_fn)(unpack_context *ctx, const char* data, Py_ssize_t len, Py_ssize_t* off);
static inline msgpack_unpack_object unpack_callback_root(unpack_user* u)
{
return NULL;
}
static inline int unpack_callback_uint16(unpack_user* u, uint16_t d, msgpack_unpack_object* o)
{
PyObject *p = PyLong_FromLong((long)d);

View file

@ -35,11 +35,6 @@ struct unpack_context {
unsigned int cs;
unsigned int trail;
unsigned int top;
/*
unpack_stack* stack;
unsigned int stack_size;
unpack_stack embed_stack[MSGPACK_EMBED_STACK_SIZE];
*/
unpack_stack stack[MSGPACK_EMBED_STACK_SIZE];
};
@ -49,22 +44,9 @@ static inline void unpack_init(unpack_context* ctx)
ctx->cs = CS_HEADER;
ctx->trail = 0;
ctx->top = 0;
/*
ctx->stack = ctx->embed_stack;
ctx->stack_size = MSGPACK_EMBED_STACK_SIZE;
*/
ctx->stack[0].obj = unpack_callback_root(&ctx->user);
ctx->stack[0].obj = NULL;
}
/*
static inline void unpack_destroy(unpack_context* ctx)
{
if(ctx->stack_size != MSGPACK_EMBED_STACK_SIZE) {
free(ctx->stack);
}
}
*/
static inline PyObject* unpack_data(unpack_context* ctx)
{
return (ctx)->stack[0].obj;
@ -94,9 +76,6 @@ static inline int unpack_execute(bool construct, unpack_context* ctx, const char
unsigned int cs = ctx->cs;
unsigned int top = ctx->top;
unpack_stack* stack = ctx->stack;
/*
unsigned int stack_size = ctx->stack_size;
*/
unpack_user* user = &ctx->user;
PyObject* obj = NULL;
@ -319,6 +298,7 @@ static inline int unpack_execute(bool construct, unpack_context* ctx, const char
start_container(_map, _msgpack_load32(uint32_t,n), CT_MAP_KEY);
default:
PyErr_Format(PyExc_RuntimeError, "Invalid state: %d", cs);
goto _failed;
}
}
@ -355,6 +335,7 @@ _push:
goto _header_again;
default:
PyErr_Format(PyExc_RuntimeError, "Invalid container type: %u", c->ct);
goto _failed;
}