mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Fix toString crash for large numbers
This commit is contained in:
parent
a6b1c4a4d4
commit
0900e6072c
@ -1,4 +1,4 @@
|
||||
1v61 : ...
|
||||
1v61 : Fix toString crash for large numbers
|
||||
|
||||
1v60 : Fix unary plug on variable not working (fix #268)
|
||||
Added DNS with eth.setIP() for W5500
|
||||
|
||||
@ -334,22 +334,27 @@ char itoch(int val) {
|
||||
#ifndef HAS_STDLIB
|
||||
void itoa(JsVarInt vals,char *str,unsigned int base) {
|
||||
JsVarIntUnsigned val;
|
||||
// handle negative numbers
|
||||
if (vals<0) {
|
||||
*(str++)='-';
|
||||
val = (JsVarIntUnsigned)(-vals);
|
||||
} else {
|
||||
val = (JsVarIntUnsigned)vals;
|
||||
}
|
||||
JsVarIntUnsigned d = 1;
|
||||
while (d*base <= val) d*=base;
|
||||
while (d > 1) {
|
||||
unsigned int v = (unsigned int)(val / d);
|
||||
val -= v*d;
|
||||
*(str++) = itoch((int)v);
|
||||
d /= base;
|
||||
// work out how many digits
|
||||
JsVarIntUnsigned tmp = val;
|
||||
int digits = 1;
|
||||
while (tmp>=base) {
|
||||
digits++;
|
||||
tmp /= base;
|
||||
}
|
||||
*(str++)=itoch((int)val);
|
||||
*(str++)=0;
|
||||
// for each digit...
|
||||
int i;
|
||||
for (i=digits-1;i>=0;i--) {
|
||||
str[i] = itoch((int)(val % base));
|
||||
val /= base;
|
||||
}
|
||||
str[digits] = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@ -114,7 +114,7 @@ typedef long long JsSysTime;
|
||||
#define JS_ERROR_BUF_SIZE 64 // size of buffer error messages are written into
|
||||
#define JS_ERROR_TOKEN_BUF_SIZE 16 // see jslTokenAsString
|
||||
|
||||
#define JS_NUMBER_BUFFER_SIZE 24
|
||||
#define JS_NUMBER_BUFFER_SIZE 66 // 64 bit base 2 + minus + terminating 0
|
||||
|
||||
#define JSPARSE_MAX_SCOPES 8
|
||||
// Don't restrict number of iterations now
|
||||
|
||||
5
tests/test_tostring2.js
Normal file
5
tests/test_tostring2.js
Normal file
@ -0,0 +1,5 @@
|
||||
// large hex values were causing crashes
|
||||
|
||||
0x8000000000000000.toString(2) // could segfault :)
|
||||
|
||||
result = (8446744073709551616).toString(16) == "7538dcfb76180000"
|
||||
Loading…
x
Reference in New Issue
Block a user