module_to_cil: fix use of uninitialized variables
PATCH v2 1/1 module_to_cil: fix use of uninitialized variables
From: William Roberts <[email protected]>
Correct errors like these reported by gcc:
module_to_cil.c: In function ‘block_to_cil’:
module_to_cil.c:229:20: error: ‘attr_list’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
struct list_node *curr = (*attr_list)->head;
Usages of attr_list_destroy() were called when list_init() fails. Just bail early on failure.
stack_init() and stack_destroy() also suffered from the aforementioned issue.
--- libsepol/src/module_to_cil.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
libsepol/src/module_to_cil.c +4 -4
@@ -1307,7 +1307,7 @@ static int cond_list_to_cil(int indent, struct policydb *pdb, struct cond_node * rc =list_init (&attr_list);if (rc !=0 ) { - goto exit; +return rc; }for (cond = cond_list; cond !=NULL ; cond = cond->next) {
@@ -3488,7 +3488,7 @@ static int block_to_cil(struct policydb *pdb, struct avrule_block *block, struct rc =list_init (&attr_list);if (rc !=0 ) { - goto exit; +return rc; } rc =typealiases_to_cil (indent, pdb, block, stack);
@@ -3635,7 +3635,7 @@ static int blocks_to_cil(struct policydb *pdb) rc =stack_init (&stack);if (rc !=0 ) { - goto exit; +return rc; } block = pdb->global;
@@ -3703,7 +3703,7 @@ static int linked_blocks_to_cil(struct policydb *pdb) rc =stack_init (&stack);if (rc !=0 ) { - goto exit; +return rc; } block = pdb->global;
Signature · 1 line
1.9.1
Quoted 52 lines · On 08/03/2016 05:42 PM, [email protected] wrote:
From: William Roberts <[email protected]>
Correct errors like these reported by gcc:
module_to_cil.c: In function ‘block_to_cil’:
module_to_cil.c:229:20: error: ‘attr_list’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
structlist_node*curr = (*attr_list)->head;Usages of
attr_list_destroy()were called whenlist_init()fails. Just bail early on failure.
stack_init()andstack_destroy()also suffered from the aforementioned issue.Signed-off-by William Roberts <[email protected]>--- libsepol/src/module_to_cil.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)libsepol/src/module_to_cil.c +3
-3@@ -1307,7 +1307,7 @@ static int cond_list_to_cil(int indent, struct policydb *pdb, struct cond_node * rc =list_init (&attr_list);if (rc !=0 ) { - goto exit; +return rc; }for (cond = cond_list; cond !=NULL ; cond = cond->next) {@@ -3488,7 +3488,7 @@ static int block_to_cil(struct policydb *pdb, struct avrule_block *block, struct rc =list_init (&attr_list);if (rc !=0 ) { - goto exit; +return rc; } rc =typealiases_to_cil (indent, pdb, block, stack);@@ -3635,7 +3635,7 @@ static int blocks_to_cil(struct policydb *pdb) rc =stack_init (&stack);if (rc !=0 ) { - goto exit; +return rc; } block = pdb->global;
libsepol/src/module_to_cil.c static int linked_blocks_to_cil(struct policydb *pdb) · line 3709
Quoted 8 lines
libsepol/src/module_to_cil.c +1
-1@@ -3703,7 +3703,7 @@ static int linked_blocks_to_cil(struct policydb *pdb) rc =stack_init (&stack);if (rc !=0 ) { - goto exit; +return rc; } block = pdb->global;
I would recommend just initializing the variables to NULL and keeping the "goto exit"'s. That would maintain the single return point, allows for extra cleanup code to be run in the future if necessary, and is consistent with the rest of the module_to_cil code.
- Steve
Quoted 72 lines
-----Original Message-----
From: Steve Lawrence [mailto:[email protected]]
Sent: Wednesday, August 3, 2016 2:51 PM
To: Roberts, William C <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2]module_to_cil: fix use of uninitialized variablesQuoted 6 lines · On 08/03/2016 05:42 PM, [email protected] wrote:
From: William Roberts <[email protected]>
Correct errors like these reported by gcc:
module_to_cil.c: In function ‘block_to_cil’:
module_to_cil.c:229:20: error: ‘attr_list’ may be used uninitialized in thisfunction [-Werror=maybe-uninitialized]
Quoted 55 lines
struct list_node *curr = (*attr_list)->head;Usages of
attr_list_destroy()were called whenlist_init()fails. Just bail early on failure.
stack_init()andstack_destroy()also suffered from the aforementioned issue.Signed-off-by William Roberts <[email protected]>--- libsepol/src/module_to_cil.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)(quoted diff) +0
-0
b/libsepol/src/module_to_cil.cindex b9a4af7..9d0d064 100644libsepol/src/module_to_cil.c +0
-0@@ -1307,7 +1307,7 @@ static int cond_list_to_cil(int indent, structpolicydb *pdb, struct
cond_node*libsepol/src/module_to_cil.c +2
-1rc =list_init (&attr_list);if (rc !=0 ) { - goto exit; +return rc; } for (cond = cond_list; cond != NULL; cond = cond->next) { @@ -3488,7 +3488,7 @@ static int block_to_cil(struct policydb *pdb, struct
avrule_block*block, structlibsepol/src/module_to_cil.c +4
-3rc =list_init (&attr_list);if (rc !=0 ) { - goto exit; +return rc; } rc = typealiases_to_cil(indent, pdb, block, stack); @@ -3635,7 +3635,7 @@ static int blocks_to_cil(struct policydb *pdb) rc =stack_init (&stack);if (rc !=0 ) { - goto exit; +return rc; } block = pdb->global;@@ -3703,7 +3703,7 @@ static int linked_blocks_to_cil(struct policydb *pdb) rc =stack_init (&stack);if (rc !=0 ) { - goto exit; +return rc; }
Quoted 7 lines
Quoted 2 lines
block =
pdb->global;I would recommend just initializing the variables to NULL and keeping the "goto exit"'s. That would maintain the single return point, allows for extra cleanup code to be run in the future if necessary, and is consistent with the rest of the
module_to_cilcode.
That's fine, but requiring only one exit point isn't consistent with the rest of libsepol, some do it this way, some don’t. There will be slightly more overhead In the approach of initializing it first and then jumping on failure, then checking if its NULL in the destruction routine, then finally returning versus just checking the init routine and returning.
Quoted 3 lines
- Steve
<snip>
Quoted 6 lines
I would recommend just initializing the variables to NULL and keeping the "goto exit"'s. That would maintain the single return point, allows for extra cleanup code to be run in the future if necessary, and is consistent with the rest of the
module_to_cilcode.
FYI these functions have multiple return paths, so that statement isn't even true for module_to_cil code:
roles_gather_map()typealiases_gather_map()stack_destroy()is_id_in_scope_with_start()
Functions that always return 0:semantic_level_to_cil()cil_print_attr_strs()
Some functions have double sets to -1 on rc, the compiler should be smart enough to disregard those, but
still, no consistency on when/where rc = -1 is set.
ebitmap_to_names()
I only went through 36% of the file and noticed all these.
On a separate note, Coverity has free scanning for open source projects, perhaps we should put selinux project in there
and get it scanning. It will likely pick up a lot of stuff.