diff options
author | Richard Braun <rbraun@sceen.net> | 2017-06-11 00:46:52 +0200 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2017-06-11 00:46:52 +0200 |
commit | 62eebd19dfd71a16df670590d0c61fbd50f18d24 (patch) | |
tree | 11e899fa69c73ab5e80a164bd9827fc8cebd7691 | |
parent | 77a2a8ad9c3590e04b49cdd64b5e5a342afa1d60 (diff) |
fmt: fix 0 and octal integer parsing
-rw-r--r-- | fmt.c | 6 | ||||
-rw-r--r-- | test/test_fmt_sscanf.c | 34 |
2 files changed, 40 insertions, 0 deletions
@@ -1109,11 +1109,17 @@ fmt_sscanf_state_produce_int(struct fmt_sscanf_state *state) c = fmt_sscanf_state_consume_string(state); } else { fmt_sscanf_state_restore_string(state); + c = '0'; } } else { if (state->base == 0) { state->base = 8; } + + if (state->base != 8) { + fmt_sscanf_state_restore_string(state); + c = '0'; + } } } diff --git a/test/test_fmt_sscanf.c b/test/test_fmt_sscanf.c index b2a4e74..8abab69 100644 --- a/test/test_fmt_sscanf.c +++ b/test/test_fmt_sscanf.c @@ -677,6 +677,38 @@ test_38(void) #undef STRING } +static void +test_39(void) +{ + int reta, retb; + unsigned int ia, ib; + +#define STRING "0" +#define FORMAT "%u" + reta = sscanf(STRING, FORMAT, &ia); + retb = fmt_sscanf(STRING, FORMAT, &ib); + check(reta == retb); + check(ia == ib); +#undef FORMAT +#undef STRING +} + +static void +test_40(void) +{ + int reta, retb; + int ia, ib; + +#define STRING "-0" +#define FORMAT "%d" + reta = sscanf(STRING, FORMAT, &ia); + retb = fmt_sscanf(STRING, FORMAT, &ib); + check(reta == retb); + check(ia == ib); +#undef FORMAT +#undef STRING +} + int main(int argc, char *argv[]) { @@ -721,6 +753,8 @@ main(int argc, char *argv[]) test_36(); test_37(); test_38(); + test_39(); + test_40(); return 0; } |