From: Dmitry Volyntsev Date: Tue, 26 Sep 2017 11:19:49 +0000 (+0300) Subject: Fixed realloc() failure handling. X-Git-Tag: 0.1.14~8 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=b226f15a70bc4388049556e691559f26332782e1;p=njs.git 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. --- 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; }