analyzer: Fix PR analyzer/101980
[official-gcc.git] / gcc / testsuite / gcc.dg / analyzer / pr100615.c
blob7a06f987d4138ffd2c3b17e38a75544ce4999011
1 /* Adapted from
2 https://github.com/stackpath/rxtxcpu/blob/816d86c5d49c4db2ea5649f6b87e96da5af660f1/cpu.c
3 which is MIT-licensed. */
5 typedef __SIZE_TYPE__ size_t;
6 #define NULL ((void *)0)
8 extern size_t strlen (const char *__s)
9 __attribute__ ((__nothrow__ , __leaf__))
10 __attribute__ ((__pure__))
11 __attribute__ ((__nonnull__ (1)));
12 extern char *strdup (const char *__s)
13 __attribute__ ((__nothrow__ , __leaf__))
14 __attribute__ ((__malloc__))
15 __attribute__ ((__nonnull__ (1)));
16 extern char *strsep (char **__restrict __stringp,
17 const char *__restrict __delim)
18 __attribute__ ((__nothrow__ , __leaf__))
19 __attribute__ ((__nonnull__ (1, 2)));
20 extern long int strtol (const char *__restrict __nptr,
21 char **__restrict __endptr, int __base)
22 __attribute__ ((__nothrow__ , __leaf__))
23 __attribute__ ((__nonnull__ (1)));
24 extern void free (void *__ptr)
25 __attribute__ ((__nothrow__ , __leaf__));
27 #define CPU_LIST_BASE 10
29 int parse_cpu_list(char *cpu_list) {
30 if (strlen(cpu_list) == 0) {
31 return 0;
34 char *endptr;
35 char *tofree, *string, *range;
37 tofree = string = strdup(cpu_list); /* { dg-message "allocated here" } */
39 while ((range = strsep(&string, ",")) != NULL) {
40 int first = strtol(range, &endptr, CPU_LIST_BASE);
41 if (!*endptr) {
42 continue;
44 char *save = endptr;
45 endptr++;
46 int last = strtol(endptr, &endptr, CPU_LIST_BASE);
47 if (save[0] != '-' || *endptr || last < first) {
48 return -1; /* { dg-warning "leak of 'tofree'" } */
51 free(tofree);
52 return 0;