Skip to content
Closed
Changes from 1 commit
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
30 changes: 27 additions & 3 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,8 @@ PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
char c, *s;
zend_long cutoff;
int cutlim;
int negative = 0;
int invalid_chars = 0;

if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
return FAILURE;
Expand All @@ -877,8 +879,12 @@ PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
c -= 'A' - 10;
else if (c >= 'a' && c <= 'z')
c -= 'a' - 10;
else
else if (c == '-')
negative = !negative;
else {
invalid_chars++;
continue;
}

if (c >= base)
continue;
Expand All @@ -898,6 +904,15 @@ PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
}
}

if (invalid_chars > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

You'll probably need some extra handling to make sure that 0xDEADBEEF, 0b10101001 and 0o640 do not generate a warning.

php_error_docref(NULL, E_WARNING, "Invalid characters passed, these have been ignored");
}

if (negative == 1) {
fnum = -fnum;
num = -num;
}

if (mode == 1) {
ZVAL_DOUBLE(ret, fnum);
} else {
Expand All @@ -917,22 +932,31 @@ PHPAPI zend_string * _php_math_longtobase(zval *arg, int base)
static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char buf[(sizeof(zend_ulong) << 3) + 1];
char *ptr, *end;
zend_ulong value;
zend_long value;
int negative = 0;

if (Z_TYPE_P(arg) != IS_LONG || base < 2 || base > 36) {
return ZSTR_EMPTY_ALLOC();
}

value = Z_LVAL_P(arg);
if (value < 0) {
negative = 1;
value = -value;
}

end = ptr = buf + sizeof(buf) - 1;
end = ptr = buf + sizeof(buf) - 1 + negative;
*ptr = '\0';

do {
*--ptr = digits[value % base];
value /= base;
} while (ptr > buf && value);

if (negative) {
*--ptr = '-';
}

return zend_string_init(ptr, end - ptr, 0);
}
/* }}} */
Expand Down