]> git.kaiwu.me - njs.git/commitdiff
Fixed fd leakage in njs_process_file().
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 1 Sep 2017 15:51:20 +0000 (18:51 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 1 Sep 2017 15:51:20 +0000 (18:51 +0300)
Initially, njs_process_file() was not designed to be invoked multiple
times. Making it reusable by releasing the resources used.

njs/njs.c

index d82c994cc709464fd602636f496bd495d4ce3147..5670e0f0d6e7ca814ec0d02044cd8c09fb5d6b24 100644 (file)
--- a/njs/njs.c
+++ b/njs/njs.c
@@ -335,7 +335,8 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options)
     script.start = realloc(NULL, size);
     if (script.start == NULL) {
         fprintf(stderr, "alloc failed while reading '%s'\n", file);
-        return NXT_ERROR;
+        ret = NXT_ERROR;
+        goto done;
     }
 
     p = script.start;
@@ -351,7 +352,8 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options)
         if (n < 0) {
             fprintf(stderr, "failed to read file: '%s' (%s)\n",
                     file, strerror(errno));
-            return NXT_ERROR;
+            ret = NXT_ERROR;
+            goto done;
         }
 
         if (p + n > end) {
@@ -360,7 +362,8 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options)
             script.start = realloc(script.start, size);
             if (script.start == NULL) {
                 fprintf(stderr, "alloc failed while reading '%s'\n", file);
-                return NXT_ERROR;
+                ret = NXT_ERROR;
+                goto done;
             }
 
             p = script.start + script.length;
@@ -376,13 +379,15 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options)
     vm = njs_vm_create(vm_options);
     if (vm == NULL) {
         fprintf(stderr, "failed to create vm\n");
-        return NXT_ERROR;
+        ret = NXT_ERROR;
+        goto done;
     }
 
     ret = njs_process_script(vm, opts, &script, &out);
     if (ret != NXT_OK) {
         fprintf(stderr, "failed to get retval from VM\n");
-        return NXT_ERROR;
+        ret = NXT_ERROR;
+        goto done;
     }
 
     if (!opts->disassemble) {
@@ -394,7 +399,19 @@ njs_process_file(njs_opts_t *opts, njs_vm_opt_t *vm_options)
         }
     }
 
-    return NXT_OK;
+    ret = NXT_OK;
+
+done:
+
+    if (script.start != NULL) {
+        free(script.start);
+    }
+
+    if (fd != STDIN_FILENO) {
+        close(fd);
+    }
+
+    return ret;
 }