Skip to content

Commit 94fd973

Browse files
authored
Fix calloc error on @base64d format (ref #3280) (#3286)
This commit fixes a regression of a8ce2ff, e.g. `"=" | @base64d`.
1 parent c70ae1a commit 94fd973

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

src/builtin.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,7 @@ static jv f_format(jq_state *jq, jv input, jv fmt) {
734734
input = f_tostring(jq, input);
735735
const unsigned char* data = (const unsigned char*)jv_string_value(input);
736736
int len = jv_string_length_bytes(jv_copy(input));
737-
if (len == 0) {
738-
jv_free(input);
739-
return jv_string("");
740-
}
741-
size_t decoded_len = (3 * (size_t)len) / 4; // 3 usable bytes for every 4 bytes of input
737+
size_t decoded_len = MAX((3 * (size_t)len) / 4, 1); // 3 usable bytes for every 4 bytes of input
742738
char *result = jv_mem_calloc(decoded_len, sizeof(char));
743739
uint32_t ri = 0;
744740
int input_bytes_read=0;

src/compile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "bytecode.h"
77
#include "locfile.h"
88
#include "jv_alloc.h"
9+
#include "util.h"
910

1011
/*
1112
The intermediate representation for jq filters is as a sequence of
@@ -1352,7 +1353,7 @@ int block_compile(block b, struct bytecode** out, struct locfile* lf, jv args) {
13521353
bc->globals = jv_mem_alloc(sizeof(struct symbol_table));
13531354
int ncfunc = count_cfunctions(b);
13541355
bc->globals->ncfunctions = 0;
1355-
bc->globals->cfunctions = jv_mem_calloc(ncfunc ? ncfunc : 1, sizeof(struct cfunction));
1356+
bc->globals->cfunctions = jv_mem_calloc(MAX(ncfunc, 1), sizeof(struct cfunction));
13561357
bc->globals->cfunc_names = jv_array();
13571358
bc->debuginfo = jv_object_set(jv_object(), jv_string("name"), jv_null());
13581359
jv env = jv_invalid();

tests/base64.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
""
2424
""
2525

26+
@base64d
27+
"="
28+
""
29+
2630
@base64d
2731
"Zm/Ds2Jhcgo="
2832
"foóbar\n"

0 commit comments

Comments
 (0)