From b226f15a70bc4388049556e691559f26332782e1 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Tue, 26 Sep 2017 14:19:49 +0300 Subject: [PATCH] Fixed realloc() failure handling. According to POSIX, if realloc() fails to allocate a new chunk of memory it returns NULL and does not free the original pointer. Using a separate pointer in order to preserve the original one. --- njs/njs.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/njs/njs.c b/njs/njs.c index d6fe03c7..cff179e8 100644 --- a/njs/njs.c +++ b/njs/njs.c @@ -300,7 +300,7 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options) { int fd; char *file; - u_char buf[4096], *p, *end; + u_char buf[4096], *p, *end, *start; size_t size; ssize_t n; njs_vm_t *vm; @@ -364,13 +364,15 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options) if (p + n > end) { size *= 2; - script.start = realloc(script.start, size); - if (script.start == NULL) { + start = realloc(script.start, size); + if (start == NULL) { fprintf(stderr, "alloc failed while reading '%s'\n", file); ret = NXT_ERROR; goto done; } + script.start = start; + p = script.start + script.length; end = script.start + size; } -- 2.47.3