Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in cmd parser #404

Merged
merged 2 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/iobroker.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ struct iobroker_set *iobroker_create(void)
}

iobs->max_fds = iobroker_max_usable_fds();
/* add sane max limit, if ulimit is set to unlimited or a very high value we
* don't want to waste memory for nothing */
if (iobs->max_fds > MAX_FD_LIMIT)
iobs->max_fds = MAX_FD_LIMIT;
iobs->iobroker_fds = calloc(iobs->max_fds, sizeof(iobroker_fd *));
if (!iobs->iobroker_fds) {
goto error_out;
Expand Down
5 changes: 5 additions & 0 deletions lib/lnae-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
# define veclen ARRAY_SIZE
#endif

/* sets a limit for max open files if ulimit is set to unlimited or a unusual high value */
#ifndef MAX_FD_LIMIT
#define MAX_FD_LIMIT 100000
#endif

#ifndef offsetof
/** standard offsetof macro */
# define offsetof(t, f) ((unsigned long)&((t *)0)->f)
Expand Down
12 changes: 11 additions & 1 deletion lib/runcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ static int maxfd = 0;
# endif /* _SC_OPEN_MAX */
#endif /* OPEN_MAX */


const char *runcmd_strerror(int code)
{
switch (code) {
Expand Down Expand Up @@ -140,6 +139,7 @@ int runcmd_cmd2strv(const char *str, int *out_argc, char **out_argv, int *out_en
len = strlen(str);
envz = malloc(len + 1);
*out_envc = env;
out_env[0] = envz;
for (i = 0; i < len && continue_env_parsing; i++) {
const char *p = &str[i];

Expand Down Expand Up @@ -423,6 +423,11 @@ void runcmd_init(void)
}
#endif

/* add sane max limit, if ulimit is set to unlimited or a very high value we
* don't want to waste memory for nothing */
if (maxfd > MAX_FD_LIMIT)
maxfd = MAX_FD_LIMIT;

/* reset pipe handling so child processes can use shell pipes */
signal(SIGPIPE, SIG_DFL);

Expand Down Expand Up @@ -472,6 +477,8 @@ int runcmd_open(const char *cmd, int *pfd, int *pfderr)
argv[2] = strdup(cmd);
if (!argv[2]) {
free(argv);
free(env[0]);
free(env);
return RUNCMD_EALLOC;
}
argv[3] = NULL;
Expand Down Expand Up @@ -549,6 +556,8 @@ int runcmd_open(const char *cmd, int *pfd, int *pfderr)
free(argv[0]);
else
free(argv[2]);
free(env[0]);
free(env);
_exit(errno);
}

Expand All @@ -565,6 +574,7 @@ int runcmd_open(const char *cmd, int *pfd, int *pfderr)
else
free(argv[2]);
free(argv);
free(env[0]);
free(env);

/* tag our file's entry in the pid-list and return it */
Expand Down