aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/deref-before-check-pr108475-haproxy-tcpcheck.c
blob: 1180e17e5552a12584d419c2eee79ca2c0843f8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Reduced from haproxy-2.7.1: src/tcpcheck.c.  */

/* { dg-additional-options "-Wno-analyzer-too-complex" } */

typedef __SIZE_TYPE__ size_t;
#define NULL ((void *)0)

extern void *calloc(size_t __nmemb, size_t __size)
    __attribute__((__nothrow__, __leaf__)) __attribute__((__malloc__))
    __attribute__((__alloc_size__(1, 2)));
extern char *strdup(const char *__s) __attribute__((__nothrow__, __leaf__))
__attribute__((__malloc__)) __attribute__((__nonnull__(1)));
extern char *strstr(const char *__haystack, const char *__needle)
    __attribute__((__nothrow__, __leaf__)) __attribute__((__pure__))
    __attribute__((__nonnull__(1, 2)));
extern size_t strlen(const char *__s) __attribute__((__nothrow__, __leaf__))
__attribute__((__pure__)) __attribute__((__nonnull__(1)));
struct list {
  struct list *n;
  struct list *p;
};
struct buffer {
  size_t size;
  char *area;
  size_t data;
  size_t head;
};
struct proxy;
struct ist {
  char *ptr;
  size_t len;
};
static inline int isttest(const struct ist ist) { return ist.ptr != NULL; }

enum http_meth_t {
  HTTP_METH_OPTIONS,
  /* [...snip...] */
} __attribute__((packed));

struct http_meth {
  enum http_meth_t meth;
  struct buffer str;
};
enum tcpcheck_send_type {
  /* [...snip...] */
  TCPCHK_SEND_HTTP,
};
enum tcpcheck_rule_type {
  TCPCHK_ACT_SEND = 0,
  /* [...snip...] */
};
struct tcpcheck_http_hdr {
  struct ist name;
  struct list value;
  struct list list;
};
struct tcpcheck_send {
  enum tcpcheck_send_type type;
  union {
    /* [...snip...] */
    struct {
      unsigned int flags;
      struct http_meth meth;
      union {
        struct ist uri;
        /* [...snip...] */
      };
      struct ist vsn;
      struct list hdrs;
      /* [...snip...] */
    } http;
  };
};
struct tcpcheck_rule {
  /* [...snip...] */
  enum tcpcheck_rule_type action;
  /* [...snip...] */
  union {
    /* [...snip...] */
    struct tcpcheck_send send;
    /* [...snip...] */
  };
};
enum http_meth_t find_http_meth(const char *str, const int len);
void free_tcpcheck(struct tcpcheck_rule *rule, int in_pool);
void free_tcpcheck_http_hdr(struct tcpcheck_http_hdr *hdr);

#define ist(str) ({                                                    \
	char *__x = (void *)(str);                                     \
	(struct ist){                                                  \
		.ptr = __x,                                            \
		.len = __builtin_constant_p(str) ?                     \
			((void *)str == (void *)0) ? 0 :               \
			__builtin_strlen(__x) :                        \
			({                                             \
				size_t __l = 0;                        \
				if (__x) for (__l--; __x[++__l]; ) ;   \
				__l;                                   \
			})                                             \
	};                                                             \
})

struct tcpcheck_rule *proxy_parse_httpchk_req(char **args, int cur_arg,
                                              struct proxy *px, char **errmsg) {
  struct tcpcheck_rule *chk = NULL;
  struct tcpcheck_http_hdr *hdr = NULL;
  char *meth = NULL, *uri = NULL, *vsn = NULL;
  char *hdrs, *body;

  hdrs = (*args[cur_arg + 2] ? strstr(args[cur_arg + 2], "\r\n") : NULL);
  body = (*args[cur_arg + 2] ? strstr(args[cur_arg + 2], "\r\n\r\n") : NULL);
  if (hdrs || body) {
    /* [...snip...] */
    goto error;
  }

  chk = calloc(1, sizeof(*chk));
  if (!chk) {
    /* [...snip...] */
    goto error;
  }
  chk->action = TCPCHK_ACT_SEND;
  chk->send.type = TCPCHK_SEND_HTTP;
  chk->send.http.flags |= 0x0004;
  chk->send.http.meth.meth = HTTP_METH_OPTIONS;
  ((&chk->send.http.hdrs)->n = (&chk->send.http.hdrs)->p =
       (&chk->send.http.hdrs));

  if (*args[cur_arg]) {
    if (!*args[cur_arg + 1])
      uri = args[cur_arg];
    else
      meth = args[cur_arg];
  }
  if (*args[cur_arg + 1])
    uri = args[cur_arg + 1];
  if (*args[cur_arg + 2])
    vsn = args[cur_arg + 2];

  if (meth) { /* { dg-bogus "check of 'meth' for NULL after already dereferencing it" } */
    chk->send.http.meth.meth = find_http_meth(meth, strlen(meth));
    chk->send.http.meth.str.area = strdup(meth);
    chk->send.http.meth.str.data = strlen(meth);
    if (!chk->send.http.meth.str.area) {
      /* [...snip...] */
      goto error;
    }
  }
  if (uri) {
    chk->send.http.uri = ist(strdup(uri));
    if (!isttest(chk->send.http.uri)) {
      /* [...snip...] */
      goto error;
    }
  }
  if (vsn) { /* { dg-bogus "check of 'vsn' for NULL after already dereferencing it" } */
    chk->send.http.vsn = ist(strdup(vsn));
    if (!isttest(chk->send.http.vsn)) {
      /* [...snip...] */
      goto error;
    }
  }
  return chk;

error:
  free_tcpcheck_http_hdr(hdr);
  free_tcpcheck(chk, 0);
  return NULL;
}