Skip to content

Commit 511d50b

Browse files
authored
ltrimstr/1+rtrimstr/1: don't leak on invalid input or arguments
ltrimstr/rtrimstr was ignoring and leaking the error returned by f_startswith()/f_endswith(). This also means that they just let the input pass through for non-string inputs or arguments. Only fix the leak for now; in the next release, jqlang#2969 will make them rethrow the error returned by startswith/endswith. Ref: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=64946
1 parent 7298972 commit 511d50b

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/builtin.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ static jv f_endswith(jq_state *jq, jv a, jv b) {
295295
}
296296

297297
static jv f_ltrimstr(jq_state *jq, jv input, jv left) {
298-
if (jv_get_kind(f_startswith(jq, jv_copy(input), jv_copy(left))) != JV_KIND_TRUE) {
298+
jv startswith = f_startswith(jq, jv_copy(input), jv_copy(left));
299+
if (jv_get_kind(startswith) != JV_KIND_TRUE) {
300+
jv_free(startswith);
299301
jv_free(left);
300302
return input;
301303
}
@@ -311,12 +313,14 @@ static jv f_ltrimstr(jq_state *jq, jv input, jv left) {
311313
}
312314

313315
static jv f_rtrimstr(jq_state *jq, jv input, jv right) {
314-
if (jv_get_kind(f_endswith(jq, jv_copy(input), jv_copy(right))) == JV_KIND_TRUE) {
316+
jv endswith = f_endswith(jq, jv_copy(input), jv_copy(right));
317+
if (jv_get_kind(endswith) == JV_KIND_TRUE) {
315318
jv res = jv_string_sized(jv_string_value(input),
316319
jv_string_length_bytes(jv_copy(input)) - jv_string_length_bytes(right));
317320
jv_free(input);
318321
return res;
319322
}
323+
jv_free(endswith);
320324
jv_free(right);
321325
return input;
322326
}

tests/jq.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,22 @@ try ("foobar" | .[1.5]) catch .
20912091
null
20922092
"Cannot index string with number"
20932093

2094+
20942095
# setpath/2 does not leak the input after an invalid get #2970
2096+
20952097
try ["ok", setpath([1]; 1)] catch ["ko", .]
20962098
{"hi":"hello"}
20972099
["ko","Cannot index object with number"]
2100+
2101+
2102+
# ltrimstr/1 rtrimstr/1 don't leak on invalid input #2977
2103+
2104+
try ltrimstr(1) catch "x", try rtrimstr(1) catch "x" | "ok"
2105+
"hi"
2106+
"ok"
2107+
"ok"
2108+
2109+
try ltrimstr("x") catch "x", try rtrimstr("x") catch "x" | "ok"
2110+
{"hey":[]}
2111+
"ok"
2112+
"ok"

0 commit comments

Comments
 (0)