Skip to content
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

Bugfix for various strtol/strtoll issues #1209

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
29 changes: 23 additions & 6 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -3918,7 +3918,14 @@ LibraryManager.library = {
str++;
}
}
}
} else if (finalBase==16) {
if ({{{ makeGetValue('str', 0, 'i8') }}} == {{{ charCode('0') }}}) {
if ({{{ makeGetValue('str+1', 0, 'i8') }}} == {{{ charCode('x') }}} ||
{{{ makeGetValue('str+1', 0, 'i8') }}} == {{{ charCode('X') }}}) {
str += 2;
}
}
}
if (!finalBase) finalBase = 10;

// Get digits.
Expand Down Expand Up @@ -3969,13 +3976,14 @@ LibraryManager.library = {
#if USE_TYPED_ARRAYS == 2
_parseInt64__deps: ['isspace', '__setErrNo', '$ERRNO_CODES', function() { Types.preciseI64MathUsed = 1 }],
_parseInt64: function(str, endptr, base, min, max, unsign) {
var start = str;
var isNegative = false;
// Skip space.
while (_isspace({{{ makeGetValue('str', 0, 'i8') }}})) str++;

// Check for a plus/minus sign.
if ({{{ makeGetValue('str', 0, 'i8') }}} == {{{ charCode('-') }}}) {
str++;
isNegative = true;
} else if ({{{ makeGetValue('str', 0, 'i8') }}} == {{{ charCode('+') }}}) {
str++;
}
Expand All @@ -3991,12 +3999,19 @@ LibraryManager.library = {
str += 2;
} else {
finalBase = 8;
str++;
ok = true; // we saw an initial zero, perhaps the entire thing is just "0"
}
}
}
} else if (finalBase==16) {
if ({{{ makeGetValue('str', 0, 'i8') }}} == {{{ charCode('0') }}}) {
if ({{{ makeGetValue('str+1', 0, 'i8') }}} == {{{ charCode('x') }}} ||
{{{ makeGetValue('str+1', 0, 'i8') }}} == {{{ charCode('X') }}}) {
str += 2;
}
}
}
if (!finalBase) finalBase = 10;
start = str;

// Get digits.
var chr;
Expand All @@ -4009,6 +4024,7 @@ LibraryManager.library = {
ok = true;
}
}

if (!ok) {
___setErrNo(ERRNO_CODES.EINVAL);
{{{ makeStructuralReturn(['0', '0']) }}};
Expand All @@ -4020,7 +4036,8 @@ LibraryManager.library = {
}

try {
i64Math.fromString(Pointer_stringify(start, str - start), finalBase, min, max, unsign);
var numberString = isNegative ? '-'+Pointer_stringify(start, str - start) : Pointer_stringify(start, str - start);
i64Math.fromString(numberString, finalBase, min, max, unsign);
} catch(e) {
___setErrNo(ERRNO_CODES.ERANGE); // not quite correct
}
Expand Down
198 changes: 198 additions & 0 deletions tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4532,6 +4532,204 @@ def test_stdlibs(self):

self.do_run(src, '*1*', force_c=True)

def test_strtoll_hex(self):
# tests strtoll for hex strings (0x...)
src = r'''
#include <stdio.h>
#include <stdlib.h>

int main() {
const char *STRING = "0x4 -0x3A +0xDEADBEEF";
char *end_char;

// undefined base
long long int l1 = strtoll(STRING, &end_char, 0);
long long int l2 = strtoll(end_char, &end_char, 0);
long long int l3 = strtoll(end_char, NULL, 0);

// defined base
long long int l4 = strtoll(STRING, &end_char, 16);
long long int l5 = strtoll(end_char, &end_char, 16);
long long int l6 = strtoll(end_char, NULL, 16);

printf("%d%d%d%d%d%d\n", l1==0x4, l2==-0x3a, l3==0xdeadbeef, l4==0x4, l5==-0x3a, l6==0xdeadbeef);
return 0;
}
'''
self.do_run(src, '111111')

def test_strtoll_dec(self):
# tests strtoll for decimal strings (0x...)
src = r'''
#include <stdio.h>
#include <stdlib.h>

int main() {
const char *STRING = "4 -38 +4711";
char *end_char;

// undefined base
long long int l1 = strtoll(STRING, &end_char, 0);
long long int l2 = strtoll(end_char, &end_char, 0);
long long int l3 = strtoll(end_char, NULL, 0);

// defined base
long long int l4 = strtoll(STRING, &end_char, 10);
long long int l5 = strtoll(end_char, &end_char, 10);
long long int l6 = strtoll(end_char, NULL, 10);

printf("%d%d%d%d%d%d\n", l1==4, l2==-38, l3==4711, l4==4, l5==-38, l6==4711);
return 0;
}
'''
self.do_run(src, '111111')

def test_strtoll_bin(self):
# tests strtoll for binary strings (0x...)
src = r'''
#include <stdio.h>
#include <stdlib.h>

int main() {
const char *STRING = "1 -101 +1011";
char *end_char;

// defined base
long long int l4 = strtoll(STRING, &end_char, 2);
long long int l5 = strtoll(end_char, &end_char, 2);
long long int l6 = strtoll(end_char, NULL, 2);

printf("%d%d%d\n", l4==1, l5==-5, l6==11);
return 0;
}
'''
self.do_run(src, '111')

def test_strtoll_oct(self):
# tests strtoll for decimal strings (0x...)
src = r'''
#include <stdio.h>
#include <stdlib.h>

int main() {
const char *STRING = "0 -035 +04711";
char *end_char;

// undefined base
long long int l1 = strtoll(STRING, &end_char, 0);
long long int l2 = strtoll(end_char, &end_char, 0);
long long int l3 = strtoll(end_char, NULL, 0);

// defined base
long long int l4 = strtoll(STRING, &end_char, 8);
long long int l5 = strtoll(end_char, &end_char, 8);
long long int l6 = strtoll(end_char, NULL, 8);

printf("%d%d%d%d%d%d\n", l1==0, l2==-29, l3==2505, l4==0, l5==-29, l6==2505);
return 0;
}
'''
self.do_run(src, '111111')

def test_strtol_hex(self):
# tests strtoll for hex strings (0x...)
src = r'''
#include <stdio.h>
#include <stdlib.h>

int main() {
const char *STRING = "0x4 -0x3A +0xDEAD";
char *end_char;

// undefined base
long l1 = strtol(STRING, &end_char, 0);
long l2 = strtol(end_char, &end_char, 0);
long l3 = strtol(end_char, NULL, 0);

// defined base
long l4 = strtol(STRING, &end_char, 16);
long l5 = strtol(end_char, &end_char, 16);
long l6 = strtol(end_char, NULL, 16);

printf("%d%d%d%d%d%d\n", l1==0x4, l2==-0x3a, l3==0xdead, l4==0x4, l5==-0x3a, l6==0xdead);
return 0;
}
'''
self.do_run(src, '111111')

def test_strtol_dec(self):
# tests strtoll for decimal strings (0x...)
src = r'''
#include <stdio.h>
#include <stdlib.h>

int main() {
const char *STRING = "4 -38 +4711";
char *end_char;

// undefined base
long l1 = strtol(STRING, &end_char, 0);
long l2 = strtol(end_char, &end_char, 0);
long l3 = strtol(end_char, NULL, 0);

// defined base
long l4 = strtol(STRING, &end_char, 10);
long l5 = strtol(end_char, &end_char, 10);
long l6 = strtol(end_char, NULL, 10);

printf("%d%d%d%d%d%d\n", l1==4, l2==-38, l3==4711, l4==4, l5==-38, l6==4711);
return 0;
}
'''
self.do_run(src, '111111')

def test_strtol_bin(self):
# tests strtoll for binary strings (0x...)
src = r'''
#include <stdio.h>
#include <stdlib.h>

int main() {
const char *STRING = "1 -101 +1011";
char *end_char;

// defined base
long l4 = strtol(STRING, &end_char, 2);
long l5 = strtol(end_char, &end_char, 2);
long l6 = strtol(end_char, NULL, 2);

printf("%d%d%d\n", l4==1, l5==-5, l6==11);
return 0;
}
'''
self.do_run(src, '111')

def test_strtol_oct(self):
# tests strtoll for decimal strings (0x...)
src = r'''
#include <stdio.h>
#include <stdlib.h>

int main() {
const char *STRING = "0 -035 +04711";
char *end_char;

// undefined base
long l1 = strtol(STRING, &end_char, 0);
long l2 = strtol(end_char, &end_char, 0);
long l3 = strtol(end_char, NULL, 0);

// defined base
long l4 = strtol(STRING, &end_char, 8);
long l5 = strtol(end_char, &end_char, 8);
long l6 = strtol(end_char, NULL, 8);

printf("%d%d%d%d%d%d\n", l1==0, l2==-29, l3==2505, l4==0, l5==-29, l6==2505);
return 0;
}
'''
self.do_run(src, '111111')

def test_atexit(self):
# Confirms they are called in reverse order
src = r'''
Expand Down