1    	/*
2    	 * Copyright (C) 2018-2026 Red Hat, Inc.  All rights reserved.
3    	 *
4    	 * Author: Fabio M. Di Nitto <fabbione@kronosnet.org>
5    	 *
6    	 * This software licensed under GPL-2.0+
7    	 */
8    	
9    	#include "config.h"
10   	
11   	#include <string.h>
12   	#include <stdio.h>
13   	#include <stdlib.h>
14   	#include <unistd.h>
15   	#include <errno.h>
16   	#include <stdint.h>
17   	#include <limits.h>
18   	#include <sys/socket.h>
19   	
20   	#include "test-common.h"
21   	
22   	static int test(void)
23   	{
24   		int err = 0;
25   		char *error_string = NULL;
26   	
27   		printf("Testing execute_bin_sh_command\n");
28   	
29   		printf("command true\n");
(1) Event alloc_arg: "execute_bin_sh_command" allocates memory that is stored into "error_string". [details]
(2) Event path: Condition "(_foe_res = execute_bin_sh_command("true", &error_string)) != 0", taking true branch.
(3) Event path: Jumping to label "out_clean".
Also see events: [leaked_storage]
30   		FAIL_ON_ERR(execute_bin_sh_command("true", &error_string));
31   		if (error_string) {
32   			printf("Error string: %s\n", error_string);
33   			free(error_string);
34   			error_string = NULL;
35   		}
36   	
37   		printf("command false\n");
38   		FAIL_ON_CMD_SUCCESS(err, execute_bin_sh_command("false", &error_string), error_string, "Can we really execute false successfully?!?!");
39   	
40   		printf("command that outputs to stdout (enforcing redirect)\n");
41   		FAIL_ON_CMD_SUCCESS(err, execute_bin_sh_command("grep -h 2>&1", &error_string), error_string, "Can we really execute grep -h successfully?!?");
42   	
43   		printf("command that outputs to stderr\n");
44   		FAIL_ON_CMD_SUCCESS(err, execute_bin_sh_command("grep -h", &error_string), error_string, "Can we really execute grep -h successfully?!?");
45   	
46   		printf("Testing ERROR conditions\n");
47   	
48   		printf("empty command\n");
49   		FAIL_ON_SUCCESS(execute_bin_sh_command(NULL, &error_string), EINVAL);
50   		if (error_string) {
51   			printf("Error string: %s\n", error_string);
52   			free(error_string);
53   			error_string = NULL;
54   		}
55   	
56   		printf("empty error\n");
57   		FAIL_ON_SUCCESS(execute_bin_sh_command("true", NULL), EINVAL);
58   	
59   		err = 0;
60   	
61   	out_clean:
62   	
CID (unavailable; MK=c066f7a4b403e6c5058a4a8b51d2bdf7) (#1 of 1): Resource leak (RESOURCE_LEAK):
(4) Event leaked_storage: Variable "error_string" going out of scope leaks the storage it points to.
Also see events: [alloc_arg]
63   		return err;
64   	}
65   	
66   	int main(void)
67   	{
68   		need_root();
69   	
70   		if (test() < 0)
71   			return FAIL;
72   	
73   		return PASS;
74   	}
75