clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name str.c -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 -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -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/make/obj -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/usr.bin/make/obj -I /usr/src/usr.bin/make -D MAKE_BSIZE=256 -D DEFMAXJOBS=4 -I /usr/src/usr.bin/make/lst.lib -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/make/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fno-jump-tables -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/scan/2024-01-11-140451-98009-1 -x c /usr/src/usr.bin/make/str.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 | |
| 38 | #include <ctype.h> |
| 39 | #include <string.h> |
| 40 | #include "defines.h" |
| 41 | #include "str.h" |
| 42 | #include "memory.h" |
| 43 | #include "buf.h" |
| 44 | |
| 45 | |
| 46 | static bool range_match(char, const char **, const char *); |
| 47 | static bool star_match(const char *, const char *, const char *, const char *); |
| 48 | |
| 49 | char * |
| 50 | Str_concati(const char *s1, const char *e1, const char *s2, const char *e2, |
| 51 | int sep) |
| 52 | { |
| 53 | size_t len1, len2; |
| 54 | char *result; |
| 55 | |
| 56 | |
| 57 | len1 = e1 - s1; |
| 58 | len2 = e2 - s2; |
| 59 | |
| 60 | |
| 61 | if (sep) |
| 62 | len1++; |
| 63 | result = emalloc(len1 + len2 + 1); |
| 64 | |
| 65 | |
| 66 | memcpy(result, s1, len1); |
| 67 | |
| 68 | |
| 69 | if (sep) |
| 70 | result[len1-1] = sep; |
| 71 | |
| 72 | |
| 73 | memcpy(result + len1, s2, len2); |
| 74 | result[len1+len2] = '\0'; |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | char ** |
| 91 | brk_string(const char *str, int *store_argc, char **buffer) |
| 92 | { |
| 93 | int argc; |
| 94 | char ch; |
| 95 | char inquote; |
| 96 | const char *p; |
| 97 | char *start, *t; |
| 98 | size_t len; |
| 99 | int argmax = 50; |
| 100 | size_t curlen = 0; |
| 101 | char **argv = ereallocarray(NULL, argmax + 1, sizeof(char *)); |
| 102 | |
| 103 | |
| 104 | for (; *str == ' ' || *str == '\t'; ++str) |
| 1 | Assuming the condition is false | |
|
| 2 | | Assuming the condition is false | |
|
| 3 | | Loop condition is false. Execution continues on line 108 | |
|
| 105 | continue; |
| 106 | |
| 107 | |
| 108 | if ((len = strlen(str) + 1) > curlen) |
| |
| 109 | *buffer = emalloc(curlen = len); |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | argc = 0; |
| 116 | inquote = '\0'; |
| 117 | for (p = str, start = t = *buffer;; ++p) { |
| |
| 6 | | Loop condition is true. Entering loop body | |
|
| 118 | switch (ch = *p) { |
| 119 | case '"': |
| 120 | case '\'': |
| 121 | if (inquote) { |
| 122 | if (inquote == ch) |
| 123 | inquote = '\0'; |
| 124 | else |
| 125 | break; |
| 126 | } else { |
| 127 | inquote = ch; |
| 128 | |
| 129 | if (start == NULL && p[1] == inquote) { |
| 130 | start = t + 1; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | continue; |
| 135 | case ' ': |
| 136 | case '\t': |
| 137 | case '\n': |
| 138 | if (inquote) |
| 139 | break; |
| 140 | if (!start) |
| 141 | continue; |
| 142 | |
| 143 | case '\0': |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | if (!start) |
| 149 | goto done; |
| 150 | |
| 151 | *t++ = '\0'; |
| 152 | if (argc == argmax) { |
| 153 | argmax *= 2; |
| 154 | argv = ereallocarray(argv, |
| 155 | (argmax + 1), sizeof(char *)); |
| 156 | } |
| 157 | argv[argc++] = start; |
| 158 | start = NULL; |
| 159 | if (ch == '\n' || ch == '\0') |
| 160 | goto done; |
| 161 | continue; |
| 162 | case '\\': |
| 163 | switch (ch = *++p) { |
| 164 | case '\0': |
| 165 | case '\n': |
| 166 | |
| 167 | ch = '\\'; |
| 168 | --p; |
| 169 | break; |
| 170 | case 'b': |
| 171 | ch = '\b'; |
| 172 | break; |
| 173 | case 'f': |
| 174 | ch = '\f'; |
| 175 | break; |
| 176 | case 'n': |
| 177 | ch = '\n'; |
| 178 | break; |
| 179 | case 'r': |
| 180 | ch = '\r'; |
| 181 | break; |
| 182 | case 't': |
| 183 | ch = '\t'; |
| 184 | break; |
| 185 | } |
| 186 | break; |
| 187 | } |
| 188 | if (!start) |
| 7 | | 'Default' branch taken. Execution continues on line 188 | |
|
| 8 | | Assuming 'start' is null | |
|
| |
| 189 | start = t; |
| 190 | *t++ = ch; |
| 10 | | Dereference of null pointer |
|
| 191 | } |
| 192 | done: |
| 193 | argv[argc] = NULL; |
| 194 | *store_argc = argc; |
| 195 | return argv; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | const char * |
| 200 | iterate_words(const char **end) |
| 201 | { |
| 202 | const char *start, *p; |
| 203 | char state = 0; |
| 204 | start = *end; |
| 205 | |
| 206 | while (ISSPACE(*start)) |
| 207 | start++; |
| 208 | if (*start == '\0') |
| 209 | return NULL; |
| 210 | |
| 211 | for (p = start;; p++) |
| 212 | switch(*p) { |
| 213 | case '\\': |
| 214 | if (p[1] != '\0') |
| 215 | p++; |
| 216 | break; |
| 217 | case '\'': |
| 218 | case '"': |
| 219 | if (state == *p) |
| 220 | state = 0; |
| 221 | else if (state == 0) |
| 222 | state = *p; |
| 223 | break; |
| 224 | case ' ': |
| 225 | case '\t': |
| 226 | if (state != 0) |
| 227 | break; |
| 228 | |
| 229 | case '\0': |
| 230 | *end = p; |
| 231 | return start; |
| 232 | default: |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | static bool |
| 238 | star_match(const char *string, const char *estring, |
| 239 | const char *pattern, const char *epattern) |
| 240 | { |
| 241 | |
| 242 | |
| 243 | |
| 244 | pattern++; |
| 245 | |
| 246 | |
| 247 | while (pattern != epattern && |
| 248 | (*pattern == '?' || *pattern == '*')) { |
| 249 | if (*pattern == '?') { |
| 250 | if (string == estring) |
| 251 | return false; |
| 252 | else |
| 253 | string++; |
| 254 | } |
| 255 | pattern++; |
| 256 | } |
| 257 | if (pattern == epattern) |
| 258 | return true; |
| 259 | for (; string != estring; string++) |
| 260 | if (Str_Matchi(string, estring, pattern, |
| 261 | epattern)) |
| 262 | return true; |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | static bool |
| 267 | range_match(char c, const char **ppat, const char *epattern) |
| 268 | { |
| 269 | if (*ppat == epattern) { |
| 270 | if (c == '[') |
| 271 | return true; |
| 272 | else |
| 273 | return false; |
| 274 | } |
| 275 | if (**ppat == '!' || **ppat == '^') { |
| 276 | (*ppat)++; |
| 277 | return !range_match(c, ppat, epattern); |
| 278 | } |
| 279 | for (;;) { |
| 280 | if (**ppat == '\\') { |
| 281 | if (++(*ppat) == epattern) |
| 282 | return false; |
| 283 | } |
| 284 | if (**ppat == c) |
| 285 | break; |
| 286 | if ((*ppat)[1] == '-') { |
| 287 | if (*ppat + 2 == epattern) |
| 288 | return false; |
| 289 | if (**ppat < c && c <= (*ppat)[2]) |
| 290 | break; |
| 291 | if ((*ppat)[2] <= c && c < **ppat) |
| 292 | break; |
| 293 | *ppat += 3; |
| 294 | } else |
| 295 | (*ppat)++; |
| 296 | |
| 297 | |
| 298 | |
| 299 | if (*ppat == epattern || **ppat == ']') |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | while (**ppat != ']') { |
| 305 | if (**ppat == '\\') |
| 306 | (*ppat)++; |
| 307 | |
| 308 | |
| 309 | if (*ppat == epattern) |
| 310 | break; |
| 311 | (*ppat)++; |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | bool |
| 317 | Str_Matchi(const char *string, const char *estring, |
| 318 | const char *pattern, const char *epattern) |
| 319 | { |
| 320 | while (pattern != epattern) { |
| 321 | |
| 322 | if (*pattern == '*') |
| 323 | return star_match(string, estring, pattern, epattern); |
| 324 | else if (string == estring) |
| 325 | return false; |
| 326 | |
| 327 | |
| 328 | |
| 329 | else if (*pattern == '[') { |
| 330 | pattern++; |
| 331 | if (!range_match(*string, &pattern, epattern)) |
| 332 | return false; |
| 333 | |
| 334 | } |
| 335 | |
| 336 | else if (*pattern != '?') { |
| 337 | |
| 338 | |
| 339 | |
| 340 | if (*pattern == '\\') { |
| 341 | if (++pattern == epattern) |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | if (*pattern != *string) |
| 347 | return false; |
| 348 | } |
| 349 | pattern++; |
| 350 | string++; |
| 351 | } |
| 352 | if (string == estring) |
| 353 | return true; |
| 354 | else |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | const char * |
| 370 | Str_SYSVMatch(const char *word, const char *pattern, size_t *len) |
| 371 | { |
| 372 | const char *p = pattern; |
| 373 | const char *w = word; |
| 374 | const char *m; |
| 375 | |
| 376 | if (*p == '\0') { |
| 377 | |
| 378 | *len = strlen(w); |
| 379 | return w; |
| 380 | } |
| 381 | |
| 382 | if ((m = strchr(p, '%')) != NULL) { |
| 383 | |
| 384 | for (; p != m && *w && *w == *p; w++, p++) |
| 385 | continue; |
| 386 | |
| 387 | if (p != m) |
| 388 | return NULL; |
| 389 | |
| 390 | if (*++p == '\0') { |
| 391 | |
| 392 | *len = strlen(w); |
| 393 | return w; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | m = w; |
| 398 | |
| 399 | |
| 400 | do { |
| 401 | if (strcmp(p, w) == 0) { |
| 402 | *len = w - m; |
| 403 | return m; |
| 404 | } |
| 405 | } while (*w++ != '\0'); |
| 406 | |
| 407 | return NULL; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | void |
| 423 | Str_SYSVSubst(Buffer buf, const char *pat, const char *src, size_t len) |
| 424 | { |
| 425 | const char *m; |
| 426 | |
| 427 | if ((m = strchr(pat, '%')) != NULL) { |
| 428 | |
| 429 | Buf_Addi(buf, pat, m); |
| 430 | |
| 431 | pat = m + 1; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | Buf_AddChars(buf, len, src); |
| 436 | |
| 437 | |
| 438 | Buf_AddString(buf, pat); |
| 439 | } |
| 440 | |
| 441 | char * |
| 442 | Str_dupi(const char *begin, const char *end) |
| 443 | { |
| 444 | char *s; |
| 445 | |
| 446 | s = emalloc(end - begin + 1); |
| 447 | memcpy(s, begin, end - begin); |
| 448 | s[end-begin] = '\0'; |
| 449 | return s; |
| 450 | } |
| 451 | |
| 452 | char * |
| 453 | escape_dupi(const char *begin, const char *end, const char *set) |
| 454 | { |
| 455 | char *s, *t; |
| 456 | |
| 457 | t = s = emalloc(end - begin + 1); |
| 458 | while (begin != end) { |
| 459 | if (*begin == '\\') { |
| 460 | begin++; |
| 461 | if (begin == end) { |
| 462 | *t++ = '\\'; |
| 463 | break; |
| 464 | } |
| 465 | if (strchr(set, *begin) == NULL) |
| 466 | *t++ = '\\'; |
| 467 | } |
| 468 | *t++ = *begin++; |
| 469 | } |
| 470 | *t++ = '\0'; |
| 471 | return s; |
| 472 | } |
| 473 | |
| 474 | char * |
| 475 | Str_rchri(const char *begin, const char *end, int c) |
| 476 | { |
| 477 | if (begin != end) |
| 478 | do { |
| 479 | if (*--end == c) |
| 480 | return (char *)end; |
| 481 | } while (end != begin); |
| 482 | return NULL; |
| 483 | } |