diff options
author | Will Newton <will.newton@imgtec.com> | 2015-05-08 01:15:19 +0300 |
---|---|---|
committer | Waldemar Brodkorb <wbx@openadk.org> | 2015-06-10 10:17:53 -0500 |
commit | b2d27c71bd13820a4263fa7ebda4c1a4a95b501c (patch) | |
tree | e9907f693fbf5a0884ccac78349290d4069bdec5 /test | |
parent | b6c1e98422165a36b19a68daaeee11856033a7f1 (diff) |
_scanf.c: Implement 'm' modifier for 'c' and '[' conversions.
The current code implements the 'm' modifier only for 's'
conversions and would cause a segfault if it was used for 'c'
or '[' conversions. This patch extends the code to cover these
cases too.
The original version could write scanned data outside the passed buffer
because index i used in the '[' conversion handling block was clobbered.
Signed-off-by: Will Newton <will.newton@imgtec.com>
Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/stdio/scanf_m.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/test/stdio/scanf_m.c b/test/stdio/scanf_m.c new file mode 100644 index 000000000..0ce78b6e4 --- /dev/null +++ b/test/stdio/scanf_m.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int main(void) +{ + const char *buf = "hello world"; + char *ps = NULL, *pc = NULL; + char s[6], c; + + /* Check that %[...]/%c work. */ + sscanf(buf, "%[a-z] %c", s, &c); + /* Check that %m[...]/%mc work. */ + sscanf(buf, "%m[a-z] %mc", &ps, &pc); + + if (strcmp(ps, "hello") != 0 || *pc != 'w' || + strcmp(s, "hello") != 0 || c != 'w') + return 1; + + free(ps); + free(pc); + + return 0; +} |