[njs] Fixed realloc() failure handling.

Dmitry Volyntsev xeioex at nginx.com
Tue Sep 26 11:20:06 UTC 2017


details:   http://hg.nginx.org/njs/rev/f6b9efd315c5
branches:  
changeset: 409:f6b9efd315c5
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Sep 26 14:19:49 2017 +0300
description:
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.

diffstat:

 njs/njs.c |  8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)

diffs (30 lines):

diff -r 616370bb1386 -r f6b9efd315c5 njs/njs.c
--- a/njs/njs.c	Fri Sep 01 18:51:20 2017 +0300
+++ b/njs/njs.c	Tue Sep 26 14:19:49 2017 +0300
@@ -300,7 +300,7 @@ njs_process_file(njs_opts_t *opts, njs_v
 {
     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_v
         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;
         }


More information about the nginx-devel mailing list