-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ext/bcmath: Prevent overflow of uint32_t/uint64_t #14297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
b57b6ee
to
c283029
Compare
If add more than a certain number of times, it will overflow, so need to adjust the digits before adding.
c283029
to
0a1de07
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice approach and good catch, thanks!
ext/bcmath/libbcmath/src/recmul.c
Outdated
#include "private.h" /* For _bc_rm_leading_zeros() */ | ||
#include "zend_alloc.h" | ||
|
||
|
||
#if SIZEOF_SIZE_T >= 8 | ||
# define BC_MUL_UINT_DIGITS 8 | ||
# define BC_MUL_UINT_OVERFLOW 100000000 | ||
# define BC_MUL_UINT_OVERFLOW (BC_UINT_T) 100000000 | ||
# define BC_MUL_MAX_ADD_COUNT (ULONG_MAX / (BC_MUL_UINT_OVERFLOW * BC_MUL_UINT_OVERFLOW)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the size of a long is not necessarily the size of size_t.
I think I prefer a single definition:
#define BC_MUL_MAX_ADD_COUNT (~((BC_UINT_T) 0) / (BC_MUL_UINT_OVERFLOW * BC_MUL_UINT_OVERFLOW))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's cool! thx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems right to me, thanks Saki!
I'll merge this after CI passes |
If add more than a certain number of times, it will overflow, so need to adjust the digits before adding.