Fix memory leaks in various c-coded builtins
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by William Langford · committed by William Langford · parent 18879fb · Browse files · 1 file·+45 added−35 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+45 added−35 removed
- other:+45 added−35 removed
Provenance
- Commit
c3cbbdd- Tree
61bb644- Author
William Langford <wlangfor@gmail.com>- Committer
William Langford <wlangfor@gmail.com>- Parent
18879fb
1 file changed+45 added−35 removed
modified:src/builtin.c+45 added-35 removed
65 unmodified lines66 jv_free(bad2);67 return err;68 }69 70+static inline jv ret_error(jv bad, jv msg) {71+ jv_free(bad);72+ return jv_invalid_with_msg(msg);73+}74+75+static inline jv ret_error2(jv bad1, jv bad2, jv msg) {76+ jv_free(bad1);77+ jv_free(bad2);78+ return jv_invalid_with_msg(msg);79+}80+81 static jv f_plus(jq_state *jq, jv input, jv a, jv b) {82 jv_free(input);83 if (jv_get_kind(a) == JV_KIND_NULL) {84 jv_free(a);122 unmodified lines207 }208 209 static jv f_startswith(jq_state *jq, jv a, jv b) {210 if (jv_get_kind(a) != JV_KIND_STRING || jv_get_kind(b) != JV_KIND_STRING)200- return jv_invalid_with_msg(jv_string("startswith() requires string inputs"));211+ return ret_error2(a, b, jv_string("startswith() requires string inputs"));212 int alen = jv_string_length_bytes(jv_copy(a));213 int blen = jv_string_length_bytes(jv_copy(b));214 jv ret;215 7 unmodified lines223 }224 225 static jv f_endswith(jq_state *jq, jv a, jv b) {226 if (jv_get_kind(a) != JV_KIND_STRING || jv_get_kind(b) != JV_KIND_STRING)216- return jv_invalid_with_msg(jv_string("endswith() requires string inputs"));227+ return ret_error2(a, b, jv_string("endswith() requires string inputs"));228 const char *astr = jv_string_value(a);229 const char *bstr = jv_string_value(b);230 size_t alen = jv_string_length_bytes(jv_copy(a));231 size_t blen = jv_string_length_bytes(jv_copy(b));852 unmodified lines1084 return jv_true();1085 }1086 1087 static jv f_halt_error(jq_state *jq, jv input, jv a) {1077- if (jv_get_kind(a) != JV_KIND_NUMBER)1078- return type_error(input, "halt_error/1: number required"); \1088+ if (jv_get_kind(a) != JV_KIND_NUMBER) {1089+ jv_free(a);1090+ return type_error(input, "halt_error/1: number required");1091+ }1092 jq_halt(jq, a, input);1093 return jv_true();1094 }1095 13 unmodified lines1109 }1110 1111 static jv f_string_split(jq_state *jq, jv a, jv b) {1112 if (jv_get_kind(a) != JV_KIND_STRING || jv_get_kind(b) != JV_KIND_STRING) {1100- jv_free(a);1101- jv_free(b);1102- return jv_invalid_with_msg(jv_string("split input and separator must be strings"));1113+ return ret_error2(a, b, jv_string("split input and separator must be strings"));1114 }1115 return jv_string_split(a, b);1116 }1117 1118 static jv f_string_explode(jq_state *jq, jv a) {1119 if (jv_get_kind(a) != JV_KIND_STRING) {1109- jv_free(a);1110- return jv_invalid_with_msg(jv_string("explode input must be a string"));1120+ return ret_error(a, jv_string("explode input must be a string"));1121 }1122 return jv_string_explode(a);1123 }1124 2 unmodified lines1127 }1128 1129 static jv f_string_implode(jq_state *jq, jv a) {1130 if (jv_get_kind(a) != JV_KIND_ARRAY) {1121- jv_free(a);1122- return jv_invalid_with_msg(jv_string("implode input must be an array"));1131+ return ret_error(a, jv_string("implode input must be an array"));1132 }1133 return jv_string_implode(a);1134 }1135 6 unmodified lines1142 static jv f_has(jq_state *jq, jv a, jv b) { return jv_has(a, b); }1143 1144 static jv f_modulemeta(jq_state *jq, jv a) {1145 if (jv_get_kind(a) != JV_KIND_STRING) {1137- jv_free(a);1138- return jv_invalid_with_msg(jv_string("modulemeta input module name must be a string"));1146+ return ret_error(a, jv_string("modulemeta input module name must be a string"));1147 }1148 return load_module_meta(jq, a);1149 }1150 137 unmodified lines1288 1289 #ifdef HAVE_STRPTIME1290 static jv f_strptime(jq_state *jq, jv a, jv b) {1291 if (jv_get_kind(a) != JV_KIND_STRING || jv_get_kind(b) != JV_KIND_STRING) {1284- jv_free(a);1285- jv_free(b);1286- return jv_invalid_with_msg(jv_string("strptime/1 requires string inputs and arguments"));1292+ return ret_error2(a, b, jv_string("strptime/1 requires string inputs and arguments"));1293 }1294 1295 struct tm tm;1296 memset(&tm, 0, sizeof(tm));3 unmodified lines1300 const char *fmt = jv_string_value(b);1301 const char *end = strptime(input, fmt, &tm);1302 1303 if (end == NULL || (*end != '\0' && !isspace(*end))) {1298- jv e = jv_invalid_with_msg(jv_string_fmt("date \"%s\" does not match format \"%s\"", input, fmt));1299- jv_free(a);1300- jv_free(b);1301- return e;1304+ return ret_error2(a, b, jv_string_fmt("date \"%s\" does not match format \"%s\"", input, fmt));1305 }1306 jv_free(b);1307 /*1308 * This is OS X or some *BSD whose strptime() is just not that24 unmodified lines1333 1334 #define TO_TM_FIELD(t, j, i) \1335 do { \1336 jv n = jv_array_get(jv_copy(j), (i)); \1334- if (jv_get_kind(n) != (JV_KIND_NUMBER)) \1337+ if (jv_get_kind(n) != (JV_KIND_NUMBER)) { \1338+ jv_free(j); \1339 return 0; \1340+ } \1341 t = jv_number_value(n); \1342 jv_free(n); \1343 } while (0)1344 24 unmodified lines1369 #undef TO_TM_FIELD1370 1371 static jv f_mktime(jq_state *jq, jv a) {1372 if (jv_get_kind(a) != JV_KIND_ARRAY)1368- return jv_invalid_with_msg(jv_string("mktime requires array inputs"));1373+ return ret_error(a, jv_string("mktime requires array inputs"));1374 if (jv_array_length(jv_copy(a)) < 6)1370- return jv_invalid_with_msg(jv_string("mktime requires parsed datetime inputs"));1375+ return ret_error(a, jv_string("mktime requires parsed datetime inputs"));1376 struct tm tm;1377 if (!jv2tm(a, &tm))1378 return jv_invalid_with_msg(jv_string("mktime requires parsed datetime inputs"));1379 time_t t = my_mktime(&tm);6 unmodified lines1386 1387 #ifdef HAVE_GMTIME_R1388 static jv f_gmtime(jq_state *jq, jv a) {1389 if (jv_get_kind(a) != JV_KIND_NUMBER)1385- return jv_invalid_with_msg(jv_string("gmtime() requires numeric inputs"));1390+ return ret_error(a, jv_string("gmtime() requires numeric inputs"));1391 struct tm tm, *tmp;1392 memset(&tm, 0, sizeof(tm));1393 double fsecs = jv_number_value(a);1394 time_t secs = fsecs;6 unmodified lines1401 }1402 #elif defined HAVE_GMTIME1403 static jv f_gmtime(jq_state *jq, jv a) {1404 if (jv_get_kind(a) != JV_KIND_NUMBER)1400- return jv_invalid_with_msg(jv_string("gmtime requires numeric inputs"));1405+ return ret_error(a, jv_string("gmtime requires numeric inputs"));1406 struct tm tm, *tmp;1407 memset(&tm, 0, sizeof(tm));1408 double fsecs = jv_number_value(a);1409 time_t secs = fsecs;13 unmodified lines1423 1424 #ifdef HAVE_LOCALTIME_R1425 static jv f_localtime(jq_state *jq, jv a) {1426 if (jv_get_kind(a) != JV_KIND_NUMBER)1422- return jv_invalid_with_msg(jv_string("localtime() requires numeric inputs"));1427+ return ret_error(a, jv_string("localtime() requires numeric inputs"));1428 struct tm tm, *tmp;1429 memset(&tm, 0, sizeof(tm));1430 double fsecs = jv_number_value(a);1431 time_t secs = fsecs;1432 jv_free(a);1433 tmp = localtime_r(&secs, &tm);1434 if (tmp == NULL)1430- return jv_invalid_with_msg(jv_string("errror converting number of seconds since epoch to datetime"));1435+ return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));1436 a = tm2jv(tmp);1437 return jv_array_set(a, 5, jv_number(jv_number_value(jv_array_get(jv_copy(a), 5)) + (fsecs - floor(fsecs))));1438 }1439 #elif defined HAVE_GMTIME1440 static jv f_localtime(jq_state *jq, jv a) {1441 if (jv_get_kind(a) != JV_KIND_NUMBER)1437- return jv_invalid_with_msg(jv_string("localtime requires numeric inputs"));1442+ return ret_error(a, jv_string("localtime requires numeric inputs"));1443 struct tm tm, *tmp;1444 memset(&tm, 0, sizeof(tm));1445 double fsecs = jv_number_value(a);1446 time_t secs = fsecs;1447 jv_free(a);1448 tmp = localtime(&secs);1449 if (tmp == NULL)1445- return jv_invalid_with_msg(jv_string("errror converting number of seconds since epoch to datetime"));1450+ return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));1451 a = tm2jv(tmp);1452 return jv_array_set(a, 5, jv_number(jv_number_value(jv_array_get(jv_copy(a), 5)) + (fsecs - floor(fsecs))));1453 }1454 #else7 unmodified lines1462 static jv f_strftime(jq_state *jq, jv a, jv b) {1463 if (jv_get_kind(a) == JV_KIND_NUMBER) {1464 a = f_gmtime(jq, a);1465 } else if (jv_get_kind(a) != JV_KIND_ARRAY) {1461- return jv_invalid_with_msg(jv_string("strftime/1 requires parsed datetime inputs"));1466+ return ret_error2(a, b, jv_string("strftime/1 requires parsed datetime inputs"));1467+ } else if (jv_get_kind(b) != JV_KIND_STRING) {1468+ return ret_error2(a, b, jv_string("strftime/1 requires a string format"));1469 }1470 struct tm tm;1471 if (!jv2tm(a, &tm))1465- return jv_invalid_with_msg(jv_string("strftime/1 requires parsed datetime inputs")); \1472+ return ret_error(b, jv_string("strftime/1 requires parsed datetime inputs"));1473+1474 const char *fmt = jv_string_value(b);1475 size_t alloced = strlen(fmt) + 100;1476 char *buf = alloca(alloced);1477 size_t n = strftime(buf, alloced, fmt, &tm);3 unmodified lines1481 return jv_invalid_with_msg(jv_string("strftime/1: unknown system failure"));1482 return jv_string(buf);1483 }1484 #else1477-static jv f_strftime(jq_state *jq, jv a) {1485+static jv f_strftime(jq_state *jq, jv a, jv b) {1486 jv_free(a);1487 jv_free(b);1488 return jv_invalid_with_msg(jv_string("strftime/1 not implemented on this platform"));1489 }3 unmodified lines1493 static jv f_strflocaltime(jq_state *jq, jv a, jv b) {1494 if (jv_get_kind(a) == JV_KIND_NUMBER) {1495 a = f_localtime(jq, a);1496 } else if (jv_get_kind(a) != JV_KIND_ARRAY) {1489- return jv_invalid_with_msg(jv_string("strflocaltime/1 requires parsed datetime inputs"));1497+ return ret_error2(a, b, jv_string("strflocaltime/1 requires parsed datetime inputs"));1498+ } else if (jv_get_kind(b) != JV_KIND_STRING) {1499+ return ret_error2(a, b, jv_string("strflocaltime/1 requires a string format"));1500 }1501 struct tm tm;1502 if (!jv2tm(a, &tm))1493- return jv_invalid_with_msg(jv_string("strflocaltime/1 requires parsed datetime inputs")); \1503+ return jv_invalid_with_msg(jv_string("strflocaltime/1 requires parsed datetime inputs"));1504 const char *fmt = jv_string_value(b);1505 size_t alloced = strlen(fmt) + 100;1506 char *buf = alloca(alloced);1507 size_t n = strftime(buf, alloced, fmt, &tm);3 unmodified lines1511 return jv_invalid_with_msg(jv_string("strflocaltime/1: unknown system failure"));1512 return jv_string(buf);1513 }1514 #else1505-static jv f_strflocaltime(jq_state *jq, jv a) {1515+static jv f_strflocaltime(jq_state *jq, jv a, jv b) {1516 jv_free(a);1517 jv_free(b);1518 return jv_invalid_with_msg(jv_string("strflocaltime/1 not implemented on this platform"));1519 }257 unmodified lines