@@ -346,6 +346,15 @@ static void _pipe_try_read_into_string(int fd, struct oscap_string *string, bool
346
346
}
347
347
}
348
348
349
+
350
+ static void free_env_values (char * * env_values , size_t index_of_first_env_value_not_compiled_in , size_t real_env_values_count ) {
351
+ for (i = index_of_first_env_value_not_compiled_in ; i < real_env_values_count ; i ++ ) {
352
+ free (env_values [i ]);
353
+ }
354
+ free (env_values );
355
+ }
356
+
357
+
349
358
xccdf_test_result_type_t sce_engine_eval_rule (struct xccdf_policy * policy , const char * rule_id , const char * id , const char * href ,
350
359
struct xccdf_value_binding_iterator * value_binding_it ,
351
360
struct xccdf_check_import_iterator * check_import_it ,
@@ -390,6 +399,7 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
390
399
// bound values in KEY=VALUE form, ready to be passed as environment variables
391
400
char * * env_values = malloc (10 * sizeof (char * ));
392
401
size_t env_value_count = 10 ;
402
+ const size_t index_of_first_env_value_not_compiled_in = 10 ;
393
403
394
404
env_values [0 ] = "PATH=/bin:/sbin:/usr/bin:/usr/sbin" ;
395
405
@@ -488,12 +498,7 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
488
498
if (pipe (stdout_pipefd ) == -1 || pipe (stderr_pipefd ) == -1 )
489
499
{
490
500
perror ("pipe" );
491
- // the first 9 values (0 to 8) are compiled in
492
- for (size_t i = 9 ; i < env_value_count ; ++ i )
493
- {
494
- free (env_values [i ]);
495
- }
496
- free (env_values );
501
+ free_env_values (env_values , index_of_first_env_value_not_compiled_in , env_value_count );
497
502
return XCCDF_RESULT_ERROR ;
498
503
}
499
504
@@ -507,9 +512,11 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
507
512
508
513
if (fork_result == 0 )
509
514
{
510
- // we won't read from the pipes, so close the reading fd
511
- close (stdout_pipefd [0 ]);
512
- close (stderr_pipefd [0 ]);
515
+ free_env_values (env_values , index_of_first_env_value_not_compiled_in , env_value_count );
516
+
517
+ // we won't read from the pipes, so close the reading fd
518
+ close (stdout_pipefd [0 ]);
519
+ close (stderr_pipefd [0 ]);
513
520
514
521
// forward stdout and stderr to our custom opened pipes
515
522
dup2 (stdout_pipefd [1 ], fileno (stdout ));
@@ -550,13 +557,15 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
550
557
if (flag_stdout == -1 ) {
551
558
oscap_seterr (OSCAP_EFAMILY_SCE , "Failed to obtain status of stdout pipe: %s" ,
552
559
strerror (errno ));
560
+ free_env_values (env_values , index_of_first_env_value_not_compiled_in , env_value_count );
553
561
return XCCDF_RESULT_ERROR ;
554
562
}
555
563
int retval = fcntl (stdout_pipefd [0 ], F_SETFL , flag_stdout | O_NONBLOCK );
556
564
if (retval == -1 ) {
557
565
oscap_seterr (OSCAP_EFAMILY_SCE ,
558
566
"Failed to set nonblocking flag on stdout pipe: %s" ,
559
567
strerror (errno ));
568
+ free_env_values (env_values , index_of_first_env_value_not_compiled_in , env_value_count );
560
569
return XCCDF_RESULT_ERROR ;
561
570
}
562
571
@@ -565,13 +574,15 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
565
574
oscap_seterr (OSCAP_EFAMILY_SCE ,
566
575
"Failed to obtain status of stderr pipe: %s" ,
567
576
strerror (errno ));
577
+ free_env_values (env_values , index_of_first_env_value_not_compiled_in , env_value_count );
568
578
return XCCDF_RESULT_ERROR ;
569
579
}
570
580
retval = fcntl (stderr_pipefd [0 ], F_SETFL , flag_stderr | O_NONBLOCK );
571
581
if (retval == -1 ) {
572
582
oscap_seterr (OSCAP_EFAMILY_SCE ,
573
583
"Failed to set nonblocking flag on stderr pipe: %s" ,
574
584
strerror (errno ));
585
+ free_env_values (env_values , index_of_first_env_value_not_compiled_in , env_value_count );
575
586
return XCCDF_RESULT_ERROR ;
576
587
}
577
588
@@ -630,12 +641,7 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
630
641
sce_session_add_check_result (session , check_result );
631
642
}
632
643
633
- // the first 10 values (0 to 9) are compiled in
634
- for (size_t i = 10 ; i < env_value_count ; ++ i )
635
- {
636
- free (env_values [i ]);
637
- }
638
- free (env_values );
644
+ free_env_values (env_values , index_of_first_env_value_not_compiled_in , env_value_count );
639
645
640
646
// lets interpret the check imports passed to us
641
647
xccdf_check_import_iterator_reset (check_import_it );
@@ -663,12 +669,7 @@ xccdf_test_result_type_t sce_engine_eval_rule(struct xccdf_policy *policy, const
663
669
}
664
670
else
665
671
{
666
- // the first 9 values (0 to 8) are compiled in
667
- for (size_t i = 9 ; i < env_value_count ; ++ i )
668
- {
669
- free (env_values [i ]);
670
- }
671
- free (env_values );
672
+ free_env_values (env_values , index_of_first_env_value_not_compiled_in , env_value_count );
672
673
673
674
close (stdout_pipefd [0 ]);
674
675
close (stdout_pipefd [1 ]);
0 commit comments