Bug 587670 Process leaks file if it is a tty and it hits a line error or a buffer error. r=jorendorff.
authortimeless@mozdev.org
Tue, 07 Dec 2010 12:53:43 -0600
changeset 58984 9df99367ef2b306c4465174e5b9ab8fdf7675dc7
parent 58983 3525f819579259736440a653ece333b463afd50b
child 58985 06902afe3a585f846860f4a635b4bc7fb9a17449
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersjorendorff
bugs587670
milestone2.0b8pre
Bug 587670 Process leaks file if it is a tty and it hits a line error or a buffer error. r=jorendorff.
js/src/shell/js.cpp
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -453,19 +453,17 @@ Process(JSContext *cx, JSObject *obj, ch
                 (void)JS_ExecuteScript(cx, obj, script, NULL);
                 int64 t2 = PRMJ_Now() - t1;
                 if (printTiming)
                     printf("runtime = %.3f ms\n", double(t2) / PRMJ_USEC_PER_MSEC);
             }
             JS_DestroyScript(cx, script);
         }
 
-        if (file != stdin)
-            fclose(file);
-        return;
+        goto cleanup;
     }
 
     /* It's an interactive filehandle; drop into read-eval-print loop. */
     lineno = 1;
     hitEOF = JS_FALSE;
     buffer = NULL;
     size = 0;           /* assign here to avoid warnings */
     do {
@@ -486,17 +484,17 @@ Process(JSContext *cx, JSObject *obj, ch
             {
                 JSAutoSuspendRequest suspended(cx);
                 line = GetLine(file, startline == lineno ? "js> " : "");
             }
             if (!line) {
                 if (errno) {
                     JS_ReportError(cx, strerror(errno));
                     free(buffer);
-                    return;
+                    goto cleanup;
                 }
                 hitEOF = JS_TRUE;
                 break;
             }
             if (!buffer) {
                 buffer = line;
                 len = strlen(buffer);
                 size = len + 1;
@@ -507,17 +505,17 @@ Process(JSContext *cx, JSObject *obj, ch
                 size_t newlen = strlen(line) + (len ? len + 1 : 0);
                 if (newlen + 1 > size) {
                     size = newlen + 1 > size * 2 ? newlen + 1 : size * 2;
                     char *newBuf = (char *) realloc(buffer, size);
                     if (!newBuf) {
                         free(buffer);
                         free(line);
                         JS_ReportOutOfMemory(cx);
-                        return;
+                        goto cleanup;
                     }
                     buffer = newBuf;
                 }
                 char *current = buffer + len;
                 if (startline != lineno)
                     *current++ = '\n';
                 strcpy(current, line);
                 len = newlen;
@@ -561,16 +559,17 @@ Process(JSContext *cx, JSObject *obj, ch
             }
             JS_DestroyScript(cx, script);
         }
         *buffer = '\0';
     } while (!hitEOF && !gQuitting);
 
     free(buffer);
     fprintf(gOutFile, "\n");
+cleanup:
     if (file != stdin)
         fclose(file);
     return;
 }
 
 static int
 usage(void)
 {