clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name arithmetic.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/games/arithmetic/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/games/arithmetic/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/games/arithmetic/arithmetic.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 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | #include <err.h> |
| 64 | #include <ctype.h> |
| 65 | #include <limits.h> |
| 66 | #include <signal.h> |
| 67 | #include <stdio.h> |
| 68 | #include <stdlib.h> |
| 69 | #include <string.h> |
| 70 | #include <time.h> |
| 71 | #include <unistd.h> |
| 72 | |
| 73 | int getrandom(uint32_t, int, int); |
| 74 | __dead void intr(int); |
| 75 | int opnum(int); |
| 76 | void penalise(int, int, int); |
| 77 | int problem(void); |
| 78 | void showstats(void); |
| 79 | __dead void usage(void); |
| 80 | |
| 81 | const char keylist[] = "+-x/"; |
| 82 | const char defaultkeys[] = "+-"; |
| 83 | const char *keys = defaultkeys; |
| 84 | int nkeys = sizeof(defaultkeys) - 1; |
| 85 | uint32_t rangemax = 10; |
| 86 | int nright, nwrong; |
| 87 | time_t qtime; |
| 88 | #define NQUESTS 20 |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | int |
| 98 | main(int argc, char *argv[]) |
| 99 | { |
| 100 | int ch, cnt; |
| 101 | const char *errstr; |
| 102 | |
| 103 | if (pledge("stdio", NULL) == -1) |
| 1 | Assuming the condition is false | |
|
| |
| 104 | err(1, "pledge"); |
| 105 | |
| 106 | while ((ch = getopt(argc, argv, "hr:o:")) != -1) |
| 3 | | Assuming the condition is false | |
|
| 4 | | Loop condition is false. Execution continues on line 126 | |
|
| 107 | switch(ch) { |
| 108 | case 'o': { |
| 109 | const char *p; |
| 110 | |
| 111 | for (p = keys = optarg; *p; ++p) |
| 112 | if (!strchr(keylist, *p)) |
| 113 | errx(1, "unknown key."); |
| 114 | nkeys = p - optarg; |
| 115 | break; |
| 116 | } |
| 117 | case 'r': |
| 118 | rangemax = strtonum(optarg, 1, (1ULL<<31)-1, &errstr); |
| 119 | if (errstr) |
| 120 | errx(1, "invalid range, %s: %s", errstr, optarg); |
| 121 | break; |
| 122 | case 'h': |
| 123 | default: |
| 124 | usage(); |
| 125 | } |
| 126 | if (argc -= optind) |
| 5 | | Assuming the condition is false | |
|
| |
| 127 | usage(); |
| 128 | |
| 129 | (void)signal(SIGINT, intr); |
| 130 | |
| 131 | |
| 132 | for (;;) { |
| 7 | | Loop condition is true. Entering loop body | |
|
| 133 | for (cnt = NQUESTS; cnt--;) |
| 8 | | Loop condition is true. Entering loop body | |
|
| 134 | if (problem() == EOF) |
| |
| 135 | intr(0); |
| 136 | showstats(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void |
| 142 | intr(int dummy) |
| 143 | { |
| 144 | showstats(); |
| 145 | _exit(0); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void |
| 150 | showstats(void) |
| 151 | { |
| 152 | if (nright + nwrong > 0) { |
| 153 | (void)printf("\n\nRights %d; Wrongs %d; Score %d%%", |
| 154 | nright, nwrong, (int)(100L * nright / (nright + nwrong))); |
| 155 | if (nright > 0) |
| 156 | (void)printf("\nTotal time %ld seconds; %.1f seconds per problem\n\n", |
| 157 | (long)qtime, (float)qtime / nright); |
| 158 | } |
| 159 | (void)printf("\n"); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | int |
| 171 | problem(void) |
| 172 | { |
| 173 | char *p; |
| 174 | time_t start, finish; |
| 175 | int left, op, right, result; |
| 10 | | 'result' declared without an initial value | |
|
| 176 | char line[80]; |
| 177 | |
| 178 | op = keys[arc4random_uniform(nkeys)]; |
| 179 | if (op != '/') |
| 11 | | Assuming the condition is true | |
|
| |
| 180 | right = getrandom(rangemax + 1, op, 1); |
| 181 | retry: |
| 182 | |
| 183 | switch (op) { |
| 13 | | 'Default' branch taken. Execution continues on line 207 | |
|
| 184 | case '+': |
| 185 | left = getrandom(rangemax + 1, op, 0); |
| 186 | result = left + right; |
| 187 | break; |
| 188 | case '-': |
| 189 | result = getrandom(rangemax + 1, op, 0); |
| 190 | left = right + result; |
| 191 | break; |
| 192 | case 'x': |
| 193 | left = getrandom(rangemax + 1, op, 0); |
| 194 | result = left * right; |
| 195 | break; |
| 196 | case '/': |
| 197 | right = getrandom(rangemax, op, 1) + 1; |
| 198 | result = getrandom(rangemax + 1, op, 0); |
| 199 | left = right * result + arc4random_uniform(right); |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | if (result < 0 || left < 0) |
| 14 | | The left operand of '<' is a garbage value |
|
| 208 | goto retry; |
| 209 | |
| 210 | (void)printf("%d %c %d = ", left, op, right); |
| 211 | (void)fflush(stdout); |
| 212 | (void)time(&start); |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | for (;;) { |
| 219 | if (!fgets(line, sizeof(line), stdin)) { |
| 220 | (void)printf("\n"); |
| 221 | return(EOF); |
| 222 | } |
| 223 | for (p = line; isspace((unsigned char)*p); ++p); |
| 224 | if (!isdigit((unsigned char)*p)) { |
| 225 | (void)printf("Please type a number.\n"); |
| 226 | continue; |
| 227 | } |
| 228 | if (atoi(p) == result) { |
| 229 | (void)printf("Right!\n"); |
| 230 | ++nright; |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | (void)printf("What?\n"); |
| 235 | ++nwrong; |
| 236 | penalise(right, op, 1); |
| 237 | if (op == 'x' || op == '+') |
| 238 | penalise(left, op, 0); |
| 239 | else |
| 240 | penalise(result, op, 0); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | (void)time(&finish); |
| 251 | qtime += finish - start; |
| 252 | return(0); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | uint32_t penalty[sizeof(keylist) - 1][2]; |
| 270 | struct penalty { |
| 271 | int value; |
| 272 | uint32_t penalty; |
| 273 | struct penalty *next; |
| 274 | } *penlist[sizeof(keylist) - 1][2]; |
| 275 | |
| 276 | #define WRONGPENALTY 5 /* Perhaps this should depend on maxrange. */ |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | void |
| 284 | penalise(int value, int op, int operand) |
| 285 | { |
| 286 | struct penalty *p; |
| 287 | |
| 288 | op = opnum(op); |
| 289 | if ((p = malloc(sizeof(*p))) == NULL) |
| 290 | return; |
| 291 | p->next = penlist[op][operand]; |
| 292 | penlist[op][operand] = p; |
| 293 | penalty[op][operand] += p->penalty = WRONGPENALTY; |
| 294 | p->value = value; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | int |
| 304 | getrandom(uint32_t maxval, int op, int operand) |
| 305 | { |
| 306 | uint32_t value; |
| 307 | struct penalty **pp, *p; |
| 308 | |
| 309 | op = opnum(op); |
| 310 | value = arc4random_uniform(maxval + penalty[op][operand]); |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | if (value < maxval) |
| 317 | return((int)value); |
| 318 | value -= maxval; |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | for (pp = &penlist[op][operand]; (p = *pp) != NULL; pp = &p->next) { |
| 325 | if (p->penalty > value) { |
| 326 | value = p->value; |
| 327 | penalty[op][operand]--; |
| 328 | if (--(p->penalty) <= 0) { |
| 329 | p = p->next; |
| 330 | (void)free((char *)*pp); |
| 331 | *pp = p; |
| 332 | } |
| 333 | return(value); |
| 334 | } |
| 335 | value -= p->penalty; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | errx(1, "bug: inconsistent penalties."); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | int |
| 347 | opnum(int op) |
| 348 | { |
| 349 | char *p; |
| 350 | |
| 351 | if (op == 0 || (p = strchr(keylist, op)) == NULL) |
| 352 | errx(1, "bug: op %c not in keylist %s.", op, keylist); |
| 353 | return(p - keylist); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | void |
| 358 | usage(void) |
| 359 | { |
| 360 | extern char *__progname; |
| 361 | (void)fprintf(stderr, "usage: %s [-o +-x/] [-r range]\n", __progname); |
| 362 | exit(1); |
| 363 | } |