]> git.kaiwu.me - njs.git/commitdiff
Fixed realloc() failure handling.
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 26 Sep 2017 11:19:49 +0000 (14:19 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 26 Sep 2017 11:19:49 +0000 (14:19 +0300)
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

index d6fe03c74f770c12c02880de0389e67aac2f0dc6..cff179e8ebcefe00f44e455a74ae3a5933720bf7 100644 (file)
--- 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;
         }