clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name flags.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 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/tmp/build/foma/foma-0.10.0+g279~a2d32b38 -resource-dir /usr/lib/llvm-16/lib/clang/16 -D _GNU_SOURCE -I /tmp/build/foma/foma-0.10.0+g279~a2d32b38 -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-missing-field-initializers -Wno-deprecated -Wno-unused-parameter -std=c18 -fdebug-compilation-dir=/tmp/build/foma/foma-0.10.0+g279~a2d32b38 -ferror-limit 19 -fvisibility=hidden -fgnuc-version=4.2.1 -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/build/foma/scan-build/2024-09-11-155945-2678-1 -x c /tmp/build/foma/foma-0.10.0+g279~a2d32b38/flags.c
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | #include <stdio.h> |
19 | #include <stdlib.h> |
20 | #include <string.h> |
21 | #include <locale.h> |
22 | #include "foma.h" |
23 | |
24 | extern int g_verbose; |
25 | |
26 | #define FAIL 1 |
27 | #define SUCCEED 2 |
28 | #define NONE 3 |
29 | |
30 | static struct flags *flag_extract (struct fsm *net); |
31 | static char *flag_type_to_char (int type); |
32 | static void flag_purge (struct fsm *net, char *name); |
33 | static struct fsm *flag_create_symbol(int type, char *name, char *value); |
34 | |
35 | struct flags { |
36 | int type; |
37 | char *name; |
38 | char *value; |
39 | struct flags *next; |
40 | }; |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | struct fsm *flag_eliminate(struct fsm *net, char *name) { |
67 | |
68 | struct flags *flags, *f, *ff; |
69 | struct fsm *filter, *succeed_flags, *fail_flags, *self, *newfilter, *newnet; |
70 | int flag, fstatus, found; |
71 | |
72 | filter = NULL; |
73 | |
74 | if (net->pathcount == 0) { |
75 | if (g_verbose) |
76 | { |
77 | fprintf(stderr,"Skipping flag elimination since there are no paths in network.\n"); |
78 | fflush(stderr); |
79 | } |
80 | return(net); |
81 | } |
82 | |
83 | flags = flag_extract(net); |
84 | |
85 | if (name != NULL) { |
86 | for (found = 0, f = flags; f != NULL; f = f->next) { |
87 | if (strcmp(name,f->name) == 0) |
88 | found = 1; |
89 | } |
90 | if (found == 0) { |
91 | if (g_verbose) |
92 | { |
93 | fprintf(stderr,"Flag attribute '%s' does not occur in the network.\n",name); |
94 | fflush(stderr); |
95 | } |
96 | return(net); |
97 | } |
98 | } |
99 | |
100 | flag = 0; |
101 | |
102 | for (f = flags; f != NULL; f = f->next) { |
103 | |
104 | if ((name == NULL || strcmp(f->name,name) == 0) && |
105 | (f->type | FLAG_UNIFY | FLAG_REQUIRE | FLAG_DISALLOW | FLAG_EQUAL)) { |
106 | |
107 | succeed_flags = fsm_empty_set(); |
108 | fail_flags = fsm_empty_set(); |
109 | self = flag_create_symbol(f->type, f->name, f->value); |
110 | |
111 | for (ff = flags, flag = 0; ff != NULL; ff = ff->next) { |
112 | fstatus = flag_build(f->type, f->name, f->value, ff->type, ff->name, ff->value); |
113 | if (fstatus == FAIL) { |
114 | fail_flags = fsm_minimize(fsm_union(fail_flags, flag_create_symbol(ff->type, ff->name, ff->value))); |
115 | flag = 1; |
116 | } |
117 | if (fstatus == SUCCEED) { |
118 | succeed_flags = fsm_minimize(fsm_union(succeed_flags, flag_create_symbol(ff->type, ff->name, ff->value))); |
119 | flag = 1; |
120 | } |
121 | } |
122 | } |
123 | |
124 | if (flag) { |
125 | if (f->type == FLAG_REQUIRE) { |
126 | newfilter = fsm_complement(fsm_concat(fsm_optionality(fsm_concat(fsm_universal(), fail_flags)), fsm_concat(fsm_complement(fsm_contains(succeed_flags)), fsm_concat(self, fsm_universal())))); |
127 | |
128 | } else { |
129 | newfilter = fsm_complement(fsm_contains(fsm_concat(fail_flags,fsm_concat(fsm_complement(fsm_contains(succeed_flags)),self)))); |
130 | } |
131 | |
132 | filter = (filter == NULL) ? newfilter : fsm_intersect(filter, newfilter); |
133 | } |
134 | flag = 0; |
135 | } |
136 | if (filter != NULL) { |
137 | extern int g_flag_is_epsilon; |
138 | int old_g_flag_is_epsilon; |
139 | old_g_flag_is_epsilon = g_flag_is_epsilon; |
140 | g_flag_is_epsilon = 0; |
141 | newnet = fsm_compose(fsm_copy(filter),fsm_compose(net,fsm_copy(filter))); |
142 | g_flag_is_epsilon = old_g_flag_is_epsilon; |
143 | } else { |
144 | newnet = net; |
145 | } |
146 | flag_purge(newnet, name); |
147 | newnet = fsm_minimize(newnet); |
148 | sigma_cleanup(newnet,0); |
149 | sigma_sort(newnet); |
150 | free(flags); |
151 | return(fsm_topsort(newnet)); |
152 | } |
153 | |
154 | struct fsm *flag_create_symbol(int type, char *name, char *value) { |
155 | char *string; |
156 | if (value == NULL) |
157 | value = ""; |
158 | |
159 | string = malloc(sizeof(char)*strlen(name)+strlen(value)+6); |
160 | *string = '\0'; |
161 | strcat(string, "@"); |
162 | strcat(string, flag_type_to_char(type)); |
163 | strcat(string, "."); |
164 | strcat(string, name); |
165 | if (strcmp(value,"") != 0) { |
166 | strcat(string, "."); |
167 | strcat(string, value); |
168 | } |
169 | strcat(string, "@"); |
170 | |
171 | return(fsm_symbol(string)); |
172 | |
173 | } |
174 | |
175 | char *flag_type_to_char (int type) { |
176 | switch(type) { |
177 | case FLAG_UNIFY: |
178 | return("U"); |
179 | case FLAG_CLEAR: |
180 | return("C"); |
181 | case FLAG_DISALLOW: |
182 | return("D"); |
183 | case FLAG_NEGATIVE: |
184 | return("N"); |
185 | case FLAG_POSITIVE: |
186 | return("P"); |
187 | case FLAG_REQUIRE: |
188 | return("R"); |
189 | case FLAG_EQUAL: |
190 | return("E"); |
191 | } |
192 | return NULL; |
193 | } |
194 | |
195 | int flag_build(int ftype, char *fname, char *fvalue, int fftype, char *ffname, char *ffvalue) { |
196 | int valeq, selfnull; |
197 | |
198 | selfnull = 0; |
199 | if (strcmp(fname,ffname) != 0) |
200 | return NONE; |
201 | |
202 | if (fvalue == NULL) { |
203 | fvalue = ""; |
204 | selfnull = 1; |
205 | } |
206 | |
207 | if (ffvalue == NULL) |
208 | ffvalue = ""; |
209 | |
210 | valeq = strcmp(fvalue, ffvalue); |
211 | |
212 | if (ftype == FLAG_UNIFY && fftype == FLAG_POSITIVE && valeq == 0) |
213 | return SUCCEED; |
214 | if (ftype == FLAG_UNIFY && fftype == FLAG_CLEAR) |
215 | return SUCCEED; |
216 | if (ftype == FLAG_UNIFY && fftype == FLAG_UNIFY && valeq != 0) |
217 | return FAIL; |
218 | if (ftype == FLAG_UNIFY && fftype == FLAG_POSITIVE && valeq != 0) |
219 | return FAIL; |
220 | if (ftype == FLAG_UNIFY && fftype == FLAG_NEGATIVE && valeq == 0) |
221 | return FAIL; |
222 | |
223 | |
224 | if (ftype == FLAG_REQUIRE && fftype == FLAG_UNIFY && selfnull) |
225 | return SUCCEED; |
226 | if (ftype == FLAG_REQUIRE && fftype == FLAG_POSITIVE && selfnull) |
227 | return SUCCEED; |
228 | if (ftype == FLAG_REQUIRE && fftype == FLAG_NEGATIVE && selfnull) |
229 | return SUCCEED; |
230 | if (ftype == FLAG_REQUIRE && fftype == FLAG_CLEAR && selfnull) |
231 | return FAIL; |
232 | |
233 | |
234 | if (ftype == FLAG_REQUIRE && fftype == FLAG_POSITIVE && valeq == 0 && !selfnull) |
235 | return SUCCEED; |
236 | if (ftype == FLAG_REQUIRE && fftype == FLAG_UNIFY && valeq == 0 && !selfnull) |
237 | return SUCCEED; |
238 | if (ftype == FLAG_REQUIRE && fftype == FLAG_POSITIVE && valeq != 0 && !selfnull) |
239 | return FAIL; |
240 | if (ftype == FLAG_REQUIRE && fftype == FLAG_UNIFY && valeq != 0 && !selfnull) |
241 | return FAIL; |
242 | if (ftype == FLAG_REQUIRE && fftype == FLAG_NEGATIVE && !selfnull) |
243 | return FAIL; |
244 | if (ftype == FLAG_REQUIRE && fftype == FLAG_CLEAR && !selfnull) |
245 | return FAIL; |
246 | |
247 | |
248 | if (ftype == FLAG_DISALLOW && fftype == FLAG_CLEAR && selfnull) |
249 | return SUCCEED; |
250 | if (ftype == FLAG_DISALLOW && fftype == FLAG_POSITIVE && selfnull) |
251 | return FAIL; |
252 | if (ftype == FLAG_DISALLOW && fftype == FLAG_UNIFY && selfnull) |
253 | return FAIL; |
254 | if (ftype == FLAG_DISALLOW && fftype == FLAG_NEGATIVE && selfnull) |
255 | return FAIL; |
256 | |
257 | |
258 | if (ftype == FLAG_DISALLOW && fftype == FLAG_POSITIVE && valeq != 0 && !selfnull) |
259 | return SUCCEED; |
260 | if (ftype == FLAG_DISALLOW && fftype == FLAG_CLEAR && !selfnull) |
261 | return SUCCEED; |
262 | if (ftype == FLAG_DISALLOW && fftype == FLAG_NEGATIVE && valeq == 0 && !selfnull) |
263 | return SUCCEED; |
264 | if (ftype == FLAG_DISALLOW && fftype == FLAG_POSITIVE && valeq == 0 && !selfnull) |
265 | return FAIL; |
266 | if (ftype == FLAG_DISALLOW && fftype == FLAG_UNIFY && valeq == 0 && !selfnull) |
267 | return FAIL; |
268 | if (ftype == FLAG_DISALLOW && fftype == FLAG_NEGATIVE && valeq != 0 && !selfnull) |
269 | return FAIL; |
270 | |
271 | return NONE; |
272 | } |
273 | |
274 | |
275 | |
276 | |
277 | void flag_purge (struct fsm *net, char *name) { |
278 | struct fsm_state *fsm; |
279 | struct sigma *sigma; |
280 | int i, *ftable, sigmasize; |
281 | char *csym; |
282 | sigmasize = sigma_max(net->sigma)+1; |
283 | ftable = malloc(sizeof(int) * sigmasize); |
284 | fsm = net->states; |
285 | for (i=0; i<sigmasize; i++) |
286 | *(ftable+i)=0; |
287 | |
288 | for (sigma = net->sigma; sigma != NULL && sigma->number != -1; sigma = sigma->next) { |
289 | |
290 | if (flag_check(sigma->symbol)) { |
291 | if (name == NULL) { |
292 | *(ftable+(sigma->number)) = 1; |
293 | } else { |
294 | csym = (sigma->symbol) + 3; |
295 | if (strncmp(csym,name,strlen(name)) == 0 && (strlen(csym)>strlen(name)) && (strncmp(csym+strlen(name),".",1) == 0 || strncmp(csym+strlen(name),"@",1) == 0)) { |
296 | *(ftable+(sigma->number)) = 1; |
297 | } |
298 | } |
299 | } |
300 | } |
301 | for (i = 0; i < sigmasize; i++) { |
302 | if (*(ftable+i)) { |
303 | net->sigma = sigma_remove_num(i, net->sigma); |
304 | } |
305 | } |
306 | |
307 | for (i=0; (fsm+i)->state_no != -1; i++) { |
308 | if ((fsm+i)->in >= 0 && (fsm+i)->out >= 0) { |
309 | if (*(ftable+(fsm+i)->in)) |
310 | (fsm+i)->in = EPSILON; |
311 | if (*(ftable+(fsm+i)->out)) |
312 | (fsm+i)->out = EPSILON; |
313 | } |
314 | } |
315 | |
316 | free(ftable); |
317 | net->is_deterministic = net->is_minimized = net->is_epsilon_free = NO; |
318 | return; |
319 | } |
320 | |
321 | |
322 | |
323 | struct flags *flag_extract (struct fsm *net) { |
324 | struct sigma *sigma; |
325 | struct flags *flags, *flagst; |
326 | |
327 | flags = NULL; |
328 | for (sigma = net->sigma ; sigma != NULL; sigma = sigma->next) { |
329 | if (flag_check(sigma->symbol)) { |
330 | flagst = malloc(sizeof(struct flags)); |
331 | flagst->next = flags; |
332 | flags = flagst; |
333 | |
334 | flags->type = flag_get_type(sigma->symbol); |
335 | flags->name = flag_get_name(sigma->symbol); |
336 | flags->value = flag_get_value(sigma->symbol); |
337 | } |
338 | } |
339 | return(flags); |
340 | } |
341 | |
342 | int flag_check(char *s) { |
343 | |
344 | |
345 | |
346 | |
347 | |
348 | int i; |
349 | i = 0; |
350 | |
351 | if (*(s+i) == '@') { i++; goto s1; } return 0; |
352 | s1: |
353 | if (*(s+i) == 'C') { i++; goto s4; } |
354 | if (*(s+i) == 'N' || *(s+i) == 'E' || *(s+i) == 'U' || *(s+i) == 'P') { i++; goto s2; } |
355 | if (*(s+i) == 'R' || *(s+i) == 'D') { i++; goto s3; } return 0; |
356 | s2: |
357 | if (*(s+i) == '.') { i++; goto s5; } return 0; |
358 | s3: |
359 | if (*(s+i) == '.') { i++; goto s6; } return 0; |
360 | s4: |
361 | if (*(s+i) == '.') { i++; goto s7; } return 0; |
362 | s5: |
363 | if (*(s+i) != '.' && *(s+i) != '\0') { i++; goto s8; } return 0; |
364 | s6: |
365 | if (*(s+i) != '.' && *(s+i) != '\0') { i++; goto s9; } return 0; |
366 | s7: |
367 | if (*(s+i) != '.' && *(s+i) != '\0') { i++; goto s10; } return 0; |
368 | s8: |
369 | if (*(s+i) == '.') { i++; goto s7; } |
370 | if (*(s+i) != '.' && *(s+i) != '\0') { i++; goto s8; } return 0; |
371 | s9: |
372 | if (*(s+i) == '@') { i++; goto s11; } |
373 | if (*(s+i) == '.') { i++; goto s7; } |
374 | if (*(s+i) != '.' && *(s+i) != '\0') { i++; goto s9; } return 0; |
375 | |
376 | s10: |
377 | if (*(s+i) == '@') {i++; goto s11;} |
378 | if (*(s+i) != '.' && *(s+i) != '\0') { i++; goto s10; } return 0; |
379 | s11: |
380 | if (*(s+i) == '\0') {return 1;} return 0; |
381 | } |
382 | |
383 | int flag_get_type(char *string) { |
384 | if (strncmp(string+1,"U.",2) == 0) { |
385 | return FLAG_UNIFY; |
386 | } |
387 | if (strncmp(string+1,"C.",2) == 0) { |
388 | return FLAG_CLEAR; |
389 | } |
390 | if (strncmp(string+1,"D.",2) == 0) { |
391 | return FLAG_DISALLOW; |
392 | } |
393 | if (strncmp(string+1,"N.",2) == 0) { |
394 | return FLAG_NEGATIVE; |
395 | } |
396 | if (strncmp(string+1,"P.",2) == 0) { |
397 | return FLAG_POSITIVE; |
398 | } |
399 | if (strncmp(string+1,"R.",2) == 0) { |
400 | return FLAG_REQUIRE; |
401 | } |
402 | if (strncmp(string+1,"E.",2) == 0) { |
403 | return FLAG_EQUAL; |
404 | } |
405 | return 0; |
406 | } |
407 | |
408 | char *flag_get_name(char *string) { |
409 | int i, start, end, len; |
410 | start = end = 0; |
411 | len = strlen(string); |
412 | |
413 | for (i=0; i < len; i += (utf8skip(string+i) + 1)) { |
414 | if (*(string+i) == '.' && start == 0) { |
415 | start = i+1; |
416 | continue; |
417 | } |
418 | if ((*(string+i) == '.' || *(string+i) == '@') && start != 0) { |
419 | end = i; |
420 | break; |
421 | } |
422 | } |
423 | if (start > 0 && end > 0) { |
424 | return(xxstrndup(string+start,end-start)); |
425 | } |
426 | return NULL; |
427 | } |
428 | |
429 | char *flag_get_value(char *string) { |
430 | int i, first, start, end, len; |
431 | first = start = end = 0; |
432 | len = strlen(string); |
433 | |
434 | for (i=0; i < len; i += (utf8skip(string+i) + 1)) { |
435 | if (*(string+i) == '.' && first == 0) { |
436 | first = i+1; |
437 | continue; |
438 | } |
439 | if (*(string+i) == '@' && start != 0) { |
440 | end = i; |
441 | break; |
442 | } |
443 | if (*(string+i) == '.' && first != 0) { |
444 | start = i+1; |
445 | continue; |
446 | } |
447 | } |
448 | if (start > 0 && end > 0) { |
449 | return(xxstrndup(string+start,end-start)); |
450 | } |
451 | return NULL; |
452 | } |
453 | |
454 | struct fsm *flag_twosided(struct fsm *net) { |
455 | struct fsm_state *fsm; |
456 | struct sigma *sigma; |
457 | int i, j, tail, *isflag, maxsigma, maxstate, newarcs, change; |
458 | |
459 | |
460 | |
461 | |
462 | maxsigma = sigma_max(net->sigma); |
463 | isflag = calloc(maxsigma+1, sizeof(int)); |
| |
464 | fsm = net->states; |
465 | for (sigma = net->sigma ; sigma != NULL; sigma = sigma->next) { |
| 2 | | Assuming 'sigma' is equal to NULL | |
|
| 3 | | Loop condition is false. Execution continues on line 472 | |
|
466 | if (flag_check(sigma->symbol)) { |
467 | *(isflag+sigma->number) = 1; |
468 | } else { |
469 | *(isflag+sigma->number) = 0; |
470 | } |
471 | } |
472 | maxstate = 0; |
473 | change = 0; |
474 | for (i = 0, newarcs = 0; (fsm+i)->state_no != -1 ; i++) { |
| 4 | | Assuming the condition is false | |
|
| 5 | | Loop condition is false. Execution continues on line 491 | |
|
475 | maxstate = (fsm+i)->state_no > maxstate ? (fsm+i)->state_no : maxstate; |
476 | if ((fsm+i)->target == -1) |
477 | continue; |
478 | if (*(isflag+(fsm+i)->in) && (fsm+i)->out == EPSILON) { |
479 | change = 1; |
480 | (fsm+i)->out = (fsm+i)->in; |
481 | } |
482 | else if (*(isflag+(fsm+i)->out) && (fsm+i)->in == EPSILON) { |
483 | change = 1; |
484 | (fsm+i)->in = (fsm+i)->out; |
485 | } |
486 | if ((*(isflag+(fsm+i)->in) || *(isflag+(fsm+i)->out)) && (fsm+i)->in != (fsm+i)->out) { |
487 | newarcs++; |
488 | } |
489 | } |
490 | |
491 | if (newarcs == 0) { |
| |
492 | if (change == 1) { |
| 7 | | Potential leak of memory pointed to by 'isflag' |
|
493 | net->is_deterministic = UNK; |
494 | net->is_minimized = UNK; |
495 | net->is_pruned = UNK; |
496 | return fsm_topsort(fsm_minimize(net)); |
497 | } |
498 | return net; |
499 | } |
500 | net->states = realloc(net->states, sizeof(struct fsm)*(i+newarcs)); |
501 | fsm = net->states; |
502 | tail = j = i; |
503 | maxstate++; |
504 | for (i = 0; i < tail; i++) { |
505 | |
506 | if ((fsm+i)->target == -1) |
507 | continue; |
508 | if ((*(isflag+(fsm+i)->in) || *(isflag+(fsm+i)->out)) && (fsm+i)->in != (fsm+i)->out) { |
509 | if (*(isflag+(fsm+i)->in) && !*(isflag+(fsm+i)->out)) { |
510 | j = add_fsm_arc(fsm, j, maxstate, EPSILON, (fsm+i)->out, (fsm+i)->target, 0, 0); |
511 | (fsm+i)->out = (fsm+i)->in; |
512 | (fsm+i)->target = maxstate; |
513 | maxstate++; |
514 | } |
515 | else if (*(isflag+(fsm+i)->out) && !*(isflag+(fsm+i)->in)) { |
516 | j = add_fsm_arc(fsm, j, maxstate, (fsm+i)->out, (fsm+i)->out, (fsm+i)->target, 0, 0); |
517 | (fsm+i)->out = EPSILON; |
518 | (fsm+i)->target = maxstate; |
519 | maxstate++; |
520 | } |
521 | else if (*(isflag+(fsm+i)->in) && *(isflag+(fsm+i)->out)) { |
522 | j = add_fsm_arc(fsm, j, maxstate, (fsm+i)->out, (fsm+i)->out, (fsm+i)->target, 0, 0); |
523 | (fsm+i)->out = (fsm+i)->in; |
524 | (fsm+i)->target = maxstate; |
525 | maxstate++; |
526 | } |
527 | } |
528 | } |
529 | |
530 | add_fsm_arc(fsm, j, -1, -1, -1, -1, -1, -1); |
531 | net->is_deterministic = UNK; |
532 | net->is_minimized = UNK; |
533 | return fsm_topsort(fsm_minimize(net)); |
534 | } |