From 283ffaaad6905373ba4e54201e16b21057ec9169 Mon Sep 17 00:00:00 2001 From: sfja Date: Fri, 27 Mar 2026 21:55:18 +0100 Subject: [PATCH] more stuff --- .vscode/launch.json | 2 +- Makefile | 19 +++++++++++++++---- src/collections.c | 14 ++++++++++---- src/json.h | 16 ++++++++-------- src/json_query.c | 30 ++++++++++++++++-------------- src/json_value.c | 13 ++++++------- 6 files changed, 56 insertions(+), 38 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 896b5e2..c5b2322 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/jq", - "args": ["", "data.json"], + "args": ["[11350].payload.issues.user", "large-file-formatted.json"], "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], diff --git a/Makefile b/Makefile index a84a1d6..95d9c6f 100644 --- a/Makefile +++ b/Makefile @@ -4,14 +4,24 @@ MAKEFLAGS += -j16 CFLAGS=-std=c17 -pedantic-errors -Wall -Wextra -Wconversion LDFLAGS= -# CFLAGS+=-fsanitize=address +# Some of the sussy stuff needs this +CFLAGS+=-Wno-strict-aliasing + +ASAN=0 +ifeq ($(ASAN),1) + CFLAGS += -fsanitize=address +endif + +LTO=0 +ifeq ($(ASAN),1) + CFLAGS += -flto=auto +endif RELEASE=0 - ifeq ($(RELEASE),1) CFLAGS += -O3 else - CFLAGS += -g + CFLAGS += -g -ggdb endif build_dir = build @@ -30,7 +40,8 @@ target=$(build_dir)/jq all: $(target) debug: $(target) - gdb -ex 'r' --args build/jq '.' data.json + # gdb -ex 'r' --args build/jq '.' data.json + gdb -ex 'r' --args build/jq '[11350].payload.issues.user' large-file-formatted.json $(target): $(sources:%.c=$(obj_dir)/%.o) gcc -o $@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/src/collections.c b/src/collections.c index c52f6d3..233cb69 100644 --- a/src/collections.c +++ b/src/collections.c @@ -270,10 +270,16 @@ static struct hash_entry *make_entry(struct hashmap *m, uint64_t hash) static void insert_key( struct hashmap *m, char const *key, size_t key_size, uint64_t hash) { - key_size = key_size != 0 ? key_size : strlen(key); - char *key_value = malloc(key_size + 1); - strncpy(key_value, key, key_size); - key_value[key_size] = '\0'; + char *key_value; + if (key_size != 0) { + key_value = malloc(key_size + 1); + strncpy(key_value, key, key_size); + key_value[key_size] = '\0'; + } else { + key_size = strlen(key); + key_value = malloc(key_size + 1); + strcpy(key_value, key); + } struct hash_key_entry key_entry = { .hash = hash, diff --git a/src/json.h b/src/json.h index 385ff50..33030b0 100644 --- a/src/json.h +++ b/src/json.h @@ -41,7 +41,8 @@ struct json_value *json_new_float(double val); size_t json_array_count(struct json_value const *array); struct json_value *json_idx(struct json_value *array, size_t idx); -struct json_value const *json_idx_const(struct json_value const *array, size_t idx); +struct json_value const *json_idx_const( + struct json_value const *array, size_t idx); void json_push(struct json_value *array, struct json_value *value); struct json_value *json_key(struct json_value *object, char const *key); @@ -55,19 +56,18 @@ void json_set_sized(struct json_value *object, char const *key, size_t key_size, struct json_value *value); -void json_object_keys( - struct json_value const *object, struct hash_key_entry const **keys, size_t *count); +void json_object_keys(struct json_value const *object, + struct hash_key_entry const **keys, + size_t *count); struct json_value *json_parse( - char const *text, size_t text_size, struct blockalloc* alloc); + char const *text, size_t text_size, struct blockalloc *alloc); struct json_value *json_query(struct json_value *val, char const *query); -typedef int JsonWriteCb(void* self, const char* data, size_t size); +typedef int JsonWriteCb(void *self, char const *data, size_t size); int json_stringify( - struct json_value const *node, - JsonWriteCb write, - void* self); + struct json_value const *node, JsonWriteCb write, void *self); #endif diff --git a/src/json_query.c b/src/json_query.c index 03d11e7..9e34e37 100644 --- a/src/json_query.c +++ b/src/json_query.c @@ -57,8 +57,7 @@ static struct tok t_tok(struct tokenizer *t, enum tokty ty, size_t idx) } static struct tok tokenizer_next(struct tokenizer *t); -static struct tok t_make_number_tok( - struct tokenizer *t, size_t idx, size_t *i) +static struct tok t_make_number_tok(struct tokenizer *t, size_t idx, size_t *i) { while (*i < t->len && t->text[*i] >= '0' && t->text[*i] <= '9') { t_step(t); @@ -66,8 +65,7 @@ static struct tok t_make_number_tok( return t_tok(t, tt_int, idx); } -static struct tok t_make_string_tok( - struct tokenizer *t, size_t idx, size_t *i) +static struct tok t_make_string_tok(struct tokenizer *t, size_t idx, size_t *i) { t_step(t); while (*i < t->len && t->text[*i] != '\"') { @@ -198,10 +196,10 @@ static struct path_seg parse_next(struct parser *p) } struct json_value *resolve_next_path_seg( - struct parser *p, struct json_value* node) + struct parser *p, struct json_value *node) { struct path_seg seg = parse_next(p); - switch (seg.ty){ + switch (seg.ty) { case pt_error: return NULL; case pt_eof: @@ -209,22 +207,28 @@ struct json_value *resolve_next_path_seg( case pt_ident_key: { if (!json_is(node, json_object)) return NULL; - struct json_value *child = - json_key_sized(node, seg.tok.ptr, seg.tok.len); + struct json_value *child + = json_key_sized(node, seg.tok.ptr, seg.tok.len); + if (!child) + return NULL; return resolve_next_path_seg(p, child); } case pt_string_key: { if (!json_is(node, json_object)) return NULL; - struct json_value *child = - json_key_sized(node, seg.tok.ptr + 1, seg.tok.len - 2); + struct json_value *child + = json_key_sized(node, seg.tok.ptr + 1, seg.tok.len - 2); + if (!child) + return NULL; return resolve_next_path_seg(p, child); } case pt_idx: { if (!json_is(node, json_array)) return NULL; - struct json_value *child = - json_idx(node, strtoull(seg.tok.ptr, NULL, 10)); + struct json_value *child + = json_idx(node, strtoull(seg.tok.ptr, NULL, 10)); + if (!child) + return NULL; return resolve_next_path_seg(p, child); } } @@ -248,5 +252,3 @@ struct json_value *json_query(struct json_value *val, char const *query) return resolve_next_path_seg(&p, val); } - - diff --git a/src/json_value.c b/src/json_value.c index b045b17..6889a51 100644 --- a/src/json_value.c +++ b/src/json_value.c @@ -49,8 +49,7 @@ static void set_shallow_val(struct json_value **ptr, size_t val) static struct json_value *new_shallow(enum json_type ty) { - struct json_value *shallow_val = 0; - *(size_t *)&shallow_val |= 0x2; + struct json_value *shallow_val = (struct json_value *)0x2; set_shallow_ty(&shallow_val, ty); return shallow_val; } @@ -259,7 +258,8 @@ struct json_value *json_idx(struct json_value *array, size_t idx) return smallarray_get(&array->array_vals, idx); } -struct json_value const *json_idx_const(struct json_value const *array, size_t idx) +struct json_value const *json_idx_const( + struct json_value const *array, size_t idx) { assert(array->ty == json_array); if (idx >= smallarray_count(&array->array_vals)) @@ -293,7 +293,6 @@ struct json_value const *json_key_hash_const( return hashmap_get_hash_const(&object->object_fields, hash); } - void json_set( struct json_value *object, char const *key, struct json_value *value) { @@ -310,11 +309,11 @@ void json_set_sized(struct json_value *object, hashmap_set_sized(&object->object_fields, key, key_size, value); } -void json_object_keys( - struct json_value const *object, struct hash_key_entry const **keys, size_t *count) +void json_object_keys(struct json_value const *object, + struct hash_key_entry const **keys, + size_t *count) { assert(object->ty == json_object); *keys = object->object_fields.keys; *count = object->object_fields.keys_count; } -