Bug Summary

File:bld/../ini/ini_valueobj_ut.c
Location:line 62, column 10
Description:Although the value stored to 'error' is used in the enclosing expression, the value is never actually read from 'error'

Annotated Source Code

1/*
2 INI LIBRARY
3
4 Unit test for the value object.
5
6 Copyright (C) Dmitri Pal <dpal@redhat.com> 2010
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "config.h"
21#include <errno(*__errno_location ()).h> /* for errors */
22#include <stdint.h>
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <limits.h>
27
28#include "ini_valueobj.h"
29#include "ini_defines.h"
30#define TRACE_HOME
31#include "trace.h"
32
33#define TEST_SIZE80 80
34
35int verbose = 0;
36
37#define VOOUT(foo)do { if (verbose) foo; } while(0) \
38 do { \
39 if (verbose) foo; \
40 } while(0)
41
42
43typedef int (*test_fn)(void);
44
45
46extern void ref_array_debug(struct ref_array *ra);
47
48static int create_comment(int i, struct ini_comment **ic)
49{
50 int error = EOK0;
51 const char *template = ";Line 0 of the value %d";
52 char comment[TEST_SIZE80];
53 struct ini_comment *new_ic = NULL((void*)0);
54
55 TRACE_FLOW_ENTRY();
56
57 snprintf(comment, TEST_SIZE80, template, i);
58
59
60 if ((error = ini_comment_create(&new_ic)) ||
61 (error = ini_comment_build(new_ic, comment)) ||
62 (error = ini_comment_build(new_ic, NULL((void*)0))) ||
Although the value stored to 'error' is used in the enclosing expression, the value is never actually read from 'error'
63 (error = ini_comment_build(new_ic, "#This is the second line")) ||
64 (error = ini_comment_build(new_ic, ";This is the third line")) ||
65 (error = ini_comment_build(new_ic, ""))) {
66 printf("Failed to create comment object\n");
67 ini_comment_destroy(new_ic);
68 return -1;
69 }
70
71 *ic = new_ic;
72
73 TRACE_FLOW_EXIT();
74 return EOK0;
75}
76
77/* Save value to the file */
78/* NOTE: might be moved into the API in future */
79int save_value(FILE *ff, const char *key, struct value_obj *vo)
80{
81
82 int error = EOK0;
83 struct simplebuffer *sbobj = NULL((void*)0);
84 uint32_t left = 0;
85
86 TRACE_FLOW_ENTRY();
87
88 error = simplebuffer_alloc(&sbobj);
89 if (error) {
90 TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
91 return error;
92 }
93
94 /* Serialize */
95 error = value_serialize(vo, key, sbobj);
96 if (error) {
97 printf("Failed to serialize a value object %d.\n", error);
98 simplebuffer_free(sbobj);
99 return error;
100 }
101
102 /* Add CR */
103 error = simplebuffer_add_cr(sbobj);
104 if (error) {
105 TRACE_ERROR_NUMBER("Failed to add CR", error);
106 simplebuffer_free(sbobj);
107 return error;
108 }
109
110 /* Save */
111 left = simplebuffer_get_len(sbobj);
112 while (left > 0) {
113 error = simplebuffer_write(fileno(ff), sbobj, &left);
114 if (error) {
115 printf("Failed to write value object %d.\n", error);
116 simplebuffer_free(sbobj);
117 return error;
118 }
119 }
120
121 simplebuffer_free(sbobj);
122
123 TRACE_FLOW_EXIT();
124 return EOK0;
125}
126
127/* Test to create value object using arrays */
128int other_create_test(FILE *ff, struct value_obj **vo)
129{
130 int error = EOK0;
131 struct value_obj *new_vo = NULL((void*)0);
132 struct ref_array *raw_lines;
133 struct ref_array *raw_lengths;
134 struct ini_comment *ic = NULL((void*)0);
135 struct ini_comment *ic2 = NULL((void*)0);
136 char *val;
137 const char *vallines[] = { "Domain1,",
138 " Domain2 ,",
139 " Domain3" };
140 const char *fullstr;
141 const char *expected = "Domain1, Domain2 , Domain3";
142 int i;
143 uint32_t origin = 0;
144 uint32_t line = 0;
145 uint32_t len = 0;
146 uint32_t expected_len = 0;
147
148
149 TRACE_FLOW_ENTRY();
150
151 /* Create a pair of arrays */
152 error = value_create_arrays(&raw_lines,
153 &raw_lengths);
154 if (error) {
155 printf("Failed to create arrays %d.\n", error);
156 return error;
157 }
158
159 for (i=0; i< 3; i++) {
160 errno(*__errno_location ()) = 0;
161 val = strdup(vallines[i]);
162 if (val == NULL((void*)0)) {
163 error = errno(*__errno_location ());
164 printf("Failed to dup memory %d.\n", error);
165 value_destroy_arrays(raw_lines,
166 raw_lengths);
167 return error;
168 }
169
170 /* Add line to the arrays */
171 error = value_add_to_arrays(val,
172 strlen(val),
173 raw_lines,
174 raw_lengths);
175 if (error) {
176 printf("Failed to add to arrays %d.\n", error);
177 value_destroy_arrays(raw_lines,
178 raw_lengths);
179 return error;
180 }
181
182 }
183
184 /* Create a comment */
185 error = create_comment(1000, &ic);
186 if (error) {
187 printf("Failed to create comment %d.\n", error);
188 value_destroy_arrays(raw_lines,
189 raw_lengths);
190 return error;
191 }
192
193 /* Create object */
194 error = value_create_from_refarray(raw_lines,
195 raw_lengths,
196 1,
197 INI_VALUE_READ0,
198 3,
199 70,
200 ic,
201 &new_vo);
202
203 if (error) {
204 printf("Failed to create comment %d.\n", error);
205 value_destroy_arrays(raw_lines,
206 raw_lengths);
207 ini_comment_destroy(ic);
208 return error;
209 }
210
211 /* Save value to the file */
212 error = save_value(ff, "baz", new_vo);
213 if (error) {
214 printf("Failed to save value to file %d.\n", error);
215 value_destroy(new_vo);
216 return error;
217 }
218
219 /* Now do assertions and modifications to the object */
220
221 /* NOTE: Below this line do not need to free arrays or comment
222 * they became internal parts of the value object
223 * and will be freed as a part of it.
224 */
225
226 /* Get concatenated value */
227 error = value_get_concatenated(new_vo,
228 &fullstr);
229
230 if (error) {
231 printf("Failed to get the string %d.\n", error);
232 value_destroy(new_vo);
233 return error;
234 }
235
236 /* Get length of the concatenated value */
237 value_get_concatenated_len(new_vo, &len);
238 expected_len = strlen(expected);
239
240 if ((len != expected_len) ||
241 (strncmp(fullstr, expected, expected_len + 1) != 0)) {
242 printf("The expected value is different.\n%s\n", fullstr);
243 value_destroy(new_vo);
244 return EINVAL22;
245 }
246
247 /* Get value's origin */
248 error = value_get_origin(new_vo, &origin);
249 if (error) {
250 printf("Failed to get origin %d.\n", error);
251 value_destroy(new_vo);
252 return error;
253 }
254
255 if (origin != INI_VALUE_READ0) {
256 printf("The expected origin is different.\n%d\n", origin);
257 value_destroy(new_vo);
258 return EINVAL22;
259 }
260
261 /* Get value's line */
262 error = value_get_line(new_vo, &line);
263 if (error) {
264 printf("Failed to get origin %d.\n", error);
265 value_destroy(new_vo);
266 return error;
267 }
268
269 if (line != 1) {
270 printf("The expected line is different.\n%d\n", origin);
271 value_destroy(new_vo);
272 return EINVAL22;
273 }
274
275 /* Get comment from the value */
276 ic = NULL((void*)0);
277 error = value_extract_comment(new_vo, &ic);
278 if (error) {
279 printf("Failed to extract comment %d.\n", error);
280 value_destroy(new_vo);
281 return error;
282 }
283
284 if (ic == NULL((void*)0)) {
285 printf("The expected comment to be there.\n");
286 value_destroy(new_vo);
287 return error;
288 }
289
290 VOOUT(ini_comment_print(ic, stdout))do { if (verbose) ini_comment_print(ic, stdout); } while(0);
291
292 /* Get comment again */
293 ic2 = NULL((void*)0);
294 error = value_extract_comment(new_vo, &ic2);
295 if (error) {
296 printf("Failed to extract comment %d.\n", error);
297 value_destroy(new_vo);
298 ini_comment_destroy(ic);
299 return error;
300 }
301
302 if (ic2 != NULL((void*)0)) {
303 printf("The expected NO comment to be there.\n");
304 value_destroy(new_vo);
305 ini_comment_destroy(ic);
306 /* No free for ic2 since it is the same object */
307
308 /* But this should not happen anyways -
309 * it will be coding error.
310 */
311 return EINVAL22;
312 }
313
314 /* Put comment back */
315 error = value_put_comment(new_vo, ic);
316 if (error) {
317 printf("Failed to put comment back %d.\n", error);
318 value_destroy(new_vo);
319 ini_comment_destroy(ic);
320 return error;
321 }
322
323 /* Save value to the file */
324 error = save_value(ff, "bar", new_vo);
325 if (error) {
326 printf("Failed to save value to file %d.\n", error);
327 value_destroy(new_vo);
328 return error;
329 }
330
331 *vo = new_vo;
332
333 TRACE_FLOW_EXIT();
334 return EOK0;
335}
336
337/* Modify the value object */
338int modify_test(FILE *ff, struct value_obj *vo)
339{
340 int error = EOK0;
341 const char *strval = "Domain100, Domain200, Domain300";
342
343 TRACE_FLOW_ENTRY();
344
345
346 /* Update key length */
347 error = value_set_keylen(vo, strlen("foobar"));
348 if (error) {
349 printf("Failed to change key length %d.\n", error);
350 return error;
351 }
352
353 /* Update value */
354 error = value_update(vo,
355 strval,
356 strlen(strval),
357 INI_VALUE_CREATED1,
358 10);
359 if (error) {
360 printf("Failed to update value %d.\n", error);
361 return error;
362 }
363
364
365 /* Save value to the file */
366 error = save_value(ff, "foobar", vo);
367 if (error) {
368 printf("Failed to save value to file %d.\n", error);
369 return error;
370 }
371
372 TRACE_FLOW_EXIT();
373 return EOK0;
374}
375
376
377int vo_basic_test(void)
378{
379 int error = EOK0;
380 const char *strvalue = "Test multi_word_value_that_will_"
381 "be_split_between_several_lines_!";
382
383 /* Other testing can be done with the following string:
384 * const char *strvalue = "Test multi word value that "
385 * "will be split between several lines";
386 */
387
388 struct value_obj *vo = NULL((void*)0);
389 uint32_t wrap = 0;
390 struct ini_comment *ic = NULL((void*)0);
391 FILE *ff = NULL((void*)0);
392
393 TRACE_FLOW_ENTRY();
394
395 errno(*__errno_location ()) = 0;
396 ff = fopen("test.ini","wt");
397 if (ff == NULL((void*)0)) {
398 error = errno(*__errno_location ());
399 printf("Failed to open file. Error %d.\n", error);
400 return error;
401 }
402
403
404 for (wrap = 0; wrap < 80; wrap++) {
405
406 ic = NULL((void*)0);
407 error = create_comment(wrap, &ic);
408 if (error) {
409 printf("Failed to create a new comment object %d.\n", error);
410 fclose(ff);
411 return error;
412 }
413
414 vo = NULL((void*)0);
415 error = value_create_new(strvalue,
416 strlen(strvalue),
417 INI_VALUE_CREATED1,
418 3,
419 wrap,
420 ic,
421 &vo);
422 if (error) {
423 printf("Failed to create a new value object %d.\n", error);
424 ini_comment_destroy(ic);
425 fclose(ff);
426 return error;
427 }
428
429 error = save_value(ff, "key", vo);
430 if (error) {
431 printf("Failed to save value to file %d.\n", error);
432 value_destroy(vo);
433 fclose(ff);
434 return error;
435 }
436
437 value_destroy(vo);
438 }
439
440 vo = NULL((void*)0);
441
442 /* Run other create test here */
443 error = other_create_test(ff, &vo);
444 if (error) {
445 printf("Create test failed %d.\n", error);
446 fclose(ff);
447 return error;
448 }
449
450 /* Run modify test here */
451 error = modify_test(ff, vo);
452 if (error) {
453 printf("Modify test failed %d.\n", error);
454 fclose(ff);
455 value_destroy(vo);
456 return error;
457 }
458
459 value_destroy(vo);
460
461
462 ic = NULL((void*)0);
463 error = create_comment(100, &ic);
464 if (error) {
465 printf("Failed to create a new comment object %d.\n", error);
466 fclose(ff);
467 return error;
468 }
469
470 ini_comment_print(ic, ff);
471
472 ini_comment_destroy(ic);
473
474 fclose(ff);
475
476 TRACE_FLOW_EXIT();
477 return EOK0;
478}
479
480int vo_copy_test(void)
481{
482 int error = EOK0;
483 const char *strvalue = "Test multi word value that "
484 "will be split between several lines";
485
486 struct value_obj *vo = NULL((void*)0);
487 struct value_obj *vo_copy = NULL((void*)0);
488 uint32_t wrap = 0;
489 struct ini_comment *ic = NULL((void*)0);
490 FILE *ff = NULL((void*)0);
491 char comment[100];
492
493 TRACE_FLOW_ENTRY();
494
495 VOOUT(printf("Copy test\n"))do { if (verbose) printf("Copy test\n"); } while(0);
496
497 errno(*__errno_location ()) = 0;
498 ff = fopen("test.ini","a");
499 if (ff == NULL((void*)0)) {
500 error = errno(*__errno_location ());
501 printf("Failed to open file. Error %d.\n", error);
502 return error;
503 }
504
505 error = ini_comment_create(&ic);
506 if (error) {
507 printf("Failed to create comment object\n");
508 fclose(ff);
509 return -1;
510 }
511
512 error = ini_comment_append(ic, "#This is a copy test!");
513 if (error) {
514 printf("Failed to add a line to the comment %d.\n", error);
515 ini_comment_destroy(ic);
516 fclose(ff);
517 return error;
518 }
519
520 error = ini_comment_append(ic, "#Replacable comment line");
521 if (error) {
522 printf("Failed to add a line to the comment %d.\n", error);
523 ini_comment_destroy(ic);
524 fclose(ff);
525 return error;
526 }
527
528 error = value_create_new(strvalue,
529 strlen(strvalue),
530 INI_VALUE_CREATED1,
531 3,
532 20,
533 ic,
534 &vo);
535 if (error) {
536 printf("Failed to create a new value object %d.\n", error);
537 ini_comment_destroy(ic);
538 fclose(ff);
539 return error;
540 }
541
542 error = save_value(ff, "key", vo);
543 if (error) {
544 printf("Failed to save value to file %d.\n", error);
545 value_destroy(vo);
546 fclose(ff);
547 return error;
548 }
549
550 for (wrap = 0; wrap < 80; wrap++) {
551
552 TRACE_INFO_NUMBER("Iteration:", wrap);
553
554 vo_copy = NULL((void*)0);
555 error = value_copy(vo, &vo_copy);
556 if (error) {
557 printf("Failed to create a new value object %d.\n", error);
558 value_destroy(vo);
559 fclose(ff);
560 return error;
561 }
562
563 error = value_set_boundary(vo_copy, wrap);
564 if (error) {
565 printf("Failed to set boundary %d.\n", error);
566 value_destroy(vo);
567 value_destroy(vo_copy);
568 fclose(ff);
569 return error;
570 }
571
572 /* Get comment from the value */
573 ic = NULL((void*)0);
574 error = value_extract_comment(vo_copy, &ic);
575 if (error) {
576 printf("Failed to extract comment %d.\n", error);
577 value_destroy(vo);
578 value_destroy(vo_copy);
579 fclose(ff);
580 return error;
581 }
582
583 /* Replace comment in the value */
584 snprintf(comment, TEST_SIZE80, ";This is value with boundary %d", wrap);
585 VOOUT(printf("Comment: %s\n", comment))do { if (verbose) printf("Comment: %s\n", comment); } while(0
)
;
586 error = ini_comment_replace(ic, 1, comment);
587 if (error) {
588 printf("Failed to replace comment %d.\n", error);
589 value_destroy(vo);
590 value_destroy(vo_copy);
591 fclose(ff);
592 return error;
593 }
594
595 /* Set comment into the value */
596 error = value_put_comment(vo_copy, ic);
597 if (error) {
598 printf("Failed to set comment %d.\n", error);
599 value_destroy(vo);
600 value_destroy(vo_copy);
601 fclose(ff);
602 return error;
603 }
604
605 error = save_value(ff, "key", vo_copy);
606 if (error) {
607 printf("Failed to save value to file %d.\n", error);
608 value_destroy(vo);
609 value_destroy(vo_copy);
610 fclose(ff);
611 return error;
612 }
613
614 value_destroy(vo_copy);
615 }
616
617 value_destroy(vo);
618 fclose(ff);
619 TRACE_FLOW_EXIT();
620 return EOK0;
621}
622
623int vo_show_test(void)
624{
625 VOOUT(system("cat test.ini"))do { if (verbose) system("cat test.ini"); } while(0);
626 return EOK0;
627}
628
629int vo_mc_test(void)
630{
631 int error = EOK0;
632 struct value_obj *vo1 = NULL((void*)0);
633 struct value_obj *vo2 = NULL((void*)0);
634 struct ini_comment *ic = NULL((void*)0);
635
636 TRACE_FLOW_ENTRY();
637
638 VOOUT(printf("<=== Merge Comment Test ===>\n"))do { if (verbose) printf("<=== Merge Comment Test ===>\n"
); } while(0)
;
639
640 error = create_comment(1, &ic);
641 if (error) {
642 printf("Failed to create a new comment object %d.\n", error);
643 return error;
644 }
645
646 error = value_create_new("test1",
647 5,
648 INI_VALUE_CREATED1,
649 3,
650 80,
651 ic,
652 &vo1);
653 if (error) {
654 printf("Failed to create the first value object %d.\n", error);
655 ini_comment_destroy(ic);
656 return error;
657 }
658
659 error = create_comment(2, &ic);
660 if (error) {
661 printf("Failed to create a new comment object %d.\n", error);
662 value_destroy(vo1);
663 return error;
664 }
665
666 error = value_create_new("test2",
667 5,
668 INI_VALUE_CREATED1,
669 3,
670 80,
671 ic,
672 &vo2);
673 if (error) {
674 printf("Failed to create the second value object %d.\n", error);
675 ini_comment_destroy(ic);
676 value_destroy(vo1);
677 return error;
678 }
679
680 /* Merge comment from one value into another */
681 error = value_merge_comment(vo2, vo1);
682 if (error) {
683 printf("Failed to merge comments %d.\n", error);
684 value_destroy(vo1);
685 value_destroy(vo2);
686 return error;
687 }
688
689 value_destroy(vo2);
690
691 VOOUT(printf("<=== Key ===>\n"))do { if (verbose) printf("<=== Key ===>\n"); } while(0);
692 VOOUT(value_print("key", vo1))do { if (verbose) value_print("key", vo1); } while(0);
693
694 error = value_extract_comment(vo1, &ic);
695 if (error) {
696 printf("Failed to extract comment %d.\n", error);
697 value_destroy(vo1);
698 return error;
699 }
700
701 value_destroy(vo1);
702
703 VOOUT(printf("<=== Comment ===>\n"))do { if (verbose) printf("<=== Comment ===>\n"); } while
(0)
;
704 VOOUT(ini_comment_print(ic, stdout))do { if (verbose) ini_comment_print(ic, stdout); } while(0);
705 ini_comment_destroy(ic);
706
707 TRACE_FLOW_EXIT();
708 return EOK0;
709}
710
711
712/* Main function of the unit test */
713int main(int argc, char *argv[])
714{
715 int error = 0;
716 test_fn tests[] = { vo_basic_test,
717 vo_copy_test,
718 vo_show_test,
719 vo_mc_test,
720 NULL((void*)0) };
721 test_fn t;
722 int i = 0;
723 char *var;
724
725 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
726 else {
727 var = getenv("COMMON_TEST_VERBOSE");
728 if (var) verbose = 1;
729 }
730
731 VOOUT(printf("Start\n"))do { if (verbose) printf("Start\n"); } while(0);
732
733 while ((t = tests[i++])) {
734 error = t();
735 if (error) {
736 VOOUT(printf("Failed with error %d!\n", error))do { if (verbose) printf("Failed with error %d!\n", error); }
while(0)
;
737 return error;
738 }
739 }
740
741 VOOUT(printf("Success!\n"))do { if (verbose) printf("Success!\n"); } while(0);
742 return 0;
743}