Skip to content

Commit

Permalink
fixed memory leak due to use popt incorrectly
Browse files Browse the repository at this point in the history
Signed-off-by: xuraoqing <[email protected]>
  • Loading branch information
xuraoqing authored and xuraoqing committed Sep 19, 2024
1 parent f83ea91 commit ea8bb02
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/sss_client/autofs/autofs_test_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ int main(int argc, const char *argv[])
exit(EXIT_FAILURE);
}

poptFreeContext(pc);

requested_protocol = pc_protocol;
protocol = _sss_auto_protocol_version(requested_protocol);
if (protocol != requested_protocol) {
fprintf(stderr, "Unsupported protocol version: %u -> %u\n",
requested_protocol, protocol);
poptFreeContext(pc);
exit(EXIT_FAILURE);
}

ret = _sss_setautomntent(mapname, &ctx);
if (ret) {
fprintf(stderr, "setautomntent failed [%d]: %s\n",
ret, strerror(ret));
poptFreeContext(pc);
exit(EXIT_FAILURE);
}
printf("setautomntent done for %s\n", mapname);
Expand Down Expand Up @@ -141,8 +141,10 @@ int main(int argc, const char *argv[])
if (ret) {
fprintf(stderr, "endautomntent failed [%d]: %s\n",
ret, strerror(ret));
poptFreeContext(pc);
exit(EXIT_FAILURE);
}
printf("endautomntent done for %s\n", mapname);
poptFreeContext(pc);
return 0;
}
41 changes: 27 additions & 14 deletions src/tests/test_ssh_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ int main(int argc, const char *argv[])
char *av[3];
char buf[5]; /* Ridiculously small buffer by design */
ssize_t len;
int return_code = 0;

/* Set debug level to invalid value so we can decide if -d 0 was used. */
debug_level = SSSDBG_INVALID;
Expand All @@ -55,18 +56,18 @@ int main(int argc, const char *argv[])
fprintf(stderr, "\nInvalid option %s: %s\n\n",
poptBadOption(pc, 0), poptStrerror(opt));
poptPrintUsage(pc, stderr, 0);
return 3;
return_code = 3;
goto end;
}
}

pc_user = poptGetArg(pc);
if (pc_user == NULL) {
fprintf(stderr, "No user specified\n");
return 3;
return_code = 3;
goto end;
}

poptFreeContext(pc);

DEBUG_CLI_INIT(debug_level);

ret = stat(SSH_AK_CLIENT_PATH, &sb);
Expand All @@ -75,13 +76,15 @@ int main(int argc, const char *argv[])
DEBUG(SSSDBG_CRIT_FAILURE,
"Could not stat %s [%d]: %s\n",
SSH_AK_CLIENT_PATH, ret, strerror(ret));
return 3;
return_code = 3;
goto end;
}

ret = pipe(p);
if (ret != 0) {
perror("pipe");
return 3;
return_code = 3;
goto end;
}

switch (pid = fork()) {
Expand All @@ -90,7 +93,8 @@ int main(int argc, const char *argv[])
close(p[0]);
close(p[1]);
DEBUG(SSSDBG_CRIT_FAILURE, "fork failed: %d\n", ret);
return 3;
return_code = 3;
goto end;
case 0:
/* child */
av[0] = discard_const(SSH_AK_CLIENT_PATH);
Expand All @@ -101,11 +105,13 @@ int main(int argc, const char *argv[])
ret = dup2(p[1], STDOUT_FILENO);
if (ret == -1) {
perror("dup2");
return 3;
return_code = 3;
goto end;
}

execv(av[0], av);
return 3;
return_code = 3;
goto end;
default:
/* parent */
break;
Expand All @@ -116,23 +122,30 @@ int main(int argc, const char *argv[])
close(p[0]);
if (len == -1) {
perror("waitpid");
return 3;
return_code = 3;
goto end;
}

pid = waitpid(pid, &status, 0);
if (pid == -1) {
perror("waitpid");
return 3;
return_code = 3;
goto end;
}

if (WIFEXITED(status)) {
printf("sss_ssh_authorizedkeys exited with return code %d\n", WEXITSTATUS(status));
return 0;
return_code = 0;
goto end;
} else if (WIFSIGNALED(status)) {
printf("sss_ssh_authorizedkeys exited with signal %d\n", WTERMSIG(status));
return 1;
return_code = 1;
goto end;
}

printf("sss_ssh_authorizedkeys exited for another reason\n");
return 2;
return_code = 2;
end:
poptFreeContext(pc);
return return_code;
}

0 comments on commit ea8bb02

Please sign in to comment.