clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name inp.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/patch/obj -resource-dir /usr/local/lib/clang/13.0.0 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/patch/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/patch/inp.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 | #include <sys/stat.h> |
30 | #include <sys/mman.h> |
31 | |
32 | #include <ctype.h> |
33 | #include <fcntl.h> |
34 | #include <stddef.h> |
35 | #include <stdint.h> |
36 | #include <stdio.h> |
37 | #include <stdlib.h> |
38 | #include <string.h> |
39 | #include <unistd.h> |
40 | |
41 | #include "common.h" |
42 | #include "util.h" |
43 | #include "pch.h" |
44 | #include "inp.h" |
45 | |
46 | |
47 | |
48 | |
49 | static off_t i_size; |
50 | static char *i_womp; |
51 | static char **i_ptr; |
52 | |
53 | static int tifd = -1; |
54 | static char *tibuf[2]; |
55 | static LINENUM tiline[2] = {-1, -1}; |
56 | static size_t lines_per_buf; |
57 | static size_t tibuflen; |
58 | static size_t tireclen; |
59 | |
60 | static bool rev_in_string(const char *); |
61 | static bool reallocate_lines(size_t *); |
62 | |
63 | |
64 | static bool plan_a(const char *); |
65 | |
66 | static void plan_b(const char *); |
67 | |
68 | |
69 | |
70 | void |
71 | re_input(void) |
72 | { |
73 | if (using_plan_a) { |
74 | free(i_ptr); |
75 | i_ptr = NULL; |
76 | if (i_womp != NULL) { |
77 | munmap(i_womp, i_size); |
78 | i_womp = NULL; |
79 | } |
80 | i_size = 0; |
81 | } else { |
82 | using_plan_a = true; |
83 | close(tifd); |
84 | tifd = -1; |
85 | free(tibuf[0]); |
86 | free(tibuf[1]); |
87 | tibuf[0] = tibuf[1] = NULL; |
88 | tiline[0] = tiline[1] = -1; |
89 | tireclen = 0; |
90 | } |
91 | } |
92 | |
93 | |
94 | |
95 | void |
96 | scan_input(const char *filename) |
97 | { |
98 | if (!plan_a(filename)) |
| |
99 | plan_b(filename); |
| |
100 | if (verbose) { |
101 | say("Patching file %s using Plan %s...\n", filename, |
102 | (using_plan_a ? "A" : "B")); |
103 | } |
104 | } |
105 | |
106 | static bool |
107 | reallocate_lines(size_t *lines_allocatedp) |
108 | { |
109 | char **p; |
110 | size_t new_size; |
111 | |
112 | new_size = *lines_allocatedp * 3 / 2; |
113 | p = reallocarray(i_ptr, new_size + 2, sizeof(char *)); |
114 | if (p == NULL) { |
115 | munmap(i_womp, i_size); |
116 | i_womp = NULL; |
117 | free(i_ptr); |
118 | i_ptr = NULL; |
119 | *lines_allocatedp = 0; |
120 | return false; |
121 | } |
122 | *lines_allocatedp = new_size; |
123 | i_ptr = p; |
124 | return true; |
125 | } |
126 | |
127 | |
128 | |
129 | static bool |
130 | plan_a(const char *filename) |
131 | { |
132 | int ifd, statfailed; |
133 | char *p, *s; |
134 | struct stat filestat; |
135 | off_t i; |
136 | ptrdiff_t sz; |
137 | size_t iline, lines_allocated; |
138 | |
139 | #ifdef DEBUGGING |
140 | if (debug & 8) |
141 | return false; |
142 | #endif |
143 | |
144 | if (filename == NULL || *filename == '\0') |
145 | return false; |
146 | |
147 | statfailed = stat(filename, &filestat); |
148 | if (statfailed && ok_to_create_file) { |
149 | int fd; |
150 | |
151 | if (verbose) |
152 | say("(Creating file %s...)\n", filename); |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | if (check_only) |
160 | return true; |
161 | makedirs(filename, true); |
162 | if ((fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0666)) != -1) |
163 | close(fd); |
164 | |
165 | statfailed = stat(filename, &filestat); |
166 | } |
167 | if (statfailed) |
168 | fatal("can't find %s\n", filename); |
169 | filemode = filestat.st_mode; |
170 | if (!S_ISREG(filemode)) |
171 | fatal("%s is not a normal file--can't patch\n", filename); |
172 | i_size = filestat.st_size; |
173 | if (out_of_mem) { |
174 | set_hunkmax(); |
175 | out_of_mem = false; |
176 | return false; |
177 | } |
178 | if (i_size > SIZE_MAX) { |
179 | say("block too large to mmap\n"); |
180 | return false; |
181 | } |
182 | if ((ifd = open(filename, O_RDONLY)) == -1) |
183 | pfatal("can't open file %s", filename); |
184 | |
185 | if (i_size) { |
186 | i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); |
187 | if (i_womp == MAP_FAILED) { |
188 | perror("mmap failed"); |
189 | i_womp = NULL; |
190 | close(ifd); |
191 | return false; |
192 | } |
193 | } else { |
194 | i_womp = NULL; |
195 | } |
196 | |
197 | close(ifd); |
198 | if (i_size) |
199 | madvise(i_womp, i_size, MADV_SEQUENTIAL); |
200 | |
201 | |
202 | lines_allocated = i_size / 25; |
203 | if (lines_allocated < 100) |
204 | lines_allocated = 100; |
205 | |
206 | if (!reallocate_lines(&lines_allocated)) |
207 | return false; |
208 | |
209 | |
210 | iline = 1; |
211 | i_ptr[iline] = i_womp; |
212 | |
213 | for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) { |
214 | if (*s == '\n') { |
215 | if (iline == lines_allocated) { |
216 | if (!reallocate_lines(&lines_allocated)) |
217 | return false; |
218 | } |
219 | |
220 | i_ptr[++iline] = s + 1; |
221 | } |
222 | } |
223 | |
224 | if (i_size > 0 && i_womp[i_size - 1] != '\n') { |
225 | last_line_missing_eol = true; |
226 | |
227 | sz = s - i_ptr[iline]; |
228 | p = malloc(sz + 1); |
229 | if (p == NULL) { |
230 | free(i_ptr); |
231 | i_ptr = NULL; |
232 | munmap(i_womp, i_size); |
233 | i_womp = NULL; |
234 | return false; |
235 | } |
236 | |
237 | memcpy(p, i_ptr[iline], sz); |
238 | p[sz] = '\n'; |
239 | i_ptr[iline] = p; |
240 | |
241 | i_ptr[++iline] = ""; |
242 | } else |
243 | last_line_missing_eol = false; |
244 | |
245 | input_lines = iline - 1; |
246 | |
247 | |
248 | |
249 | if (revision != NULL) { |
250 | if (i_womp == NULL || !rev_in_string(i_womp)) { |
251 | if (force) { |
252 | if (verbose) |
253 | say("Warning: this file doesn't appear " |
254 | "to be the %s version--patching anyway.\n", |
255 | revision); |
256 | } else if (batch) { |
257 | fatal("this file doesn't appear to be the " |
258 | "%s version--aborting.\n", |
259 | revision); |
260 | } else { |
261 | ask("This file doesn't appear to be the " |
262 | "%s version--patch anyway? [n] ", |
263 | revision); |
264 | if (*buf != 'y') |
265 | fatal("aborted\n"); |
266 | } |
267 | } else if (verbose) |
268 | say("Good. This file appears to be the %s version.\n", |
269 | revision); |
270 | } |
271 | return true; |
272 | } |
273 | |
274 | |
275 | |
276 | static void |
277 | plan_b(const char *filename) |
278 | { |
279 | FILE *ifp; |
280 | size_t i = 0, j, len, maxlen = 1; |
281 | char *lbuf = NULL, *p; |
282 | bool found_revision = (revision == NULL); |
| 3 | | Assuming 'revision' is not equal to NULL | |
|
283 | |
284 | using_plan_a = false; |
285 | if ((ifp = fopen(filename, "r")) == NULL) |
| 4 | | Assuming the condition is false | |
|
| |
286 | pfatal("can't open file %s", filename); |
287 | (void) unlink(TMPINNAME); |
288 | if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) == -1) |
| 6 | | Assuming the condition is false | |
|
| |
289 | pfatal("can't open file %s", TMPINNAME); |
290 | while ((p = fgetln(ifp, &len)) != NULL) { |
| 8 | | Assuming the condition is true | |
|
| 9 | | Loop condition is true. Entering loop body | |
|
| 18 | | Assuming the condition is true | |
|
| 19 | | Loop condition is true. Entering loop body | |
|
291 | if (p[len - 1] == '\n') |
| 10 | | Assuming the condition is false | |
|
| |
| 20 | | Assuming the condition is false | |
|
| |
292 | p[len - 1] = '\0'; |
293 | else { |
294 | |
295 | if ((lbuf = malloc(len + 1)) == NULL) |
| |
| 13 | | Assuming the condition is false | |
|
| |
| 22 | | Assuming the condition is true | |
|
| |
296 | fatal("out of memory\n"); |
| 24 | | Potential leak of memory pointed to by 'p' |
|
297 | memcpy(lbuf, p, len); |
298 | lbuf[len] = '\0'; |
299 | p = lbuf; |
300 | |
301 | last_line_missing_eol = true; |
302 | len++; |
303 | } |
304 | if (revision != NULL && !found_revision && rev_in_string(p)) |
| 15 | | Assuming the condition is false | |
|
| |
305 | found_revision = true; |
306 | if (len > maxlen) |
| |
307 | maxlen = len; |
308 | } |
309 | free(lbuf); |
310 | if (ferror(ifp)) |
311 | pfatal("can't read file %s", filename); |
312 | |
313 | if (revision != NULL) { |
314 | if (!found_revision) { |
315 | if (force) { |
316 | if (verbose) |
317 | say("Warning: this file doesn't appear " |
318 | "to be the %s version--patching anyway.\n", |
319 | revision); |
320 | } else if (batch) { |
321 | fatal("this file doesn't appear to be the " |
322 | "%s version--aborting.\n", |
323 | revision); |
324 | } else { |
325 | ask("This file doesn't appear to be the %s " |
326 | "version--patch anyway? [n] ", |
327 | revision); |
328 | if (*buf != 'y') |
329 | fatal("aborted\n"); |
330 | } |
331 | } else if (verbose) |
332 | say("Good. This file appears to be the %s version.\n", |
333 | revision); |
334 | } |
335 | fseek(ifp, 0L, SEEK_SET); |
336 | tireclen = maxlen; |
337 | tibuflen = maxlen > BUFFERSIZE ? maxlen : BUFFERSIZE; |
338 | lines_per_buf = tibuflen / maxlen; |
339 | tibuf[0] = malloc(tibuflen + 1); |
340 | if (tibuf[0] == NULL) |
341 | fatal("out of memory\n"); |
342 | tibuf[1] = malloc(tibuflen + 1); |
343 | if (tibuf[1] == NULL) |
344 | fatal("out of memory\n"); |
345 | for (i = 1;; i++) { |
346 | p = tibuf[0] + maxlen * (i % lines_per_buf); |
347 | if (i % lines_per_buf == 0) |
348 | if (write(tifd, tibuf[0], tibuflen) != |
349 | (ssize_t) tibuflen) |
350 | pfatal("can't write temp file"); |
351 | if (fgets(p, maxlen + 1, ifp) == NULL) { |
352 | input_lines = i - 1; |
353 | if (i % lines_per_buf != 0) |
354 | if (write(tifd, tibuf[0], tibuflen) != |
355 | (ssize_t) tibuflen) |
356 | pfatal("can't write temp file"); |
357 | break; |
358 | } |
359 | j = strlen(p); |
360 | |
361 | if (j == 0 || p[j - 1] != '\n') |
362 | p[j] = '\n'; |
363 | } |
364 | fclose(ifp); |
365 | close(tifd); |
366 | if ((tifd = open(TMPINNAME, O_RDONLY)) == -1) |
367 | pfatal("can't reopen file %s", TMPINNAME); |
368 | } |
369 | |
370 | |
371 | |
372 | |
373 | char * |
374 | ifetch(LINENUM line, int whichbuf) |
375 | { |
376 | if (line < 1 || line > input_lines) { |
377 | if (warn_on_invalid_line) { |
378 | say("No such line %ld in input file, ignoring\n", line); |
379 | warn_on_invalid_line = false; |
380 | } |
381 | return NULL; |
382 | } |
383 | if (using_plan_a) |
384 | return i_ptr[line]; |
385 | else { |
386 | LINENUM offline = line % lines_per_buf; |
387 | LINENUM baseline = line - offline; |
388 | |
389 | if (tiline[0] == baseline) |
390 | whichbuf = 0; |
391 | else if (tiline[1] == baseline) |
392 | whichbuf = 1; |
393 | else { |
394 | tiline[whichbuf] = baseline; |
395 | |
396 | if (lseek(tifd, (off_t) (baseline / lines_per_buf * |
397 | tibuflen), SEEK_SET) == -1) |
398 | pfatal("cannot seek in the temporary input file"); |
399 | |
400 | if (read(tifd, tibuf[whichbuf], tibuflen) |
401 | != (ssize_t) tibuflen) |
402 | pfatal("error reading tmp file %s", TMPINNAME); |
403 | } |
404 | return tibuf[whichbuf] + (tireclen * offline); |
405 | } |
406 | } |
407 | |
408 | |
409 | |
410 | |
411 | static bool |
412 | rev_in_string(const char *string) |
413 | { |
414 | const char *s; |
415 | size_t patlen; |
416 | |
417 | if (revision == NULL) |
418 | return true; |
419 | patlen = strlen(revision); |
420 | if (strnEQ(string, revision, patlen) && |
421 | isspace((unsigned char)string[patlen])) |
422 | return true; |
423 | for (s = string; *s; s++) { |
424 | if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) && |
425 | isspace((unsigned char)s[patlen + 1])) { |
426 | return true; |
427 | } |
428 | } |
429 | return false; |
430 | } |