Skip to content

fix: check return code in unpack_callback_int64#665

Merged
methane merged 4 commits intomsgpack:mainfrom
KowalskiThomas:kowalski/fix-check-return-code-in-unpack_callback_int64
Apr 21, 2026
Merged

fix: check return code in unpack_callback_int64#665
methane merged 4 commits intomsgpack:mainfrom
KowalskiThomas:kowalski/fix-check-return-code-in-unpack_callback_int64

Conversation

@KowalskiThomas
Copy link
Copy Markdown
Contributor

This simply adds a null pointer check after calling PyLong_FromLongLong / PyLong_FromLong like other similar functions do it (example here).

@KowalskiThomas KowalskiThomas marked this pull request as ready for review April 19, 2026 12:45
Comment thread msgpack/unpack.h Outdated
Copy link
Copy Markdown
Contributor

@ThomasWaldmann ThomasWaldmann Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be PyLong_FromUnsignedLong((unsigned long)d)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

@ThomasWaldmann ThomasWaldmann Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, agreed.

But if both are correct, one could use the less eyebrows-raising "unsigned" call and cast?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to change this in the PR?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@methane decides.

It is somehow unrelated to the PR topic, I just stumbled over it while looking at the code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if both are correct, one could use the less eyebrows-raising "unsigned" call and cast?

There is no PyLong_FromUnsignedLong(). PyLong_FromLong requires (unsigned long) cast to suppress implicit cast warning.

Do you want me to change this in the PR?

Please remove if (d > LONG_MAX) { and use PyLong_FromUnsignedLongLong always.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no PyLong_FromUnsignedLong()

There is a call to that in line 333.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. But using only PyLong_FromUnsignedLongLong is much simpler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the call and now it's always PyLong_FromUnsignedLongLong!

Comment thread msgpack/unpack.h Outdated
@KowalskiThomas KowalskiThomas force-pushed the kowalski/fix-check-return-code-in-unpack_callback_int64 branch from 4b0097e to 5142b3d Compare April 20, 2026 18:07
Comment thread msgpack/unpack.h
} else {
p = PyLong_FromLong((long)d);
}
PyObject *p = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)d);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, but it seems correct.

(I am not really familiar with the C api.)

Comment thread msgpack/unpack.h Outdated
Comment on lines 102 to 106
@@ -109,6 +104,8 @@ static inline int unpack_callback_int64(unpack_user* u, int64_t d, msgpack_unpac
} else {
p = PyLong_FromLong((long)d);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if that other change is correct, then this one can also be simplified in the same way, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

methane pushed a commit that referenced this pull request Apr 21, 2026
Similar to #665, just a return value check to propagate the error in
case one happens (instead of silently ignoring it).

Note that as opposed to the previous lines, we don't need to
`PyErr_SetString` since `unpack_callback_uint32` calls
`PyLong_FromSize_t` which itself should set whatever Python error is
relevant; we just need to make it clear to the caller that an error
occurred.
Comment thread msgpack/unpack.h Outdated
methane added 2 commits April 21, 2026 18:08
Refactor unpack_callback_int64 to simplify PyObject creation.
@methane methane merged commit 95c8be5 into msgpack:main Apr 21, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants