Fix memory leaks and double frees (#3487)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by itchyny · committed by GitHub · parent 2ad99a6 · Browse files · 6 files·+30 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+30 added−20 removed
- other:+30 added−20 removed
Provenance
- Commit
750dbe7- Tree
dc3d6ab- Author
itchyny <itchyny@cybozu.co.jp>- Committer
GitHub <noreply@github.com>- Parent
2ad99a6
6 files changed+30 added−20 removed
modified:src/execute.c+16 added-6 removed
920 unmodified lines921 922 if (!jv_is_valid(top)) {923 if (jv_invalid_has_msg(jv_copy(top)))924 set_error(jq, top);925+ else926+ jv_free(top);927 goto do_backtrack;928 }929 930 stack_push(jq, top);90 unmodified lines1021 1022 // Invalid with msg; prefix with "jq: error: "1023 1024 if (jv_get_kind(msg) != JV_KIND_INVALID) {1023- if (jv_get_kind(msg) == JV_KIND_STRING)1024- return jv_string_fmt("jq: error: %s", jv_string_value(msg));1025+ if (jv_get_kind(msg) == JV_KIND_STRING) {1026+ jv r = jv_string_fmt("jq: error: %s", jv_string_value(msg));1027+ jv_free(msg);1028+ return r;1029+ }1030 1031 msg = jv_dump_string(msg, JV_PRINT_INVALID);1027- if (jv_get_kind(msg) == JV_KIND_STRING)1028- return jv_string_fmt("jq: error: %s", jv_string_value(msg));1032+ if (jv_get_kind(msg) == JV_KIND_STRING) {1033+ jv r = jv_string_fmt("jq: error: %s", jv_string_value(msg));1034+ jv_free(msg);1035+ return r;1036+ }1037+ jv_free(msg);1038 return jq_format_error(jv_null()); // ENOMEM1039 }1040 1041 // An invalid inside an invalid!200 unmodified lines1242 }1243 int nerrors = load_program(jq, locations, &program);1244 if (nerrors == 0) {1245 nerrors = builtins_bind(jq, &program);1237- if (nerrors == 0) {1246+ if (nerrors == 0)1247 nerrors = block_compile(program, &jq->bc, locations, args2obj(args));1239- }1248+ else1249+ jv_free(args);1250 } else1251 jv_free(args);1252 if (nerrors)1253 jq_report_error(jq, jv_string_fmt("jq: %d compile %s", nerrors, nerrors > 1 ? "errors" : "error"));95 unmodified linesmodified:src/jv_aux.c+9 added-13 removed
89 unmodified lines90 jv_free(t);91 v = jv_null();92 } else {93 double didx = jv_number_value(k);94- if (jvp_number_is_nan(k)) {94+ if (didx < INT_MIN) didx = INT_MIN;95+ if (didx > INT_MAX) didx = INT_MAX;96+ int idx = (int)didx;97+ if (idx < 0)98+ idx += jv_array_length(jv_copy(t));99+ v = jv_array_get(t, idx);100+ if (!jv_is_valid(v)) {101+ jv_free(v);102 v = jv_null();96- } else {97- if (didx < INT_MIN) didx = INT_MIN;98- if (didx > INT_MAX) didx = INT_MAX;99- int idx = (int)didx;100- if (idx < 0)101- idx += jv_array_length(jv_copy(t));102- v = jv_array_get(t, idx);103- if (!jv_is_valid(v)) {104- jv_free(v);105- v = jv_null();106- }103 }104 }105 jv_free(k);106 } else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_OBJECT) {51 unmodified lines158 (jv_get_kind(t) == JV_KIND_ARRAY || isnull)) {159 if (jvp_number_is_nan(k)) {160 jv_free(t);161 jv_free(k);162+ jv_free(v);163 t = jv_invalid_with_msg(jv_string("Cannot set array element at NaN index"));164 } else {165 double didx = jv_number_value(k);166 if (didx < INT_MIN) didx = INT_MIN;121 unmodified lines288 starts = jv_array_append(starts, jv_number(start));289 ends = jv_array_append(ends, jv_number(end));290 } else {291 jv_free(new_array);295- jv_free(key);292 new_array = e;293 goto arr_out;294 }295 } else {456 unmodified linesmodified:src/jv_file.c+1 added
65 unmodified lines66 jv_free(data);67 data = value;68 break;69 }70+ jv_free(value);71 }72 }73 if (!raw)74 jv_parser_free(parser);8 unmodified linesmodified:src/jv.c+2 added
1744 unmodified lines1745 if (slot) {1746 slot->value = jv_invalid();1747 } else {1748 if (!jvp_object_rehash(object)) {1749+ jvp_string_free(key);1750 *valpp = NULL;1751 return 0;1752 }1753 bucket = jvp_object_find_bucket(*object, key);105 unmodified lines1859 // copy/free of object, key, value coalesced1860 jv* slot;1861 if (!jvp_object_write(&object, key, &slot)) {1862 jv_free(object);1863+ jv_free(value);1864 return jv_invalid_with_msg(jv_string("Object too big"));1865 }1866 jv_free(*slot);1867 *slot = value;205 unmodified linesmodified:src/linker.c+1 added-1 removed
14 unmodified lines15 #include "jq_parser.h"16 #include "locfile.h"17 #include "jv.h"18 #include "jq.h"19-#include "parser.h"19 #include "util.h"20 #include "compile.h"21 #include "jv_alloc.h"22 35 unmodified lines58 continue;59 }60 path = expand_path(path);61 if (!jv_is_valid(path)) {62+ jv_free(err);63 err = path;64 path = jv_null();65 continue;66 }386 unmodified linesmodified:src/main.c+1 added
605 unmodified lines606 jv data = jv_load_file(program, 1);607 if (!jv_is_valid(data)) {608 data = jv_invalid_get_msg(data);609 fprintf(stderr, "jq: %s\n", jv_string_value(data));610+ free(program_origin);611 jv_free(data);612 ret = JQ_ERROR_SYSTEM;613 goto out;614 }108 unmodified lines