Skip to content

Commit 0294661

Browse files
committed
parsenum: Signedness issues.
char can be signedness, and using signedness types is dangerous - it can lead to negative offsets when doing table lookups. We apparently should just ban char usage.
1 parent 812025b commit 0294661

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

py/parsenum.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
#include <math.h>
4141
#endif
4242

43-
mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
44-
const char *restrict top = str + len;
43+
mp_obj_t mp_parse_num_integer(const char *restrict str_, uint len, int base) {
44+
const byte *restrict str = (const byte *)str_;
45+
const byte *restrict top = str + len;
4546
bool neg = false;
4647
mp_obj_t ret_val;
4748

@@ -65,11 +66,11 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
6566
}
6667

6768
// parse optional base prefix
68-
str += mp_parse_num_base(str, top - str, &base);
69+
str += mp_parse_num_base((const char*)str, top - str, &base);
6970

7071
// string should be an integer number
7172
machine_int_t int_val = 0;
72-
const char *restrict str_val_start = str;
73+
const byte *restrict str_val_start = str;
7374
for (; str < top; str++) {
7475
// get next digit as a value
7576
int dig = *str;
@@ -129,9 +130,9 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
129130
overflow:
130131
// reparse using long int
131132
{
132-
const char *s2 = str_val_start;
133+
const char *s2 = (const char*)str_val_start;
133134
ret_val = mp_obj_new_int_from_str_len(&s2, top - str_val_start, neg, base);
134-
str = s2;
135+
str = (const byte*)s2;
135136
goto have_ret_val;
136137
}
137138

py/parsenumbase.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// find real radix base, and strip preceding '0x', '0o' and '0b'
3232
// puts base in *base, and returns number of bytes to skip the prefix
3333
int mp_parse_num_base(const char *str, uint len, int *base) {
34-
const char *p = str;
34+
const byte *p = (const byte*)str;
3535
int c = *(p++);
3636
if ((*base == 0 || *base == 16) && c == '0') {
3737
c = *(p++);
@@ -63,6 +63,6 @@ int mp_parse_num_base(const char *str, uint len, int *base) {
6363
}
6464
p--;
6565
}
66-
return p - str;
66+
return p - (const byte*)str;
6767
}
6868

0 commit comments

Comments
 (0)