clang -cc1 -cc1 -triple x86_64-redhat-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name cavp_parser.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 2 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/build/quick-libreswan-2/programs/cavp -resource-dir /usr/lib64/clang/13.0.0 -D TimeZoneOffset=timezone -D PIE -D NSS_IPSEC_PROFILE -D XFRM_LIFETIME_DEFAULT=30 -D USE_IKEv1 -D XFRM_SUPPORT -D USE_XFRM_INTERFACE -D USE_DNSSEC -D DEFAULT_DNSSEC_ROOTKEY_FILE="/var/lib/unbound/root.key" -D HAVE_LABELED_IPSEC -D HAVE_SECCOMP -D LIBCURL -D USE_LINUX_AUDIT -D HAVE_NM -D USE_PAM_AUTH -D USE_3DES -D USE_AES -D USE_CAMELLIA -D USE_CHACHA -D USE_DH31 -D USE_MD5 -D USE_SHA1 -D USE_SHA2 -D USE_PRF_AES_XCBC -D USE_NSS_KDF -D DEFAULT_RUNDIR="/run/pluto" -D IPSEC_CONF="/etc/ipsec.conf" -D IPSEC_CONFDDIR="/etc/ipsec.d" -D IPSEC_NSSDIR="/var/lib/ipsec/nss" -D IPSEC_CONFDIR="/etc" -D IPSEC_EXECDIR="/usr/local/libexec/ipsec" -D IPSEC_SBINDIR="/usr/local/sbin" -D IPSEC_VARDIR="/var" -D POLICYGROUPSDIR="/etc/ipsec.d/policies" -D IPSEC_SECRETS_FILE="/etc/ipsec.secrets" -D FORCE_PR_ASSERT -D USE_FORK=1 -D USE_VFORK=0 -D USE_DAEMON=0 -D USE_PTHREAD_SETSCHEDPRIO=1 -D GCC_LINT -D HAVE_LIBCAP_NG -D USE_IKEv1 -I . -I ../../OBJ.linux.x86_64/programs/cavp -I ../../include -I /usr/include/nss3 -I /usr/include/nspr4 -D HERE_FILENAME="programs/cavp/cavp_parser.c" -internal-isystem /usr/lib64/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/11/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=gnu99 -fdebug-compilation-dir=/home/build/quick-libreswan-2/programs/cavp -ferror-limit 19 -stack-protector 3 -fgnuc-version=4.2.1 -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-01-205714-1273399-1 -x c /home/build/quick-libreswan-2/programs/cavp/cavp_parser.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <regex.h> |
| 21 | |
| 22 | #include "lswfips.h" |
| 23 | #include "lswnss.h" |
| 24 | |
| 25 | #include "cavp.h" |
| 26 | #include "cavps.h" |
| 27 | #include "cavp_entry.h" |
| 28 | #include "cavp_print.h" |
| 29 | #include "cavp_parser.h" |
| 30 | |
| 31 | enum what { HEADER, BODY, BLANK, CONFIG, DATA, IDLE, END } state = HEADER; |
| 32 | |
| 33 | const char *const whats[] = { |
| 34 | "HEADER", "BODY", "BLANK", "CONFIG", "DATA", "IDLE", "END", |
| 35 | }; |
| 36 | |
| 37 | static void error_state(enum what state, enum what what, |
| 38 | const char *message) |
| 39 | { |
| 40 | fprintf(stderr, "\nbad state transition from %s(%d) to %s(%d)\n%s\n", |
| 41 | whats[state], state, whats[what], what, message); |
| 42 | exit(1); |
| 43 | } |
| 44 | |
| 45 | static void next_state(const struct cavp *cavp, enum what what, struct logger *logger) |
| 46 | { |
| 47 | switch (state) { |
| 48 | case HEADER: |
| 49 | switch (what) { |
| 50 | case BODY: |
| 51 | state = what; |
| 52 | break; |
| 53 | default: |
| 54 | error_state(state, what, |
| 55 | "expecting header containing file type"); |
| 56 | } |
| 57 | break; |
| 58 | case BODY: |
| 59 | switch (what) { |
| 60 | case CONFIG: |
| 61 | state = CONFIG; |
| 62 | break; |
| 63 | case BLANK: |
| 64 | break; |
| 65 | default: |
| 66 | error_state(state, what, "expecting config section"); |
| 67 | } |
| 68 | break; |
| 69 | case CONFIG: |
| 70 | switch (what) { |
| 71 | case CONFIG: |
| 72 | break; |
| 73 | case BLANK: |
| 74 | cavp->print_config(); |
| 75 | state = DATA; |
| 76 | break; |
| 77 | default: |
| 78 | error_state(state, what, "expecting data section"); |
| 79 | } |
| 80 | break; |
| 81 | case DATA: |
| 82 | switch (what) { |
| 83 | case DATA: |
| 84 | break; |
| 85 | case BLANK: |
| 86 | cavp->run_test(logger); |
| 87 | state = IDLE; |
| 88 | break; |
| 89 | case END: |
| 90 | cavp->run_test(logger); |
| 91 | state = END; |
| 92 | break; |
| 93 | default: |
| 94 | error_state(state, what, "expecting EOF or CONFIG section"); |
| 95 | } |
| 96 | break; |
| 97 | case IDLE: |
| 98 | switch (what) { |
| 99 | case CONFIG: |
| 100 | state = CONFIG; |
| 101 | break; |
| 102 | case DATA: |
| 103 | state = DATA; |
| 104 | break; |
| 105 | case END: |
| 106 | state = END; |
| 107 | break; |
| 108 | case BLANK: |
| 109 | break; |
| 110 | default: |
| 111 | error_state(state, what, "expecting config section"); |
| 112 | } |
| 113 | break; |
| 114 | default: |
| 115 | error_state(state, what, "expecting the unexpected"); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | struct fields { |
| 121 | char *key; |
| 122 | char *value; |
| 123 | }; |
| 124 | |
| 125 | static struct fields parse_fields(char *line) |
| 126 | { |
| 127 | struct fields fields = { |
| 128 | .key = line, |
| 129 | }; |
| 130 | char *eq = strchr(line, '='); |
| 131 | if (eq != NULL) { |
| 132 | char *ke = eq; |
| 133 | while (ke > fields.key && ke[-1] == ' ') { |
| 134 | ke--; |
| 135 | } |
| 136 | *ke = '\0'; |
| 137 | } |
| 138 | if (eq == NULL) { |
| 139 | fields.value = NULL; |
| 140 | } else { |
| 141 | fields.value = eq + 1; |
| 142 | while (*fields.value == ' ') { |
| 143 | fields.value++; |
| 144 | } |
| 145 | } |
| 146 | return fields; |
| 147 | } |
| 148 | |
| 149 | void cavp_parser(const struct cavp *cavp, struct logger *logger) |
| 150 | { |
| 151 | |
| 152 | char line[65536] = ""; |
| 153 | int line_nr = 0; |
| 154 | |
| 155 | if (cavp != NULL) { |
| 1 | Assuming 'cavp' is equal to NULL | |
|
| |
| 156 | next_state(cavp, BODY, logger); |
| 157 | } |
| 158 | |
| 159 | for (;;) { |
| 3 | | Loop condition is true. Entering loop body | |
|
| 160 | line_nr++; |
| 161 | if (fgets(line, sizeof(line), stdin) == NULL) { |
| 4 | | Assuming the condition is false | |
|
| |
| 162 | int error = ferror(stdin); |
| 163 | if (error != 0) { |
| 164 | fprintf(stderr, "unexpected error at line %d: %s(%d)\n", |
| 165 | line_nr, strerror(error), error); |
| 166 | exit(1); |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | if (strlen(line) >= sizeof(line) - 1) { |
| 6 | | Assuming the condition is false | |
|
| |
| 171 | fprintf(stderr, "line %d exceeded buffer length of %zu: %s\n", |
| 172 | line_nr, sizeof(line), line); |
| 173 | exit(1); |
| 174 | } |
| 175 | |
| 176 | int last = strlen(line) - 1; |
| 177 | while (last >= 0 && strchr("\r\n", line[last]) != NULL) { |
| 8 | | Assuming the condition is false | |
|
| 9 | | Loop condition is false. Execution continues on line 180 | |
|
| 178 | last--; |
| 179 | } |
| 180 | line[last + 1] = '\0'; |
| 181 | |
| 182 | if (line[0] == '\0') { |
| 10 | | Assuming the condition is false | |
|
| |
| 183 | next_state(cavp, BLANK, logger); |
| 184 | |
| 185 | print_line(line); |
| 186 | } else if (line[0] == '#') { |
| 12 | | Assuming the condition is false | |
|
| |
| 187 | |
| 188 | if (cavp == NULL) { |
| 189 | for (const struct cavp **cavpp = cavps; |
| 190 | cavp == NULL && *cavpp != NULL; |
| 191 | cavpp++) { |
| 192 | for (const char *const *match = (*cavpp)->match; |
| 193 | cavp == NULL && *match != NULL; |
| 194 | match++) { |
| 195 | regex_t regex; |
| 196 | if (regcomp(®ex, *match, REG_EXTENDED)) { |
| 197 | fprintf(stderr, "bad regex %s\n", *match); |
| 198 | exit(1); |
| 199 | } |
| 200 | if (regexec(®ex, line, 0, NULL, 0) == 0) { |
| 201 | cavp = *cavpp; |
| 202 | fprintf(stderr, "\ntest: %s (header matched '%s')\n\n", |
| 203 | cavp->description, *match); |
| 204 | next_state(cavp, BODY, logger); |
| 205 | } |
| 206 | regfree(®ex); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | print_line(line); |
| 211 | } else if (line[0] == '[') { |
| 14 | | Assuming the condition is false | |
|
| |
| 212 | next_state(cavp, CONFIG, logger); |
| 213 | |
| 214 | char *rparen = strchr(line, ']'); |
| 215 | *rparen = '\0'; |
| 216 | struct fields fields = parse_fields(line + 1); |
| 217 | const struct cavp_entry *entry = cavp_entry_by_key(cavp->config, fields.key); |
| 218 | if (entry == NULL) { |
| 219 | fprintf(stderr, "unknown config entry: ['%s' = '%s']\n", |
| 220 | fields.key, fields.value); |
| 221 | exit(1); |
| 222 | } else if (entry->op == NULL) { |
| 223 | fprintf(stderr, "ignoring config entry: ['%s' = '%s']\n", |
| 224 | fields.key, fields.value); |
| 225 | } else { |
| 226 | entry->op(entry, fields.value, logger); |
| 227 | } |
| 228 | } else { |
| 229 | next_state(cavp, DATA, logger); |
| 230 | struct fields fields = parse_fields(line); |
| 231 | const struct cavp_entry *entry = cavp_entry_by_key(cavp->data, fields.key); |
| 16 | | Access to field 'data' results in a dereference of a null pointer (loaded from variable 'cavp') |
|
| 232 | if (entry == NULL) { |
| 233 | fprintf(stderr, "unknown data entry: '%s' = '%s'\n", |
| 234 | fields.key, fields.value); |
| 235 | exit(1); |
| 236 | } else if (entry->op == NULL) { |
| 237 | fprintf(stderr, "ignoring data entry: '%s' = '%s'\n", |
| 238 | fields.key, fields.value); |
| 239 | } else { |
| 240 | entry->op(entry, fields.value, logger); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | next_state(cavp, END, logger); |
| 245 | } |