Welcome! Log In Create A New Profile

Advanced

[njs] Shell: fixed potential memory leak in main().

Dmitry Volyntsev
March 25, 2019 09:00AM
details: https://hg.nginx.org/njs/rev/3aa52df685bc
branches:
changeset: 840:3aa52df685bc
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Mon Mar 25 15:41:21 2019 +0300
description:
Shell: fixed potential memory leak in main().

diffstat:

njs/njs_shell.c | 19 ++++++++++++-------
1 files changed, 12 insertions(+), 7 deletions(-)

diffs (65 lines):

diff -r 81303df0fd67 -r 3aa52df685bc njs/njs_shell.c
--- a/njs/njs_shell.c Sat Mar 23 16:39:40 2019 +0300
+++ b/njs/njs_shell.c Mon Mar 25 15:41:21 2019 +0300
@@ -209,12 +209,14 @@ main(int argc, char **argv)

ret = njs_get_options(&opts, argc, argv);
if (ret != NXT_OK) {
- return (ret == NXT_DONE) ? EXIT_SUCCESS : EXIT_FAILURE;
+ ret = (ret == NXT_DONE) ? NXT_OK : NXT_ERROR;
+ goto done;
}

if (opts.version != 0) {
printf("%s\n", NJS_VERSION);
- return EXIT_SUCCESS;
+ ret = NXT_OK;
+ goto done;
}

nxt_memzero(&vm_options, sizeof(njs_vm_opt_t));
@@ -224,7 +226,8 @@ main(int argc, char **argv)
p = getcwd(path, sizeof(path));
if (p == NULL) {
fprintf(stderr, "getcwd() failed:%s\n", strerror(errno));
- return EXIT_FAILURE;
+ ret = NXT_ERROR;
+ goto done;
}

memcpy(path + strlen(path), "/shell", sizeof("/shell"));
@@ -249,6 +252,8 @@ main(int argc, char **argv)
ret = njs_process_file(&opts, &vm_options);
}

+done:
+
if (opts.paths != NULL) {
free(opts.paths);
}
@@ -260,7 +265,7 @@ main(int argc, char **argv)
static nxt_int_t
njs_get_options(njs_opts_t *opts, int argc, char** argv)
{
- char *p;
+ char *p, **paths;
nxt_int_t i, ret;

static const char help[] =
@@ -309,13 +314,13 @@ njs_get_options(njs_opts_t *opts, int ar
case 'p':
if (argv[++i] != NULL) {
opts->n_paths++;
- opts->paths = realloc(opts->paths,
- opts->n_paths * sizeof(char *));
- if (opts->paths == NULL) {
+ paths = realloc(opts->paths, opts->n_paths * sizeof(char *));
+ if (paths == NULL) {
fprintf(stderr, "failed to add path\n");
return NXT_ERROR;
}

+ opts->paths = paths;
opts->paths[opts->n_paths - 1] = argv[i];
break;
}
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Shell: fixed potential memory leak in main().

Dmitry Volyntsev 292 March 25, 2019 09:00AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 150
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready