libselinux: getdefaultcon: free memory on multiple same arguments

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>
This commit is contained in:
Christian Göttsche 2021-05-03 17:11:07 +02:00 committed by Petr Lautrbach
parent 411c5a5458
commit db69a3d362

View file

@ -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':