Bug Summary

File:src/mod/xml_int/mod_xml_rpc/../../../../libs/xmlrpc-c/lib/util/cmdline_parser.c
Location:line 211, column 9
Description:Branch condition evaluates to a garbage value

Annotated Source Code

1#define _XOPEN_SOURCE700 600 /* Make sure <string.h> has strdup() */
2
3#include "xmlrpc_config.h" /* prereq for mallocvar.h -- defines __inline__ */
4
5#include <sys/types.h>
6#include <string.h>
7#include <stdio.h>
8#include <errno(*__errno_location ()).h>
9#include <limits.h>
10
11#include "bool.h"
12#include "int.h"
13#include "mallocvar.h"
14#include "casprintf.h"
15#include "getoptx.h"
16#include "string_parser.h"
17
18#include "cmdline_parser.h"
19
20#define MAXOPTS100 100
21
22struct optionDesc {
23 const char * name;
24 enum optiontype type;
25 bool present;
26 union {
27 unsigned int u;
28 int i;
29 const char * s;
30 uint64_t llu;
31 double d;
32 } value;
33};
34
35
36
37struct cmdlineParserCtl {
38 struct optionDesc * optionDescArray;
39 unsigned int numOptions;
40 const char ** argumentArray;
41 unsigned int numArguments;
42};
43
44
45
46static struct optionx *
47createLongOptsArray(struct optionDesc * const optionDescArray,
48 unsigned int const numOptions) {
49
50 struct optionx * longopts;
51
52 MALLOCARRAY(longopts, numOptions+1)do { void * array; mallocProduct(&array, numOptions+1, sizeof
(longopts[0])); longopts = array; } while (0)
;
53 if (longopts != NULL((void*)0)) {
54 unsigned int i;
55
56 for (i = 0; i < numOptions; ++i) {
57 longopts[i].name = optionDescArray[i].name;
58 /* If the option takes a value, we say it is optional even
59 though it never is. That's because if we say it is
60 mandatory, getopt_long_only() pretends it doesn't even
61 recognize the option if the user doesn't give a value.
62 We prefer to generate a meaningful error message when
63 the user omits a required option value.
64 */
65 longopts[i].has_arg =
66 optionDescArray[i].type == OPTTYPE_FLAG ?
67 no_argument : optional_argument;
68 longopts[i].flag = NULL((void*)0);
69 longopts[i].val = i;
70 }
71 longopts[numOptions].name = 0;
72 longopts[numOptions].has_arg = 0;
73 longopts[numOptions].flag = 0;
74 longopts[numOptions].val = 0;
75 }
76 return longopts;
77}
78
79
80
81static void
82parseInt(enum optiontype const type,
83 const char * const optarg,
84 unsigned int * const valueUintP,
85 int * const valueIntP,
86 const char ** const errorP) {
87
88 if (optarg == NULL((void*)0))
89 casprintf(errorP, "Option requires a value");
90 else if (strlen(optarg) == 0)
91 casprintf(errorP, "Numeric option value is null string");
92 else {
93 char * tailptr;
94 long const longvalue = strtol(optarg, &tailptr, 10);
95
96 if (*tailptr != '\0')
97 casprintf(errorP, "Non-numeric value "
98 "for numeric option value: '%s'", optarg);
99 else if (errno(*__errno_location ()) == ERANGE34 || longvalue > INT_MAX2147483647)
100 casprintf(errorP, "Numeric value out of range: %s", optarg);
101 else {
102 if (type == OPTTYPE_UINT) {
103 if (longvalue < 0)
104 casprintf(errorP, "Unsigned numeric value is "
105 "negative: %ld", longvalue);
106 else {
107 *errorP = NULL((void*)0);
108 *valueUintP = (unsigned int) longvalue;
109 }
110 } else {
111 *errorP = NULL((void*)0);
112 *valueIntP = (int) longvalue;
113 }
114 }
115 }
116}
117
118
119
120static void
121parseBinUint(const char * const optarg,
122 uint64_t * const valueP,
123 const char ** const errorP) {
124
125 if (optarg == NULL((void*)0))
13
Assuming 'optarg' is not equal to null
14
Taking false branch
126 casprintf(errorP, "Option requires a value");
127 else if (strlen(optarg) == 0)
15
Taking false branch
128 casprintf(errorP, "Numeric option value is null string");
129 else {
130 const char * error;
131 interpretBinUint(optarg, valueP, &error);
132
133 if (error) {
16
Assuming 'error' is null
17
Taking false branch
134 casprintf(errorP, "Invalid numeric option value '%s'. %s",
135 optarg, error);
136 strfree(error);
137 }
138 }
139}
140
141
142
143static void
144parseFloat(const char * const optarg,
145 double * const valueP,
146 const char ** const errorP) {
147
148 if (optarg == NULL((void*)0))
149 casprintf(errorP, "Option requires a value");
150 else if (strlen(optarg) == 0)
151 casprintf(errorP, "Numeric option value is null string");
152 else {
153 char * tailptr;
154 double const doublevalue = strtod(optarg, &tailptr);
155
156 if (*tailptr != '\0')
157 casprintf(errorP, "Non-numeric value "
158 "for numeric option value: '%s'", optarg);
159 else if (errno(*__errno_location ()) == ERANGE34)
160 casprintf(errorP, "Numeric value out of range: %s", optarg);
161 else {
162 *errorP = NULL((void*)0);
163 *valueP = doublevalue;
164 }
165 }
166}
167
168
169
170static void
171parseOptionValue(const char * const optarg,
172 struct optionDesc * const optionP,
173 const char ** const errorP) {
174
175 switch (optionP->type) {
11
Control jumps to 'case OPTTYPE_BINUINT:' at line 192
176 case OPTTYPE_FLAG:
177 *errorP = NULL((void*)0);
178 break;
179 case OPTTYPE_INT:
180 case OPTTYPE_UINT:
181 parseInt(optionP->type, optarg, &optionP->value.u, &optionP->value.i,
182 errorP);
183 break;
184 case OPTTYPE_STRING:
185 if (optarg == NULL((void*)0))
186 casprintf(errorP, "Option requires a value");
187 else {
188 *errorP = NULL((void*)0);
189 optionP->value.s = strdup(optarg)(__extension__ (__builtin_constant_p (optarg) && ((size_t
)(const void *)((optarg) + 1) - (size_t)(const void *)(optarg
) == 1) ? (((const char *) (optarg))[0] == '\0' ? (char *) calloc
((size_t) 1, (size_t) 1) : ({ size_t __len = strlen (optarg)
+ 1; char *__retval = (char *) malloc (__len); if (__retval !=
((void*)0)) __retval = (char *) memcpy (__retval, optarg, __len
); __retval; })) : __strdup (optarg)))
;
190 }
191 break;
192 case OPTTYPE_BINUINT:
193 parseBinUint(optarg, &optionP->value.llu, errorP);
12
Calling 'parseBinUint'
18
Returning from 'parseBinUint'
194 break;
19
Execution continues on line 193
195 case OPTTYPE_FLOAT:
196 parseFloat(optarg, &optionP->value.d, errorP);
197 break;
198 }
199}
200
201
202
203static void
204processOption(struct optionDesc * const optionP,
205 const char * const optarg,
206 const char ** const errorP) {
207
208 const char * error;
9
'error' declared without an initial value
209
210 parseOptionValue(optarg, optionP, &error);
10
Calling 'parseOptionValue'
20
Returning from 'parseOptionValue'
211 if (error)
21
Branch condition evaluates to a garbage value
212 casprintf(errorP, "Error in '%s' option: %s", optionP->name, error);
213 else
214 optionP->present = true;
215}
216
217
218
219static void
220extractArguments(struct cmdlineParserCtl * const cpP,
221 unsigned int const argc,
222 const char ** const argv) {
223
224 cpP->numArguments = argc - getopt_argstart();
225 MALLOCARRAY(cpP->argumentArray, cpP->numArguments)do { void * array; mallocProduct(&array, cpP->numArguments
, sizeof(cpP->argumentArray[0])); cpP->argumentArray = array
; } while (0)
;
226
227 if (cpP->argumentArray == NULL((void*)0)) {
228 fprintf(stderrstderr, "Unable to allocate memory for argument array "
229 "(%u arguments)\n", cpP->numArguments);
230 abort();
231 } else {
232 unsigned int i;
233
234 for (i = 0; i < cpP->numArguments; ++i) {
235 cpP->argumentArray[i] = strdup(argv[getopt_argstart() + i])(__extension__ (__builtin_constant_p (argv[getopt_argstart() +
i]) && ((size_t)(const void *)((argv[getopt_argstart
() + i]) + 1) - (size_t)(const void *)(argv[getopt_argstart()
+ i]) == 1) ? (((const char *) (argv[getopt_argstart() + i])
)[0] == '\0' ? (char *) calloc ((size_t) 1, (size_t) 1) : ({ size_t
__len = strlen (argv[getopt_argstart() + i]) + 1; char *__retval
= (char *) malloc (__len); if (__retval != ((void*)0)) __retval
= (char *) memcpy (__retval, argv[getopt_argstart() + i], __len
); __retval; })) : __strdup (argv[getopt_argstart() + i])))
;
236 if (cpP->argumentArray[i] == NULL((void*)0)) {
237 fprintf(stderrstderr, "Unable to allocate memory for Argument %u\n",
238 i);
239 abort();
240 }
241 }
242 }
243}
244
245
246
247void
248cmd_processOptions(cmdlineParser const cpP,
249 int const argc,
250 const char ** const argv,
251 const char ** const errorP) {
252
253 struct optionx * longopts;
254
255 longopts = createLongOptsArray(cpP->optionDescArray, cpP->numOptions);
256
257 if (longopts == NULL((void*)0))
1
Taking false branch
258 casprintf(errorP, "Unable to get memory for longopts array");
259 else {
260 int endOfOptions;
261 unsigned int i;
262
263 *errorP = NULL((void*)0);
264
265 /* Set up initial assumption: No options present */
266
267 for (i = 0; i < cpP->numOptions; ++i)
2
Loop condition is false. Execution continues on line 270
268 cpP->optionDescArray[i].present = false;
269
270 endOfOptions = false; /* initial value */
271
272 while (!endOfOptions && !*errorP) {
3
Loop condition is true. Entering loop body
273 int const opterr0 = 0;
274 /* Don't let getopt_long_only() print an error message */
275 unsigned int longoptsIndex;
276 const char * unrecognizedOption;
277 const char * optarg;
278
279 getopt_long_onlyx(argc, (char**) argv, "", longopts,
280 &longoptsIndex, opterr0,
281 &endOfOptions, &optarg, &unrecognizedOption);
282
283 if (unrecognizedOption)
4
Assuming 'unrecognizedOption' is null
5
Taking false branch
284 casprintf(errorP, "Unrecognized option: '%s'",
285 unrecognizedOption);
286 else {
287 if (!endOfOptions)
6
Assuming 'endOfOptions' is 0
7
Taking true branch
288 processOption(&cpP->optionDescArray[longoptsIndex], optarg,
8
Calling 'processOption'
289 errorP);
290 }
291 }
292 if (!*errorP)
293 extractArguments(cpP, argc, argv);
294
295 free(longopts);
296 }
297}
298
299
300
301cmdlineParser
302cmd_createOptionParser(void) {
303
304 struct cmdlineParserCtl * cpP;
305
306 MALLOCVAR(cpP)cpP = malloc(sizeof(*cpP));
307
308 if (cpP != NULL((void*)0)) {
309 struct optionDesc * optionDescArray;
310
311 cpP->numOptions = 0;
312 MALLOCARRAY(optionDescArray, MAXOPTS)do { void * array; mallocProduct(&array, 100, sizeof(optionDescArray
[0])); optionDescArray = array; } while (0)
;
313 if (optionDescArray == NULL((void*)0)) {
314 free(cpP);
315 cpP = NULL((void*)0);
316 } else
317 cpP->optionDescArray = optionDescArray;
318 }
319 return cpP;
320}
321
322
323
324void
325cmd_destroyOptionParser(cmdlineParser const cpP) {
326
327 unsigned int i;
328
329 for (i = 0; i < cpP->numOptions; ++i) {
330 struct optionDesc const option = cpP->optionDescArray[i];
331 if (option.type == OPTTYPE_STRING && option.present)
332 strfree(option.value.s);
333 strfree(option.name);
334 }
335
336 for (i = 0; i < cpP->numArguments; ++i)
337 strfree(cpP->argumentArray[i]);
338
339 free(cpP->optionDescArray);
340 free(cpP);
341}
342
343
344
345void
346cmd_defineOption(cmdlineParser const cpP,
347 const char * const name,
348 enum optiontype const type) {
349
350 if (cpP->numOptions < MAXOPTS100) {
351 cpP->optionDescArray[cpP->numOptions].name = strdup(name)(__extension__ (__builtin_constant_p (name) && ((size_t
)(const void *)((name) + 1) - (size_t)(const void *)(name) ==
1) ? (((const char *) (name))[0] == '\0' ? (char *) calloc (
(size_t) 1, (size_t) 1) : ({ size_t __len = strlen (name) + 1
; char *__retval = (char *) malloc (__len); if (__retval != (
(void*)0)) __retval = (char *) memcpy (__retval, name, __len)
; __retval; })) : __strdup (name)))
;
352 cpP->optionDescArray[cpP->numOptions].type = type;
353
354 ++cpP->numOptions;
355 }
356}
357
358
359
360static struct optionDesc *
361findOptionDesc(struct cmdlineParserCtl * const cpP,
362 const char * const name) {
363
364 struct optionDesc * retval;
365 unsigned int i;
366
367 retval = NULL((void*)0);
368
369 for (i = 0; i < cpP->numOptions && !retval; ++i)
370 if (strcmp(cpP->optionDescArray[i].name, name)__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(cpP->optionDescArray[i].name) && __builtin_constant_p
(name) && (__s1_len = __builtin_strlen (cpP->optionDescArray
[i].name), __s2_len = __builtin_strlen (name), (!((size_t)(const
void *)((cpP->optionDescArray[i].name) + 1) - (size_t)(const
void *)(cpP->optionDescArray[i].name) == 1) || __s1_len >=
4) && (!((size_t)(const void *)((name) + 1) - (size_t
)(const void *)(name) == 1) || __s2_len >= 4)) ? __builtin_strcmp
(cpP->optionDescArray[i].name, name) : (__builtin_constant_p
(cpP->optionDescArray[i].name) && ((size_t)(const
void *)((cpP->optionDescArray[i].name) + 1) - (size_t)(const
void *)(cpP->optionDescArray[i].name) == 1) && (__s1_len
= __builtin_strlen (cpP->optionDescArray[i].name), __s1_len
< 4) ? (__builtin_constant_p (name) && ((size_t)(
const void *)((name) + 1) - (size_t)(const void *)(name) == 1
) ? __builtin_strcmp (cpP->optionDescArray[i].name, name) :
(__extension__ ({ const unsigned char *__s2 = (const unsigned
char *) (const char *) (name); int __result = (((const unsigned
char *) (const char *) (cpP->optionDescArray[i].name))[0]
- __s2[0]); if (__s1_len > 0 && __result == 0) { __result
= (((const unsigned char *) (const char *) (cpP->optionDescArray
[i].name))[1] - __s2[1]); if (__s1_len > 1 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
cpP->optionDescArray[i].name))[2] - __s2[2]); if (__s1_len
> 2 && __result == 0) __result = (((const unsigned
char *) (const char *) (cpP->optionDescArray[i].name))[3]
- __s2[3]); } } __result; }))) : (__builtin_constant_p (name
) && ((size_t)(const void *)((name) + 1) - (size_t)(const
void *)(name) == 1) && (__s2_len = __builtin_strlen (
name), __s2_len < 4) ? (__builtin_constant_p (cpP->optionDescArray
[i].name) && ((size_t)(const void *)((cpP->optionDescArray
[i].name) + 1) - (size_t)(const void *)(cpP->optionDescArray
[i].name) == 1) ? __builtin_strcmp (cpP->optionDescArray[i
].name, name) : (- (__extension__ ({ const unsigned char *__s2
= (const unsigned char *) (const char *) (cpP->optionDescArray
[i].name); int __result = (((const unsigned char *) (const char
*) (name))[0] - __s2[0]); if (__s2_len > 0 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
name))[1] - __s2[1]); if (__s2_len > 1 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
name))[2] - __s2[2]); if (__s2_len > 2 && __result
== 0) __result = (((const unsigned char *) (const char *) (name
))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp (cpP
->optionDescArray[i].name, name)))); })
== 0)
371 retval = &cpP->optionDescArray[i];
372
373 return retval;
374}
375
376
377
378int
379cmd_optionIsPresent(cmdlineParser const cpP,
380 const char * const name) {
381
382 struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
383
384 bool present;
385
386 if (!optionDescP) {
387 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
388 "optionIsPresent() called for undefined option '%s'\n",
389 name);
390 abort();
391 } else
392 present = optionDescP->present;
393
394 return present;
395}
396
397
398
399unsigned int
400cmd_getOptionValueUint(cmdlineParser const cpP,
401 const char * const name) {
402
403 struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
404
405 unsigned int retval;
406
407 if (!optionDescP) {
408 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
409 "cmd_getOptionValueUint() called for undefined option '%s'\n",
410 name);
411 abort();
412 } else {
413 if (optionDescP->type != OPTTYPE_UINT) {
414 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
415 "cmd_getOptionValueUint() called for non-unsigned integer "
416 "option '%s'\n", optionDescP->name);
417 abort();
418 } else {
419 if (optionDescP->present)
420 retval = optionDescP->value.u;
421 else
422 retval = 0;
423 }
424 }
425 return retval;
426}
427
428
429
430int
431cmd_getOptionValueInt(cmdlineParser const cpP,
432 const char * const name) {
433
434 struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
435
436 int retval;
437
438 if (!optionDescP) {
439 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
440 "cmd_getOptionValueInt() called for undefined option '%s'\n",
441 name);
442 abort();
443 } else {
444 if (optionDescP->type != OPTTYPE_INT) {
445 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
446 "cmd_getOptionValueInt() called for non-integer "
447 "option '%s'\n", optionDescP->name);
448 abort();
449 } else {
450 if (optionDescP->present)
451 retval = optionDescP->value.i;
452 else
453 retval = 0;
454 }
455 }
456
457 return retval;
458}
459
460
461
462const char *
463cmd_getOptionValueString(cmdlineParser const cpP,
464 const char * const name) {
465
466 struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
467
468 const char * retval;
469
470 if (!optionDescP) {
471 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
472 "cmd_getOptionValueString() called for "
473 "undefined option '%s'\n",
474 name);
475 abort();
476 } else {
477 if (optionDescP->type != OPTTYPE_STRING) {
478 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
479 "getOptionValueString() called for non-string "
480 "option '%s'\n", optionDescP->name);
481 abort();
482 } else {
483 if (optionDescP->present) {
484 retval = strdup(optionDescP->value.s)(__extension__ (__builtin_constant_p (optionDescP->value.s
) && ((size_t)(const void *)((optionDescP->value.s
) + 1) - (size_t)(const void *)(optionDescP->value.s) == 1
) ? (((const char *) (optionDescP->value.s))[0] == '\0' ? (
char *) calloc ((size_t) 1, (size_t) 1) : ({ size_t __len = strlen
(optionDescP->value.s) + 1; char *__retval = (char *) malloc
(__len); if (__retval != ((void*)0)) __retval = (char *) memcpy
(__retval, optionDescP->value.s, __len); __retval; })) : __strdup
(optionDescP->value.s)))
;
485 if (retval == NULL((void*)0)) {
486 fprintf(stderrstderr,
487 "out of memory in cmd_getOptionValueString()\n");
488 abort();
489 }
490 } else
491 retval = NULL((void*)0);
492 }
493 }
494 return retval;
495}
496
497
498
499uint64_t
500cmd_getOptionValueBinUint(cmdlineParser const cpP,
501 const char * const name) {
502
503 struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
504
505 uint64_t retval;
506
507 if (!optionDescP) {
508 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
509 "cmd_getOptionValueUint() called for undefined option '%s'\n",
510 name);
511 abort();
512 } else {
513 if (optionDescP->type != OPTTYPE_BINUINT) {
514 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
515 "cmd_getOptionValueBinUint() called for "
516 "non-OPTTYPE_BINUINT "
517 "option '%s'\n", optionDescP->name);
518 abort();
519 } else {
520 if (optionDescP->present)
521 retval = optionDescP->value.llu;
522 else
523 retval = 0;
524 }
525 }
526 return retval;
527}
528
529
530
531double
532cmd_getOptionValueFloat(cmdlineParser const cpP,
533 const char * const name) {
534
535 struct optionDesc * const optionDescP = findOptionDesc(cpP, name);
536
537 double retval;
538
539 if (!optionDescP) {
540 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
541 "cmd_getOptionValueInt() called for undefined option '%s'\n",
542 name);
543 abort();
544 } else {
545 if (optionDescP->type != OPTTYPE_FLOAT) {
546 fprintf(stderrstderr, "cmdlineParser called incorrectly. "
547 "cmd_getOptionValueInt() called for non-float "
548 "option '%s'\n", optionDescP->name);
549 abort();
550 } else {
551 if (optionDescP->present)
552 retval = optionDescP->value.d;
553 else
554 retval = 0.0;
555 }
556 }
557 return retval;
558}
559
560
561
562unsigned int
563cmd_argumentCount(cmdlineParser const cpP) {
564
565 return cpP->numArguments;
566
567}
568
569
570
571const char *
572cmd_getArgument(cmdlineParser const cpP,
573 unsigned int const argNumber) {
574
575 const char * retval;
576
577 if (argNumber >= cpP->numArguments)
578 retval = NULL((void*)0);
579 else {
580 retval = strdup(cpP->argumentArray[argNumber])(__extension__ (__builtin_constant_p (cpP->argumentArray[argNumber
]) && ((size_t)(const void *)((cpP->argumentArray[
argNumber]) + 1) - (size_t)(const void *)(cpP->argumentArray
[argNumber]) == 1) ? (((const char *) (cpP->argumentArray[
argNumber]))[0] == '\0' ? (char *) calloc ((size_t) 1, (size_t
) 1) : ({ size_t __len = strlen (cpP->argumentArray[argNumber
]) + 1; char *__retval = (char *) malloc (__len); if (__retval
!= ((void*)0)) __retval = (char *) memcpy (__retval, cpP->
argumentArray[argNumber], __len); __retval; })) : __strdup (cpP
->argumentArray[argNumber])))
;
581
582 if (retval == NULL((void*)0)) {
583 fprintf(stderrstderr,
584 "out of memory in cmd_getArgument()\n");
585 abort();
586 }
587 }
588 return retval;
589}