rename
[libc-test] / src / functional / sscanf_long.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <math.h>
5 #include <errno.h>
6 #include <sys/resource.h>
7 #include "test.h"
8
9 static void setrl(int r, long lim)
10 {
11         struct rlimit rl;
12
13         if (getrlimit(r, &rl))
14                 error("getrlimit %d: %s\n", r, strerror(errno));
15         rl.rlim_cur = lim;
16         if (setrlimit(r, &rl))
17                 error("setrlimit %d: %s\n", r, strerror(errno));
18 }
19
20 int main(void)
21 {
22         enum {n = 8*1024*1024};
23         char *s = malloc(n);
24         int i;
25         float f;
26         char c;
27
28         if (!s)
29                 return error("out of memory");
30         setrl(RLIMIT_STACK, 128*1024);
31
32         for (i = 0; i < n; i++) s[i] = '1';
33         s[n-3] = ' ';
34         s[n-1] = 0;
35
36         /*
37          * stack overflow if scanf copies s on the stack (glibc)
38          * same issue with %d except then storing the conversion
39          * result is undefined behaviour
40          */
41         i = sscanf(s, "%f %c", &f, &c);
42
43         if (i != 2)
44                 error("sscanf returned %d, want 2\n", i);
45         if (f != INFINITY)
46                 error("sscanf(longnum, \"%%f\") read %f, want inf\n", f);
47         if (c != '1')
48                 error("sscanf(\"1\", %%c) read '%c', want '1'\n", c);
49         free(s);
50         return test_status;
51 }