Skip to content

Commit

Permalink
ltrimstr/1+rtrimstr/1: don't leak on invalid input or arguments
Browse files Browse the repository at this point in the history
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, #2969 will make them
rethrow the error returned by startswith/endswith.

Ref: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=64946
  • Loading branch information
emanuele6 committed Dec 11, 2023
1 parent 7e54d96 commit 3e1cc63
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ static jv f_endswith(jq_state *jq, jv a, jv b) {
}

static jv f_ltrimstr(jq_state *jq, jv input, jv left) {
if (jv_get_kind(f_startswith(jq, jv_copy(input), jv_copy(left))) != JV_KIND_TRUE) {
jv startswith = f_startswith(jq, jv_copy(input), jv_copy(left));
if (jv_get_kind(startswith) != JV_KIND_TRUE) {
jv_free(startswith);
jv_free(left);
return input;
}
Expand All @@ -311,12 +313,14 @@ static jv f_ltrimstr(jq_state *jq, jv input, jv left) {
}

static jv f_rtrimstr(jq_state *jq, jv input, jv right) {
if (jv_get_kind(f_endswith(jq, jv_copy(input), jv_copy(right))) == JV_KIND_TRUE) {
jv endswith = f_endswith(jq, jv_copy(input), jv_copy(right));
if (jv_get_kind(endswith) == JV_KIND_TRUE) {
jv res = jv_string_sized(jv_string_value(input),
jv_string_length_bytes(jv_copy(input)) - jv_string_length_bytes(right));
jv_free(input);
return res;
}
jv_free(endswith);
jv_free(right);
return input;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -2091,7 +2091,22 @@ try ("foobar" | .[1.5]) catch .
null
"Cannot index string with number"


# setpath/2 does not leak the input after an invalid get #2970

try ["ok", setpath([1]; 1)] catch ["ko", .]
{"hi":"hello"}
["ko","Cannot index object with number"]


# ltrimstr/1 rtrimstr/1 don't leak on invalid input #2977

try ltrimstr(1) catch "x", try rtrimstr(1) catch "x" | "ok"
"hi"
"ok"
"ok"

try ltrimstr("x") catch "x", try rtrimstr("x") catch "x" | "ok"
{"hey":[]}
"ok"
"ok"

0 comments on commit 3e1cc63

Please sign in to comment.