clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lam.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/lam/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/lam/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/lam/lam.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 <sys/types.h> |
| 39 | |
| 40 | #include <ctype.h> |
| 41 | #include <err.h> |
| 42 | #include <locale.h> |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <limits.h> |
| 46 | #include <string.h> |
| 47 | #include <unistd.h> |
| 48 | |
| 49 | #define BIGBUFSIZ 5 * BUFSIZ |
| 50 | |
| 51 | struct openfile { |
| 52 | FILE *fp; |
| 53 | int minwidth; |
| 54 | int maxwidth; |
| 55 | short eof; |
| 56 | short pad; |
| 57 | char eol; |
| 58 | char align; |
| 59 | char *sepstring; |
| 60 | } *input; |
| 61 | int inputsize; |
| 62 | |
| 63 | int output; |
| 64 | int nofinalnl; |
| 65 | char line[BIGBUFSIZ]; |
| 66 | char *linep; |
| 67 | |
| 68 | int mbswidth_truncate(char *, int); |
| 69 | |
| 70 | void usage(void); |
| 71 | char *gatherline(struct openfile *); |
| 72 | void getargs(int, char *[]); |
| 73 | char *pad(struct openfile *); |
| 74 | |
| 75 | int |
| 76 | main(int argc, char *argv[]) |
| 77 | { |
| 78 | int i; |
| 79 | |
| 80 | setlocale(LC_CTYPE, ""); |
| 81 | |
| 82 | if (pledge("stdio rpath", NULL) == -1) |
| 1 | Assuming the condition is false | |
|
| |
| 83 | err(1, "pledge"); |
| 84 | |
| 85 | getargs(argc, argv); |
| |
| 86 | if (inputsize == 0) |
| 87 | usage(); |
| 88 | |
| 89 | if (pledge("stdio", NULL) == -1) |
| 90 | err(1, "pledge"); |
| 91 | |
| 92 | |
| 93 | for (;;) { |
| 94 | linep = line; |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | output = 0; |
| 100 | for (i = 0; i < inputsize && input[i].fp != NULL; i++) |
| 101 | linep = gatherline(&input[i]); |
| 102 | if (output == 0) |
| 103 | exit(0); |
| 104 | fputs(line, stdout); |
| 105 | |
| 106 | fputs(input[i].sepstring, stdout); |
| 107 | if (!nofinalnl) |
| 108 | putchar('\n'); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | getargs(int argc, char *argv[]) |
| 114 | { |
| 115 | struct openfile *ip; |
| 116 | const char *errstr; |
| 117 | char *p, *q; |
| 118 | void *tmp; |
| 119 | int ch, P, S, F, T; |
| 120 | |
| 121 | input = calloc(inputsize+1, sizeof *input); |
| 122 | if (input == NULL) |
| 4 | | Assuming 'input' is not equal to NULL | |
|
| |
| 123 | errx(1, "too many files"); |
| 124 | ip = &input[inputsize]; |
| 125 | |
| 126 | P = S = F = T = 0; |
| 127 | while (optind < argc) { |
| 6 | | Assuming 'optind' is < 'argc' | |
|
| 7 | | Loop condition is true. Entering loop body | |
|
| 10 | | Loop condition is true. Entering loop body | |
|
| 27 | | Assuming 'optind' is < 'argc' | |
|
| 28 | | Loop condition is true. Entering loop body | |
|
| 128 | switch (ch = getopt(argc, argv, "F:f:P:p:S:s:T:t:")) { |
| 8 | | Control jumps to 'case 83:' at line 159 | |
|
| 11 | | Control jumps to 'case -1:' at line 170 | |
|
| 29 | | Control jumps to 'case 102:' at line 133 | |
|
| 129 | case 'P': case 'p': |
| 130 | P = (ch == 'P'); |
| 131 | ip->pad = 1; |
| 132 | |
| 133 | case 'F': case 'f': |
| 134 | F = (ch == 'F'); |
| 135 | |
| 136 | p = optarg; |
| 30 | | Null pointer value stored to 'p' | |
|
| 137 | if (*p == '0' || *p == '-') |
| 31 | | Dereference of null pointer (loaded from variable 'p') |
|
| 138 | ip->align = *p++; |
| 139 | else |
| 140 | ip->align = ' '; |
| 141 | if ((q = strchr(p, '.')) != NULL) |
| 142 | *q++ = '\0'; |
| 143 | if (*p != '\0') { |
| 144 | ip->minwidth = strtonum(p, 1, INT_MAX, |
| 145 | &errstr); |
| 146 | if (errstr != NULL) |
| 147 | errx(1, "minimum width is %s: %s", |
| 148 | errstr, p); |
| 149 | } |
| 150 | if (q != NULL) { |
| 151 | ip->maxwidth = strtonum(q, 1, INT_MAX, |
| 152 | &errstr); |
| 153 | if (errstr != NULL) |
| 154 | errx(1, "maximum width is %s: %s", |
| 155 | errstr, q); |
| 156 | } else |
| 157 | ip->maxwidth = INT_MAX; |
| 158 | break; |
| 159 | case 'S': case 's': |
| 160 | S = (ch == 'S'); |
| 161 | ip->sepstring = optarg; |
| 162 | break; |
| 9 | | Execution continues on line 127 | |
|
| 163 | case 'T': case 't': |
| 164 | T = (ch == 'T'); |
| 165 | if (strlen(optarg) != 1) |
| 166 | usage(); |
| 167 | ip->eol = optarg[0]; |
| 168 | nofinalnl = 1; |
| 169 | break; |
| 170 | case -1: |
| 171 | if (optind >= argc) |
| |
| 172 | break; |
| 173 | |
| 174 | if (strcmp(argv[optind], "-") == 0) |
| 13 | | Assuming the condition is false | |
|
| |
| 175 | ip->fp = stdin; |
| 176 | else if ((ip->fp = fopen(argv[optind], "r")) == NULL) |
| 15 | | Assuming the condition is false | |
|
| |
| 177 | err(1, "%s", argv[optind]); |
| 178 | ip->pad = P; |
| 179 | if (ip->sepstring == NULL) |
| 17 | | Assuming field 'sepstring' is equal to NULL | |
|
| |
| 180 | ip->sepstring = S ? (ip-1)->sepstring : ""; |
| |
| 181 | if (ip->eol == '\0') |
| 20 | | Assuming the condition is false | |
|
| |
| 182 | ip->eol = T ? (ip-1)->eol : '\n'; |
| 183 | if (ip->align == '\0') { |
| 22 | | Assuming the condition is false | |
|
| |
| 184 | if (F || P) { |
| 185 | ip->align = (ip-1)->align; |
| 186 | ip->minwidth = (ip-1)->minwidth; |
| 187 | ip->maxwidth = (ip-1)->maxwidth; |
| 188 | } else |
| 189 | ip->maxwidth = INT_MAX; |
| 190 | } |
| 191 | |
| 192 | ++inputsize; |
| 193 | |
| 194 | |
| 195 | tmp = recallocarray(input, inputsize, |
| 196 | inputsize+1, sizeof *input); |
| 197 | if (tmp == NULL) |
| 24 | | Assuming 'tmp' is not equal to NULL | |
|
| |
| 198 | errx(1, "too many files"); |
| 199 | input = tmp; |
| 200 | ip = &input[inputsize]; |
| 201 | optind++; |
| 202 | break; |
| 26 | | Execution continues on line 127 | |
|
| 203 | default: |
| 204 | usage(); |
| 205 | |
| 206 | } |
| 207 | } |
| 208 | ip->fp = NULL; |
| 209 | if (ip->sepstring == NULL) |
| 210 | ip->sepstring = ""; |
| 211 | } |
| 212 | |
| 213 | char * |
| 214 | pad(struct openfile *ip) |
| 215 | { |
| 216 | size_t n; |
| 217 | char *lp = linep; |
| 218 | int i = 0; |
| 219 | |
| 220 | n = strlcpy(lp, ip->sepstring, line + sizeof(line) - lp); |
| 221 | lp += (n < line + sizeof(line) - lp) ? n : strlen(lp); |
| 222 | if (ip->pad) |
| 223 | while (i++ < ip->minwidth && lp + 1 < line + sizeof(line)) |
| 224 | *lp++ = ' '; |
| 225 | *lp = '\0'; |
| 226 | return (lp); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | char * |
| 234 | gatherline(struct openfile *ip) |
| 235 | { |
| 236 | size_t n; |
| 237 | char s[BUFSIZ]; |
| 238 | char *p; |
| 239 | char *lp = linep; |
| 240 | char *end = s + BUFSIZ - 1; |
| 241 | int c, width; |
| 242 | |
| 243 | if (ip->eof) |
| 244 | return (pad(ip)); |
| 245 | for (p = s; (c = fgetc(ip->fp)) != EOF && p < end; p++) |
| 246 | if ((*p = c) == ip->eol) |
| 247 | break; |
| 248 | *p = '\0'; |
| 249 | if (c == EOF) { |
| 250 | ip->eof = 1; |
| 251 | if (ip->fp == stdin) |
| 252 | fclose(stdin); |
| 253 | return (pad(ip)); |
| 254 | } |
| 255 | |
| 256 | output++; |
| 257 | n = strlcpy(lp, ip->sepstring, line + sizeof(line) - lp); |
| 258 | lp += (n < line + sizeof(line) - lp) ? n : strlen(lp); |
| 259 | width = mbswidth_truncate(s, ip->maxwidth); |
| 260 | if (ip->align != '-') |
| 261 | while (width++ < ip->minwidth && lp + 1 < line + sizeof(line)) |
| 262 | *lp++ = ip->align; |
| 263 | n = strlcpy(lp, s, line + sizeof(line) - lp); |
| 264 | lp += (n < line + sizeof(line) - lp) ? n : strlen(lp); |
| 265 | if (ip->align == '-') |
| 266 | while (width++ < ip->minwidth && lp + 1 < line + sizeof(line)) |
| 267 | *lp++ = ' '; |
| 268 | *lp = '\0'; |
| 269 | return (lp); |
| 270 | } |
| 271 | |
| 272 | void |
| 273 | usage(void) |
| 274 | { |
| 275 | extern char *__progname; |
| 276 | |
| 277 | fprintf(stderr, |
| 278 | "usage: %s [-F|f min.max] [-P|p min.max] [-S|s sepstring] [-T|t c] file ...\n", |
| 279 | __progname); |
| 280 | exit(1); |
| 281 | } |