Skip to content

ext/bcmath: use memcmp for compare #18871

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions ext/bcmath/libbcmath/src/compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,24 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool us
const char *n1ptr = n1->n_value;
const char *n2ptr = n2->n_value;

while ((count > 0) && (*n1ptr == *n2ptr)) {
n1ptr++;
n2ptr++;
count--;
}

if (count != 0) {
if (*n1ptr > *n2ptr) {
/* Magnitude of n1 > n2. */
if (!use_sign || n1->n_sign == PLUS) {
return BCMATH_LEFT_GREATER;
} else {
return BCMATH_RIGHT_GREATER;
}
int cmp_ret = memcmp(n1ptr, n2ptr, count);
if (cmp_ret > 0) {
/* Magnitude of n1 > n2. */
if (!use_sign || n1->n_sign == PLUS) {
return BCMATH_LEFT_GREATER;
} else {
/* Magnitude of n1 < n2. */
if (!use_sign || n1->n_sign == PLUS) {
return BCMATH_RIGHT_GREATER;
} else {
return BCMATH_LEFT_GREATER;
}
return BCMATH_RIGHT_GREATER;
}
} else if (cmp_ret < 0) {
/* Magnitude of n1 < n2. */
if (!use_sign || n1->n_sign == PLUS) {
return BCMATH_RIGHT_GREATER;
} else {
return BCMATH_LEFT_GREATER;
}
}
n1ptr += count;
n2ptr += count;

/* They are equal up to the last part of the equal part of the fraction. */
if (n1_scale != n2_scale) {
Expand Down