Avoid zero-length `calloc` (#3280)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Using `calloc` function with a count of zero elements is implementation defined and it may cause allocation errors on some platforms (ref #3277). This commit fixes `jv_mem_calloc` callers to avoid undefined behavior.
authored by itchyny · committed by GitHub · parent 4003202 · Browse files · 6 files·+28 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+28 added−7 removed
- tests:+8 added−0 removed
- other:+20 added−7 removed
Provenance
- Commit
a8ce2ff- Tree
6b64661- Author
itchyny <itchyny@cybozu.co.jp>- Committer
GitHub <noreply@github.com>- Parent
4003202
6 files changed+28 added−7 removed
modified:src/builtin.c+4 added-1 removed
732 unmodified lines733 jv_free(fmt);734 input = f_tostring(jq, input);735 const unsigned char* data = (const unsigned char*)jv_string_value(input);736 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 input742 char *result = jv_mem_calloc(decoded_len, sizeof(char));739- memset(result, 0, decoded_len * sizeof(char));743 uint32_t ri = 0;744 int input_bytes_read=0;745 uint32_t code = 0;746 for (int i=0; i<len && data[i] != '='; i++) {1314 unmodified linesmodified:src/compile.c+1 added-3 removed
1 #include <assert.h>2-#include <math.h>2 #include <string.h>3 #include <stdlib.h>4 #include <unistd.h>5 #include "compile.h"6 #include "bytecode.h"7 #include "locfile.h"8 #include "jv_alloc.h"10-#include "linker.h"9 10 /*11 The intermediate representation for jq filters is as a sequence of12 struct inst, which form a doubly-linked list via the next and prev1358 unmodified lines1371 bc->nclosures = 0;1372 bc->globals = jv_mem_alloc(sizeof(struct symbol_table));1373 int ncfunc = count_cfunctions(b);1374 bc->globals->ncfunctions = 0;1377- bc->globals->cfunctions = jv_mem_calloc(ncfunc, sizeof(struct cfunction));1375+ bc->globals->cfunctions = jv_mem_calloc(ncfunc ? ncfunc : 1, sizeof(struct cfunction));1376 bc->globals->cfunc_names = jv_array();1377 bc->debuginfo = jv_object_set(jv_object(), jv_string("name"), jv_null());1378 jv env = jv_invalid();1379 int nerrors = compile(bc, b, lf, args, &env);19 unmodified linesmodified:src/jv_alloc.c+5 added-2 removed
1-#include <stdlib.h>1+#include <assert.h>2 #include <stdio.h>3+#include <stdlib.h>4 #include <string.h>4-#include "jv_alloc.h"5+#include "jv.h"6 7 struct nomem_handler {8 jv_nomem_handler_f handler;9 void *data;140 unmodified lines150 return malloc(sz);151 }152 153 void* jv_mem_calloc(size_t nemb, size_t sz) {154+ assert(nemb > 0 && sz > 0);155 void* p = calloc(nemb, sz);156 if (!p) {157 memory_exhausted();158 }159 return p;160 }161 162 void* jv_mem_calloc_unguarded(size_t nemb, size_t sz) {163+ assert(nemb > 0 && sz > 0);164 return calloc(nemb, sz);165 }166 167 char* jv_mem_strdup(const char *s) {22 unmodified linesmodified:src/jv_alloc.h-1 removed
1 #ifndef JV_ALLOC_H2 #define JV_ALLOC_H3 4 #include <stddef.h>5-#include "jv.h"5 6 void* jv_mem_alloc(size_t);7 void* jv_mem_alloc_unguarded(size_t);8 void* jv_mem_calloc(size_t, size_t);7 unmodified linesmodified:src/jv_aux.c+10 added
1 unmodified line2 #include <limits.h>3 #include <math.h>4 #include <string.h>5 #include <stdlib.h>6+#include "jv.h"7 #include "jv_alloc.h"8 #include "jv_private.h"9 10 // making this static verbose function here541 unmodified lines552 553 jv jv_keys(jv x) {554 if (jv_get_kind(x) == JV_KIND_OBJECT) {555 int nkeys = jv_object_length(jv_copy(x));556+ if (nkeys == 0) {557+ jv_free(x);558+ return jv_array();559+ }560 jv* keys = jv_mem_calloc(nkeys, sizeof(jv));561 int kidx = 0;562 jv_object_foreach(x, key, value) {563 keys[kidx++] = key;113 unmodified lines677 assert(jv_get_kind(objects) == JV_KIND_ARRAY);678 assert(jv_get_kind(keys) == JV_KIND_ARRAY);679 assert(jv_array_length(jv_copy(objects)) == jv_array_length(jv_copy(keys)));680 int n = jv_array_length(jv_copy(objects));681+ if (n == 0) {682+ jv_free(objects);683+ jv_free(keys);684+ return NULL;685+ }686 struct sort_entry* entries = jv_mem_calloc(n, sizeof(struct sort_entry));687 for (int i=0; i<n; i++) {688 entries[i].object = jv_array_get(jv_copy(objects), i);689 entries[i].key = jv_array_get(jv_copy(keys), i);72 unmodified linesmodified:tests/base64.test+8 added
1 # Tests are groups of three lines: program, input, expected output2 # Blank lines and lines starting with # are ignored3 4 @base645+""6+""7+8+@base649 "<>&'\"\t"10 "PD4mJyIJ"11 12 # decoding encoded output results in same text6 unmodified lines19 "foóbar\n"20 "Zm/Ds2Jhcgo="21 22 @base64d23+""24+""25+26+@base64d27 "Zm/Ds2Jhcgo="28 "foóbar\n"29 30 # optional trailing equals padding (With padding, this is cWl4YmF6Cg==)13 unmodified lines