diff mbox series

[5/5] libsemanage: avoid double fclose

Message ID 20220408131054.7957-5-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit e205e3e84a87
Headers show
Series [1/5] libsepol/cil: declare file local function pointer static | expand

Commit Message

Christian Göttsche April 8, 2022, 1:10 p.m. UTC
The cleanup goto block in `semanage_direct_set_enabled()` closes the
file stream pointer fp if not NULL.  Set the stream to NULL after a
manual fclose(3), even on failure.

    direct_api.c: In function ‘semanage_direct_set_enabled’:
    direct_api.c:2130:25: error: pointer ‘fp’ may be used after ‘fclose’ [-Werror=use-after-free]
     2130 |         if (fp != NULL) fclose(fp);
          |                         ^~~~~~~~~~
    direct_api.c:2092:29: note: call to ‘fclose’ here
     2092 |                         if (fclose(fp) != 0) {
          |                             ^~~~~~~~~~

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libsemanage/src/direct_api.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libsemanage/src/direct_api.c b/libsemanage/src/direct_api.c
index d5716ce5..7206483a 100644
--- a/libsemanage/src/direct_api.c
+++ b/libsemanage/src/direct_api.c
@@ -2089,7 +2089,9 @@  static int semanage_direct_set_enabled(semanage_handle_t *sh,
 				goto cleanup;
 			}
 
-			if (fclose(fp) != 0) {
+			ret = fclose(fp);
+			fp = NULL;
+			if (ret != 0) {
 				ERR(sh,
 				    "Unable to close disabled file for module %s",
 				    modkey->name);
@@ -2097,8 +2099,6 @@  static int semanage_direct_set_enabled(semanage_handle_t *sh,
 				goto cleanup;
 			}
 
-			fp = NULL;
-
 			break;
 		case 1: /* enable the module */
 			if (unlink(fn) < 0) {