Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calloc error on @base64d format (ref #3280) #3286

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,11 +734,7 @@ static jv f_format(jq_state *jq, jv input, jv fmt) {
input = f_tostring(jq, input);
const unsigned char* data = (const unsigned char*)jv_string_value(input);
int len = jv_string_length_bytes(jv_copy(input));
if (len == 0) {
jv_free(input);
return jv_string("");
}
size_t decoded_len = (3 * (size_t)len) / 4; // 3 usable bytes for every 4 bytes of input
size_t decoded_len = MAX((3 * (size_t)len) / 4, 1); // 3 usable bytes for every 4 bytes of input
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah doh sorry, yeah my suggetion wont work 👍 think i've convinced myself that below code in the case of len < 4 will not be able to write outside result

char *result = jv_mem_calloc(decoded_len, sizeof(char));
uint32_t ri = 0;
int input_bytes_read=0;
Expand Down
3 changes: 2 additions & 1 deletion src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "bytecode.h"
#include "locfile.h"
#include "jv_alloc.h"
#include "util.h"

/*
The intermediate representation for jq filters is as a sequence of
Expand Down Expand Up @@ -1352,7 +1353,7 @@ int block_compile(block b, struct bytecode** out, struct locfile* lf, jv args) {
bc->globals = jv_mem_alloc(sizeof(struct symbol_table));
int ncfunc = count_cfunctions(b);
bc->globals->ncfunctions = 0;
bc->globals->cfunctions = jv_mem_calloc(ncfunc ? ncfunc : 1, sizeof(struct cfunction));
bc->globals->cfunctions = jv_mem_calloc(MAX(ncfunc, 1), sizeof(struct cfunction));
bc->globals->cfunc_names = jv_array();
bc->debuginfo = jv_object_set(jv_object(), jv_string("name"), jv_null());
jv env = jv_invalid();
Expand Down
4 changes: 4 additions & 0 deletions tests/base64.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
""
""

@base64d
"="
""

@base64d
"Zm/Ds2Jhcgo="
"foóbar\n"
Expand Down
Loading