clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name c_test.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/bin/ksh/obj -resource-dir /usr/local/lib/clang/13.0.0 -D EMACS -D VI -I . -I /usr/src/bin/ksh -I /usr/src/bin/ksh/../../lib/libc/gen -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/bin/ksh/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/bin/ksh/c_test.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | #include <sys/stat.h> |
| 13 | |
| 14 | #include <string.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #include "sh.h" |
| 18 | #include "c_test.h" |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | #define T_ERR_EXIT 2 /* POSIX says > 1 for errors */ |
| 41 | |
| 42 | struct t_op { |
| 43 | char op_text[4]; |
| 44 | Test_op op_num; |
| 45 | }; |
| 46 | static const struct t_op u_ops [] = { |
| 47 | {"-a", TO_FILAXST }, |
| 48 | {"-b", TO_FILBDEV }, |
| 49 | {"-c", TO_FILCDEV }, |
| 50 | {"-d", TO_FILID }, |
| 51 | {"-e", TO_FILEXST }, |
| 52 | {"-f", TO_FILREG }, |
| 53 | {"-G", TO_FILGID }, |
| 54 | {"-g", TO_FILSETG }, |
| 55 | {"-h", TO_FILSYM }, |
| 56 | {"-H", TO_FILCDF }, |
| 57 | {"-k", TO_FILSTCK }, |
| 58 | {"-L", TO_FILSYM }, |
| 59 | {"-n", TO_STNZE }, |
| 60 | {"-O", TO_FILUID }, |
| 61 | {"-o", TO_OPTION }, |
| 62 | {"-p", TO_FILFIFO }, |
| 63 | {"-r", TO_FILRD }, |
| 64 | {"-s", TO_FILGZ }, |
| 65 | {"-S", TO_FILSOCK }, |
| 66 | {"-t", TO_FILTT }, |
| 67 | {"-u", TO_FILSETU }, |
| 68 | {"-w", TO_FILWR }, |
| 69 | {"-x", TO_FILEX }, |
| 70 | {"-z", TO_STZER }, |
| 71 | {"", TO_NONOP } |
| 72 | }; |
| 73 | static const struct t_op b_ops [] = { |
| 74 | {"=", TO_STEQL }, |
| 75 | {"==", TO_STEQL }, |
| 76 | {"!=", TO_STNEQ }, |
| 77 | {"<", TO_STLT }, |
| 78 | {">", TO_STGT }, |
| 79 | {"-eq", TO_INTEQ }, |
| 80 | {"-ne", TO_INTNE }, |
| 81 | {"-gt", TO_INTGT }, |
| 82 | {"-ge", TO_INTGE }, |
| 83 | {"-lt", TO_INTLT }, |
| 84 | {"-le", TO_INTLE }, |
| 85 | {"-ef", TO_FILEQ }, |
| 86 | {"-nt", TO_FILNT }, |
| 87 | {"-ot", TO_FILOT }, |
| 88 | {"", TO_NONOP } |
| 89 | }; |
| 90 | |
| 91 | static int test_eaccess(const char *, int); |
| 92 | static int test_oexpr(Test_env *, int); |
| 93 | static int test_aexpr(Test_env *, int); |
| 94 | static int test_nexpr(Test_env *, int); |
| 95 | static int test_primary(Test_env *, int); |
| 96 | static int ptest_isa(Test_env *, Test_meta); |
| 97 | static const char *ptest_getopnd(Test_env *, Test_op, int); |
| 98 | static int ptest_eval(Test_env *, Test_op, const char *, |
| 99 | const char *, int); |
| 100 | static void ptest_error(Test_env *, int, const char *); |
| 101 | |
| 102 | int |
| 103 | c_test(char **wp) |
| 104 | { |
| 105 | int argc; |
| 106 | int res; |
| 107 | Test_env te; |
| 108 | |
| 109 | te.flags = 0; |
| 110 | te.isa = ptest_isa; |
| 111 | te.getopnd = ptest_getopnd; |
| 112 | te.eval = ptest_eval; |
| 113 | te.error = ptest_error; |
| 114 | |
| 115 | for (argc = 0; wp[argc]; argc++) |
| 1 | Assuming pointer value is null | |
|
| 2 | | Loop condition is false. Execution continues on line 118 | |
|
| 116 | ; |
| 117 | |
| 118 | if (strcmp(wp[0], "[") == 0) { |
| 3 | | Null pointer passed as 1st argument to string comparison function |
|
| 119 | if (strcmp(wp[--argc], "]") != 0) { |
| 120 | bi_errorf("missing ]"); |
| 121 | return T_ERR_EXIT; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | te.pos.wp = wp + 1; |
| 126 | te.wp_end = wp + argc; |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | if (argc <= 5) { |
| 134 | char **owp = wp; |
| 135 | int invert = 0; |
| 136 | Test_op op; |
| 137 | const char *opnd1, *opnd2; |
| 138 | |
| 139 | while (--argc >= 0) { |
| 140 | if ((*te.isa)(&te, TM_END)) |
| 141 | return !0; |
| 142 | if (argc == 3) { |
| 143 | opnd1 = (*te.getopnd)(&te, TO_NONOP, 1); |
| 144 | if ((op = (Test_op) (*te.isa)(&te, TM_BINOP))) { |
| 145 | opnd2 = (*te.getopnd)(&te, op, 1); |
| 146 | res = (*te.eval)(&te, op, opnd1, |
| 147 | opnd2, 1); |
| 148 | if (te.flags & TEF_ERROR) |
| 149 | return T_ERR_EXIT; |
| 150 | if (invert & 1) |
| 151 | res = !res; |
| 152 | return !res; |
| 153 | } |
| 154 | |
| 155 | te.pos.wp--; |
| 156 | } |
| 157 | if (argc == 1) { |
| 158 | opnd1 = (*te.getopnd)(&te, TO_NONOP, 1); |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | if (!Flag(FPOSIX) && strcmp(opnd1, "-t") == 0) |
| 164 | break; |
| 165 | res = (*te.eval)(&te, TO_STNZE, opnd1, |
| 166 | NULL, 1); |
| 167 | if (invert & 1) |
| 168 | res = !res; |
| 169 | return !res; |
| 170 | } |
| 171 | if ((*te.isa)(&te, TM_NOT)) { |
| 172 | invert++; |
| 173 | } else |
| 174 | break; |
| 175 | } |
| 176 | te.pos.wp = owp + 1; |
| 177 | } |
| 178 | |
| 179 | return test_parse(&te); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | Test_op |
| 187 | test_isop(Test_env *te, Test_meta meta, const char *s) |
| 188 | { |
| 189 | char sc1; |
| 190 | const struct t_op *otab; |
| 191 | |
| 192 | otab = meta == TM_UNOP ? u_ops : b_ops; |
| 193 | if (*s) { |
| 194 | sc1 = s[1]; |
| 195 | for (; otab->op_text[0]; otab++) |
| 196 | if (sc1 == otab->op_text[1] && |
| 197 | strcmp(s, otab->op_text) == 0) |
| 198 | return otab->op_num; |
| 199 | } |
| 200 | return TO_NONOP; |
| 201 | } |
| 202 | |
| 203 | int |
| 204 | test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2, |
| 205 | int do_eval) |
| 206 | { |
| 207 | int res; |
| 208 | int not; |
| 209 | struct stat b1, b2; |
| 210 | |
| 211 | if (!do_eval) |
| 212 | return 0; |
| 213 | |
| 214 | switch ((int) op) { |
| 215 | |
| 216 | |
| 217 | |
| 218 | case TO_STNZE: |
| 219 | return *opnd1 != '\0'; |
| 220 | case TO_STZER: |
| 221 | return *opnd1 == '\0'; |
| 222 | case TO_OPTION: |
| 223 | if ((not = *opnd1 == '!')) |
| 224 | opnd1++; |
| 225 | if ((res = option(opnd1)) < 0) |
| 226 | res = 0; |
| 227 | else { |
| 228 | res = Flag(res); |
| 229 | if (not) |
| 230 | res = !res; |
| 231 | } |
| 232 | return res; |
| 233 | case TO_FILRD: |
| 234 | return test_eaccess(opnd1, R_OK) == 0; |
| 235 | case TO_FILWR: |
| 236 | return test_eaccess(opnd1, W_OK) == 0; |
| 237 | case TO_FILEX: |
| 238 | return test_eaccess(opnd1, X_OK) == 0; |
| 239 | case TO_FILAXST: |
| 240 | return stat(opnd1, &b1) == 0; |
| 241 | case TO_FILEXST: |
| 242 | |
| 243 | |
| 244 | |
| 245 | return stat(opnd1, &b1) == 0; |
| 246 | case TO_FILREG: |
| 247 | return stat(opnd1, &b1) == 0 && S_ISREG(b1.st_mode); |
| 248 | case TO_FILID: |
| 249 | return stat(opnd1, &b1) == 0 && S_ISDIR(b1.st_mode); |
| 250 | case TO_FILCDEV: |
| 251 | return stat(opnd1, &b1) == 0 && S_ISCHR(b1.st_mode); |
| 252 | case TO_FILBDEV: |
| 253 | return stat(opnd1, &b1) == 0 && S_ISBLK(b1.st_mode); |
| 254 | case TO_FILFIFO: |
| 255 | return stat(opnd1, &b1) == 0 && S_ISFIFO(b1.st_mode); |
| 256 | case TO_FILSYM: |
| 257 | return lstat(opnd1, &b1) == 0 && S_ISLNK(b1.st_mode); |
| 258 | case TO_FILSOCK: |
| 259 | return stat(opnd1, &b1) == 0 && S_ISSOCK(b1.st_mode); |
| 260 | case TO_FILCDF: |
| 261 | return 0; |
| 262 | case TO_FILSETU: |
| 263 | return stat(opnd1, &b1) == 0 && |
| 264 | (b1.st_mode & S_ISUID) == S_ISUID; |
| 265 | case TO_FILSETG: |
| 266 | return stat(opnd1, &b1) == 0 && |
| 267 | (b1.st_mode & S_ISGID) == S_ISGID; |
| 268 | case TO_FILSTCK: |
| 269 | return stat(opnd1, &b1) == 0 && |
| 270 | (b1.st_mode & S_ISVTX) == S_ISVTX; |
| 271 | case TO_FILGZ: |
| 272 | return stat(opnd1, &b1) == 0 && b1.st_size > 0L; |
| 273 | case TO_FILTT: |
| 274 | if (opnd1 && !bi_getn(opnd1, &res)) { |
| 275 | te->flags |= TEF_ERROR; |
| 276 | res = 0; |
| 277 | } else { |
| 278 | |
| 279 | res = isatty(opnd1 ? res : 0); |
| 280 | } |
| 281 | return res; |
| 282 | case TO_FILUID: |
| 283 | return stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid; |
| 284 | case TO_FILGID: |
| 285 | return stat(opnd1, &b1) == 0 && b1.st_gid == getegid(); |
| 286 | |
| 287 | |
| 288 | |
| 289 | case TO_STEQL: |
| 290 | if (te->flags & TEF_DBRACKET) |
| 291 | return gmatch(opnd1, opnd2, false); |
| 292 | return strcmp(opnd1, opnd2) == 0; |
| 293 | case TO_STNEQ: |
| 294 | if (te->flags & TEF_DBRACKET) |
| 295 | return !gmatch(opnd1, opnd2, false); |
| 296 | return strcmp(opnd1, opnd2) != 0; |
| 297 | case TO_STLT: |
| 298 | return strcmp(opnd1, opnd2) < 0; |
| 299 | case TO_STGT: |
| 300 | return strcmp(opnd1, opnd2) > 0; |
| 301 | case TO_INTEQ: |
| 302 | case TO_INTNE: |
| 303 | case TO_INTGE: |
| 304 | case TO_INTGT: |
| 305 | case TO_INTLE: |
| 306 | case TO_INTLT: |
| 307 | { |
| 308 | int64_t v1, v2; |
| 309 | |
| 310 | if (!evaluate(opnd1, &v1, KSH_RETURN_ERROR, false) || |
| 311 | !evaluate(opnd2, &v2, KSH_RETURN_ERROR, false)) { |
| 312 | |
| 313 | te->flags |= TEF_ERROR; |
| 314 | return 1; |
| 315 | } |
| 316 | switch ((int) op) { |
| 317 | case TO_INTEQ: |
| 318 | return v1 == v2; |
| 319 | case TO_INTNE: |
| 320 | return v1 != v2; |
| 321 | case TO_INTGE: |
| 322 | return v1 >= v2; |
| 323 | case TO_INTGT: |
| 324 | return v1 > v2; |
| 325 | case TO_INTLE: |
| 326 | return v1 <= v2; |
| 327 | case TO_INTLT: |
| 328 | return v1 < v2; |
| 329 | } |
| 330 | } |
| 331 | case TO_FILNT: |
| 332 | { |
| 333 | int s2; |
| 334 | |
| 335 | |
| 336 | |
| 337 | return stat(opnd1, &b1) == 0 && |
| 338 | (((s2 = stat(opnd2, &b2)) == 0 && |
| 339 | b1.st_mtime > b2.st_mtime) || s2 < 0); |
| 340 | } |
| 341 | case TO_FILOT: |
| 342 | { |
| 343 | int s1; |
| 344 | |
| 345 | |
| 346 | |
| 347 | return stat(opnd2, &b2) == 0 && |
| 348 | (((s1 = stat(opnd1, &b1)) == 0 && |
| 349 | b1.st_mtime < b2.st_mtime) || s1 < 0); |
| 350 | } |
| 351 | case TO_FILEQ: |
| 352 | return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0 && |
| 353 | b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino; |
| 354 | } |
| 355 | (*te->error)(te, 0, "internal error: unknown op"); |
| 356 | return 1; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | |
| 361 | static int |
| 362 | test_eaccess(const char *path, int amode) |
| 363 | { |
| 364 | int res; |
| 365 | |
| 366 | res = access(path, amode); |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | if (res == 0 && ksheuid == 0 && (amode & X_OK)) { |
| 372 | struct stat statb; |
| 373 | |
| 374 | if (stat(path, &statb) == -1) |
| 375 | res = -1; |
| 376 | else if (S_ISDIR(statb.st_mode)) |
| 377 | res = 0; |
| 378 | else |
| 379 | res = (statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) ? |
| 380 | 0 : -1; |
| 381 | } |
| 382 | |
| 383 | return res; |
| 384 | } |
| 385 | |
| 386 | int |
| 387 | test_parse(Test_env *te) |
| 388 | { |
| 389 | int res; |
| 390 | |
| 391 | res = test_oexpr(te, 1); |
| 392 | |
| 393 | if (!(te->flags & TEF_ERROR) && !(*te->isa)(te, TM_END)) |
| 394 | (*te->error)(te, 0, "unexpected operator/operand"); |
| 395 | |
| 396 | return (te->flags & TEF_ERROR) ? T_ERR_EXIT : !res; |
| 397 | } |
| 398 | |
| 399 | static int |
| 400 | test_oexpr(Test_env *te, int do_eval) |
| 401 | { |
| 402 | int res; |
| 403 | |
| 404 | res = test_aexpr(te, do_eval); |
| 405 | if (res) |
| 406 | do_eval = 0; |
| 407 | if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_OR)) |
| 408 | return test_oexpr(te, do_eval) || res; |
| 409 | return res; |
| 410 | } |
| 411 | |
| 412 | static int |
| 413 | test_aexpr(Test_env *te, int do_eval) |
| 414 | { |
| 415 | int res; |
| 416 | |
| 417 | res = test_nexpr(te, do_eval); |
| 418 | if (!res) |
| 419 | do_eval = 0; |
| 420 | if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_AND)) |
| 421 | return test_aexpr(te, do_eval) && res; |
| 422 | return res; |
| 423 | } |
| 424 | |
| 425 | static int |
| 426 | test_nexpr(Test_env *te, int do_eval) |
| 427 | { |
| 428 | if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_NOT)) |
| 429 | return !test_nexpr(te, do_eval); |
| 430 | return test_primary(te, do_eval); |
| 431 | } |
| 432 | |
| 433 | static int |
| 434 | test_primary(Test_env *te, int do_eval) |
| 435 | { |
| 436 | const char *opnd1, *opnd2; |
| 437 | int res; |
| 438 | Test_op op; |
| 439 | |
| 440 | if (te->flags & TEF_ERROR) |
| 441 | return 0; |
| 442 | if ((*te->isa)(te, TM_OPAREN)) { |
| 443 | res = test_oexpr(te, do_eval); |
| 444 | if (te->flags & TEF_ERROR) |
| 445 | return 0; |
| 446 | if (!(*te->isa)(te, TM_CPAREN)) { |
| 447 | (*te->error)(te, 0, "missing closing paren"); |
| 448 | return 0; |
| 449 | } |
| 450 | return res; |
| 451 | } |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | if ((te->flags & TEF_DBRACKET) || (&te->pos.wp[1] < te->wp_end && |
| 457 | !test_isop(te, TM_BINOP, te->pos.wp[1]))) { |
| 458 | if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) { |
| 459 | |
| 460 | opnd1 = (*te->getopnd)(te, op, do_eval); |
| 461 | if (!opnd1) { |
| 462 | (*te->error)(te, -1, "missing argument"); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | return (*te->eval)(te, op, opnd1, NULL, |
| 467 | do_eval); |
| 468 | } |
| 469 | } |
| 470 | opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval); |
| 471 | if (!opnd1) { |
| 472 | (*te->error)(te, 0, "expression expected"); |
| 473 | return 0; |
| 474 | } |
| 475 | if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) { |
| 476 | |
| 477 | opnd2 = (*te->getopnd)(te, op, do_eval); |
| 478 | if (!opnd2) { |
| 479 | (*te->error)(te, -1, "missing second argument"); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | return (*te->eval)(te, op, opnd1, opnd2, do_eval); |
| 484 | } |
| 485 | if (te->flags & TEF_DBRACKET) { |
| 486 | (*te->error)(te, -1, "missing expression operator"); |
| 487 | return 0; |
| 488 | } |
| 489 | return (*te->eval)(te, TO_STNZE, opnd1, NULL, do_eval); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | static int |
| 501 | ptest_isa(Test_env *te, Test_meta meta) |
| 502 | { |
| 503 | |
| 504 | static const char *const tokens[] = { |
| 505 | "-o", "-a", "!", "(", ")" |
| 506 | }; |
| 507 | int ret; |
| 508 | |
| 509 | if (te->pos.wp >= te->wp_end) |
| 510 | return meta == TM_END; |
| 511 | |
| 512 | if (meta == TM_UNOP || meta == TM_BINOP) |
| 513 | ret = (int) test_isop(te, meta, *te->pos.wp); |
| 514 | else if (meta == TM_END) |
| 515 | ret = 0; |
| 516 | else |
| 517 | ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0; |
| 518 | |
| 519 | |
| 520 | if (ret) |
| 521 | te->pos.wp++; |
| 522 | |
| 523 | return ret; |
| 524 | } |
| 525 | |
| 526 | static const char * |
| 527 | ptest_getopnd(Test_env *te, Test_op op, int do_eval) |
| 528 | { |
| 529 | if (te->pos.wp >= te->wp_end) |
| 530 | return op == TO_FILTT ? "1" : NULL; |
| 531 | return *te->pos.wp++; |
| 532 | } |
| 533 | |
| 534 | static int |
| 535 | ptest_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2, |
| 536 | int do_eval) |
| 537 | { |
| 538 | return test_eval(te, op, opnd1, opnd2, do_eval); |
| 539 | } |
| 540 | |
| 541 | static void |
| 542 | ptest_error(Test_env *te, int offset, const char *msg) |
| 543 | { |
| 544 | const char *op = te->pos.wp + offset >= te->wp_end ? |
| 545 | NULL : te->pos.wp[offset]; |
| 546 | |
| 547 | te->flags |= TEF_ERROR; |
| 548 | if (op) |
| 549 | bi_errorf("%s: %s", op, msg); |
| 550 | else |
| 551 | bi_errorf("%s", msg); |
| 552 | } |