Merge pull request #198 from Smilex/mem_fix

Fixed cases where file would be opened, but not closed.
This commit is contained in:
Petri Lehtinen 2014-07-21 08:09:18 +03:00
commit abd151f5d7

View File

@ -181,6 +181,7 @@ int use_conf(char *test_path)
if (conf.indent < 0 || conf.indent > 31) { if (conf.indent < 0 || conf.indent > 31) {
fprintf(stderr, "invalid value for JSON_INDENT: %d\n", conf.indent); fprintf(stderr, "invalid value for JSON_INDENT: %d\n", conf.indent);
fclose(infile);
return 2; return 2;
} }
if (conf.indent) if (conf.indent)
@ -201,6 +202,7 @@ int use_conf(char *test_path)
if (conf.precision < 0 || conf.precision > 31) { if (conf.precision < 0 || conf.precision > 31) {
fprintf(stderr, "invalid value for JSON_REAL_PRECISION: %d\n", fprintf(stderr, "invalid value for JSON_REAL_PRECISION: %d\n",
conf.precision); conf.precision);
fclose(infile);
return 2; return 2;
} }
if (conf.precision) if (conf.precision)
@ -303,17 +305,19 @@ int use_env()
if(getenv_int("STRIP")) { if(getenv_int("STRIP")) {
/* Load to memory, strip leading and trailing whitespace */ /* Load to memory, strip leading and trailing whitespace */
size_t size = 0, used = 0; size_t size = 0, used = 0;
char *buffer = NULL; char *buffer = NULL, *buf_ck = NULL;
while(1) { while(1) {
size_t count; size_t count;
size = (size == 0 ? 128 : size * 2); size = (size == 0 ? 128 : size * 2);
buffer = realloc(buffer, size); buf_ck = realloc(buffer, size);
if(!buffer) { if(!buf_ck) {
fprintf(stderr, "Unable to allocate %d bytes\n", (int)size); fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
free(buffer);
return 1; return 1;
} }
buffer = buf_ck;
count = fread(buffer + used, 1, size - used, stdin); count = fread(buffer + used, 1, size - used, stdin);
if(count < size - used) { if(count < size - used) {