clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name buf.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/lex/obj -resource-dir /usr/local/lib/clang/13.0.0 -I . -I /usr/src/usr.bin/lex -D HAVE_CONFIG_H -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/lex/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.bin/lex/buf.c
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 | #include "flexdef.h" |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | struct Buf userdef_buf; |
53 | struct Buf defs_buf; |
54 | struct Buf yydmap_buf; |
55 | struct Buf m4defs_buf; |
56 | struct Buf top_buf; |
57 | |
58 | struct Buf * |
59 | buf_print_strings(struct Buf * buf, FILE * out) |
60 | { |
61 | int i; |
62 | |
63 | if (!buf || !out) |
64 | return buf; |
65 | |
66 | for (i = 0; i < buf->nelts; i++) { |
67 | const char *s = ((char **) buf->elts)[i]; |
68 | if (s) |
69 | fprintf(out, "%s", s); |
70 | } |
71 | return buf; |
72 | } |
73 | |
74 | |
75 | struct Buf * |
76 | buf_prints(struct Buf * buf, const char *fmt, const char *s) |
77 | { |
78 | char *t; |
79 | size_t tsz; |
80 | |
81 | tsz = strlen(fmt) + strlen(s) + 1; |
82 | t = malloc(tsz); |
| |
83 | if (!t) |
| |
| |
84 | flexfatal(_("Allocation of buffer to print string failed")); |
85 | snprintf(t, tsz, fmt, s); |
86 | buf = buf_strappend(buf, t); |
| 4 | | Passing null pointer value via 2nd parameter 'str' | |
|
| |
87 | free(t); |
88 | return buf; |
89 | } |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | struct Buf * |
98 | buf_linedir(struct Buf * buf, const char *filename, int lineno) |
99 | { |
100 | const char *src; |
101 | char *dst, *t; |
102 | size_t tsz; |
103 | |
104 | tsz = strlen("#line \"\"\n") + |
105 | 2 * strlen(filename) + |
106 | (int) (1 + log10(abs(lineno))) + |
107 | 1; |
108 | t = malloc(tsz); |
109 | if (!t) |
110 | flexfatal(_("Allocation of buffer for line directive failed")); |
111 | dst = t + snprintf(t, tsz, "#line %d \"", lineno); |
112 | for (src = filename; *src; *dst++ = *src++) |
113 | if (*src == '\\') |
114 | *dst++ = '\\'; |
115 | *dst++ = '"'; |
116 | *dst++ = '\n'; |
117 | *dst = '\0'; |
118 | buf = buf_strappend(buf, t); |
119 | free(t); |
120 | return buf; |
121 | } |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | struct Buf * |
130 | buf_concat(struct Buf * dest, const struct Buf * src) |
131 | { |
132 | buf_append(dest, src->elts, src->nelts); |
133 | return dest; |
134 | } |
135 | |
136 | |
137 | |
138 | struct Buf * |
139 | buf_strnappend(struct Buf *buf, const char *str, int n) |
140 | { |
141 | buf_append(buf, str, n + 1); |
142 | |
143 | |
144 | buf->nelts--; |
145 | |
146 | return buf; |
147 | } |
148 | |
149 | |
150 | struct Buf * |
151 | buf_strappend(struct Buf *buf, const char *str) |
152 | { |
153 | return buf_strnappend(buf, str, strlen(str)); |
| 6 | | Null pointer passed as 1st argument to string length function |
|
154 | } |
155 | |
156 | |
157 | struct Buf * |
158 | buf_strdefine(struct Buf *buf, const char *str, const char *def) |
159 | { |
160 | buf_strappend(buf, "#define "); |
161 | buf_strappend(buf, " "); |
162 | buf_strappend(buf, str); |
163 | buf_strappend(buf, " "); |
164 | buf_strappend(buf, def); |
165 | buf_strappend(buf, "\n"); |
166 | return buf; |
167 | } |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | struct Buf * |
176 | buf_m4_define(struct Buf * buf, const char *def, const char *val) |
177 | { |
178 | const char *fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n"; |
179 | char *str; |
180 | size_t strsz; |
181 | |
182 | val = val ? val : ""; |
183 | strsz = strlen(fmt) + strlen(def) + strlen(val) + 2; |
184 | str = malloc(strsz); |
185 | if (!str) |
186 | flexfatal(_("Allocation of buffer for m4 def failed")); |
187 | |
188 | snprintf(str, strsz, fmt, def, val); |
189 | buf_append(buf, &str, 1); |
190 | return buf; |
191 | } |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | struct Buf * |
199 | buf_m4_undefine(struct Buf * buf, const char *def) |
200 | { |
201 | const char *fmt = "m4_undefine( [[%s]])m4_dnl\n"; |
202 | char *str; |
203 | size_t strsz; |
204 | |
205 | strsz = strlen(fmt) + strlen(def) + 2; |
206 | str = malloc(strsz); |
207 | if (!str) |
208 | flexfatal(_("Allocation of buffer for m4 undef failed")); |
209 | |
210 | snprintf(str, strsz, fmt, def); |
211 | buf_append(buf, &str, 1); |
212 | return buf; |
213 | } |
214 | |
215 | |
216 | void |
217 | buf_init(struct Buf *buf, size_t elem_size) |
218 | { |
219 | buf->elts = NULL; |
220 | buf->nelts = 0; |
221 | buf->elt_size = elem_size; |
222 | buf->nmax = 0; |
223 | } |
224 | |
225 | |
226 | void |
227 | buf_destroy(struct Buf *buf) |
228 | { |
229 | free(buf->elts); |
230 | buf->elts = NULL; |
231 | } |
232 | |
233 | |
234 | |
235 | |
236 | |
237 | |
238 | |
239 | |
240 | struct Buf * |
241 | buf_append(struct Buf *buf, const void *ptr, int n_elem) |
242 | { |
243 | int n_alloc = 0; |
244 | |
245 | if (!ptr || n_elem == 0) |
246 | return buf; |
247 | |
248 | |
249 | if (n_elem + buf->nelts > buf->nmax) { |
250 | |
251 | |
252 | n_alloc = (n_elem + buf->nelts) * buf->elt_size; |
253 | |
254 | |
255 | if (((n_alloc * buf->elt_size) % 512) != 0 |
256 | && buf->elt_size < 512) |
257 | n_alloc += |
258 | (512 - |
259 | ((n_alloc * buf->elt_size) % 512)) / |
260 | buf->elt_size; |
261 | |
262 | if (!buf->elts) |
263 | buf->elts = |
264 | allocate_array(n_alloc, buf->elt_size); |
265 | else |
266 | buf->elts = |
267 | reallocate_array(buf->elts, n_alloc, |
268 | buf->elt_size); |
269 | |
270 | buf->nmax = n_alloc; |
271 | } |
272 | memcpy((char *) buf->elts + buf->nelts * buf->elt_size, ptr, |
273 | n_elem * buf->elt_size); |
274 | buf->nelts += n_elem; |
275 | |
276 | return buf; |
277 | } |