fix: check return code in unpack_callback_int64#665
fix: check return code in unpack_callback_int64#665KowalskiThomas wants to merge 1 commit intomsgpack:mainfrom
unpack_callback_int64#665Conversation
| if (d > LONG_MAX) { | ||
| p = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)d); | ||
| } else { | ||
| p = PyLong_FromLong((long)d); |
There was a problem hiding this comment.
Should this be PyLong_FromUnsignedLong((unsigned long)d)?
There was a problem hiding this comment.
Both are correct. Since we checked d <= LONG_MAX, (long)d is guaranteed to fit in a signed long.
But p = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)d) without d > LONG_MAX check would be better.
There was a problem hiding this comment.
OK, agreed.
But if both are correct, one could use the less eyebrows-raising "unsigned" call and cast?
| static inline int unpack_callback_int64(unpack_user* u, int64_t d, msgpack_unpack_object* o) | ||
| { | ||
| PyObject *p; | ||
| if (d > LONG_MAX || d < LONG_MIN) { |
There was a problem hiding this comment.
That "|| d < LONG_MIN" looks strange, is it correct?
There was a problem hiding this comment.
I do believe it's correct -- long could be less than 64 bits in which case you want to make sure either the number is positive and less than the max a long can fit, or it's negative and it's more than the smallest negative number a long can fit.
Am I missing something or does that clear things up?
This simply adds a null pointer check after calling
PyLong_FromLongLong/PyLong_FromLonglike other similar functions do it (example here).