diff mbox series

[15/25] libselinux: getdefaultcon: free memory on multiple same arguments

Message ID 20210503175350.55954-16-cgzones@googlemail.com (mailing list archive)
State Accepted
Headers show
Series libselinux: misc compiler and static analyzer findings | expand

Commit Message

Christian Göttsche May 3, 2021, 5:53 p.m. UTC
Do not leak memory if program arguments get specified more than once.

Found by clang-anlyzer.

getdefaultcon.c:52:3: warning: Potential leak of memory pointed to by 'level' [unix.Malloc]
                fprintf(stderr,
                ^~~~~~~~~~~~~~~
getdefaultcon.c:52:3: warning: Potential leak of memory pointed to by 'role' [unix.Malloc]
                fprintf(stderr,
                ^~~~~~~~~~~~~~~
getdefaultcon.c:52:3: warning: Potential leak of memory pointed to by 'service' [unix.Malloc]
                fprintf(stderr,
                ^~~~~~~~~~~~~~~

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libselinux/utils/getdefaultcon.c | 3 +++
 1 file changed, 3 insertions(+)
diff mbox series

Patch

diff --git a/libselinux/utils/getdefaultcon.c b/libselinux/utils/getdefaultcon.c
index 96a5a8c2..957c1cb2 100644
--- a/libselinux/utils/getdefaultcon.c
+++ b/libselinux/utils/getdefaultcon.c
@@ -28,12 +28,15 @@  int main(int argc, char **argv)
 	while ((opt = getopt(argc, argv, "l:r:s:v")) > 0) {
 		switch (opt) {
 		case 'l':
+			free(level);
 			level = strdup(optarg);
 			break;
 		case 'r':
+			free(role);
 			role = strdup(optarg);
 			break;
 		case 's':
+			free(service);
 			service = strdup(optarg);
 			break;
 		case 'v':