mlibc port: temp fix do_scanf while fix is not upstreamed

This commit is contained in:
Matheus
2024-05-07 12:24:49 -03:00
parent ed5adaceeb
commit 256360d0e9
+172
View File
@@ -29,6 +29,178 @@ index 471604e..3c5bea0 100644
else else
error('No sysdeps defined for OS: ' + host_machine.system()) error('No sysdeps defined for OS: ' + host_machine.system())
endif endif
diff --git mlibc-clean/options/ansi/generic/stdio-stubs.cpp mlibc-workdir/options/ansi/generic/stdio-stubs.cpp
index 574d4b5..698304b 100644
--- mlibc-clean/options/ansi/generic/stdio-stubs.cpp
+++ mlibc-workdir/options/ansi/generic/stdio-stubs.cpp
@@ -349,6 +349,8 @@ static void store_int(void *dest, unsigned int size, unsigned long long i) {
template<typename H>
static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
+ #define NOMATCH_CHECK(cond) if(cond) return match_count // if cond is true, matching error
+ #define EOF_CHECK(cond) if(cond) return match_count ? match_count : EOF // if cond is true, no more data to read
int match_count = 0;
for (; *fmt; fmt++) {
@@ -468,8 +470,10 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
case 'i': {
bool is_negative = false;
unsigned long long res = 0;
+ char c = handler.look_ahead();
+ EOF_CHECK(c == '\0');
- if((*fmt == 'i' || *fmt == 'd') && handler.look_ahead() == '-') {
+ if((*fmt == 'i' || *fmt == 'd') && c == '-') {
handler.consume();
is_negative = true;
}
@@ -484,11 +488,11 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
}
}
- char c = handler.look_ahead();
+ c = handler.look_ahead();
+ int count = 0;
switch (base) {
case 10:
- if(!isdigit(c))
- return match_count;
+ NOMATCH_CHECK(!isdigit(c));
while (c >= '0' && c <= '9') {
handler.consume();
res = res * 10 + (c - '0');
@@ -496,14 +500,6 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
}
break;
case 16:
- if (c == '0') {
- handler.consume();
- c = handler.look_ahead();
- if (c == 'x') {
- handler.consume();
- c = handler.look_ahead();
- }
- }
while (true) {
if (c >= '0' && c <= '9') {
handler.consume();
@@ -517,8 +513,10 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
} else {
break;
}
+ count++;
c = handler.look_ahead();
}
+ NOMATCH_CHECK(count == 0);
break;
case 8:
while (c >= '0' && c <= '7') {
@@ -527,6 +525,7 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
c = handler.look_ahead();
}
break;
+ // no need for a match check, the starting 0 was already consumed
}
if (dest) {
if(is_negative)
@@ -539,6 +538,8 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
case 'o': {
unsigned long long res = 0;
char c = handler.look_ahead();
+ EOF_CHECK(c);
+ NOMATCH_CHECK(!(c >= '0' && c <= '7'));
while (c >= '0' && c <= '7') {
handler.consume();
res = res * 8 + (c - '0');
@@ -552,6 +553,8 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
case 'X': {
unsigned long long res = 0;
char c = handler.look_ahead();
+ int count = 0;
+ EOF_CHECK(c == '\0');
if (c == '0') {
handler.consume();
c = handler.look_ahead();
@@ -573,8 +576,10 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
} else {
break;
}
+ count++;
c = handler.look_ahead();
}
+ NOMATCH_CHECK(count == 0);
if (dest)
store_int(dest, type, res);
break;
@@ -583,6 +588,7 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
char *typed_dest = (char *)dest;
char c = handler.look_ahead();
int count = 0;
+ EOF_CHECK(c == '\0');
while (c && !isspace(c)) {
handler.consume();
if (typed_dest)
@@ -592,6 +598,7 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
if (width && count >= width)
break;
}
+ NOMATCH_CHECK(count == 0);
if (typed_dest)
typed_dest[count] = '\0';
break;
@@ -599,6 +606,7 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
case 'c': {
char *typed_dest = (char *)dest;
char c = handler.look_ahead();
+ EOF_CHECK(c == '\0');
int count = 0;
if (!width)
width = 1;
@@ -644,6 +652,7 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
char *typed_dest = (char *)dest;
int count = 0;
char c = handler.look_ahead();
+ EOF_CHECK(c == '\0');
while (c && (!width || count < width)) {
handler.consume();
if (!scanset[1 + c])
@@ -653,6 +662,7 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
c = handler.look_ahead();
count++;
}
+ NOMATCH_CHECK(count == 0);
if (typed_dest)
typed_dest[count] = '\0';
break;
@@ -660,6 +670,9 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
case 'p': {
unsigned long long res = 0;
char c = handler.look_ahead();
+ int count = 0;
+ EOF_CHECK(c == '\0');
+
if (c == '0') {
handler.consume();
c = handler.look_ahead();
@@ -668,6 +681,7 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
c = handler.look_ahead();
}
}
+
while (true) {
if (c >= '0' && c <= '9') {
handler.consume();
@@ -681,8 +695,10 @@ static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
} else {
break;
}
+ count++;
c = handler.look_ahead();
}
+ NOMATCH_CHECK(count == 0);
void **typed_dest = (void **)dest;
*typed_dest = (void *)(uintptr_t)res;
break;
diff --git mlibc-clean/options/ansi/generic/string-stubs.cpp mlibc-workdir/options/ansi/generic/string-stubs.cpp diff --git mlibc-clean/options/ansi/generic/string-stubs.cpp mlibc-workdir/options/ansi/generic/string-stubs.cpp
index 8defd0e..d6cb65b 100644 index 8defd0e..d6cb65b 100644
--- mlibc-clean/options/ansi/generic/string-stubs.cpp --- mlibc-clean/options/ansi/generic/string-stubs.cpp