getopt: fix null pointer arithmetic ub
[musl] / src / stdlib / strtol.c
1 #include "stdio_impl.h"
2 #include "intscan.h"
3 #include "shgetc.h"
4 #include <inttypes.h>
5 #include <limits.h>
6 #include <ctype.h>
7
8 static unsigned long long strtox(const char *s, char **p, int base, unsigned long long lim)
9 {
10         FILE f;
11         sh_fromstring(&f, s);
12         shlim(&f, 0);
13         unsigned long long y = __intscan(&f, base, 1, lim);
14         if (p) {
15                 size_t cnt = shcnt(&f);
16                 *p = (char *)s + cnt;
17         }
18         return y;
19 }
20
21 unsigned long long strtoull(const char *restrict s, char **restrict p, int base)
22 {
23         return strtox(s, p, base, ULLONG_MAX);
24 }
25
26 long long strtoll(const char *restrict s, char **restrict p, int base)
27 {
28         return strtox(s, p, base, LLONG_MIN);
29 }
30
31 unsigned long strtoul(const char *restrict s, char **restrict p, int base)
32 {
33         return strtox(s, p, base, ULONG_MAX);
34 }
35
36 long strtol(const char *restrict s, char **restrict p, int base)
37 {
38         return strtox(s, p, base, 0UL+LONG_MIN);
39 }
40
41 intmax_t strtoimax(const char *restrict s, char **restrict p, int base)
42 {
43         return strtoll(s, p, base);
44 }
45
46 uintmax_t strtoumax(const char *restrict s, char **restrict p, int base)
47 {
48         return strtoull(s, p, base);
49 }
50
51 weak_alias(strtol, __strtol_internal);
52 weak_alias(strtoul, __strtoul_internal);
53 weak_alias(strtoll, __strtoll_internal);
54 weak_alias(strtoull, __strtoull_internal);
55 weak_alias(strtoimax, __strtoimax_internal);
56 weak_alias(strtoumax, __strtoumax_internal);