From 6e5fb2ac4e5b780c74f310446ddd80d571170b0d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 12 Oct 2025 14:19:59 -0400 Subject: More code cleanups --- src/stdlib/cli.c | 1 - src/stdlib/paths.c | 3 ++- src/stdlib/stacktrace.c | 2 +- src/stdlib/stdlib.c | 3 ++- src/stdlib/tables.c | 15 ++++++--------- 5 files changed, 11 insertions(+), 13 deletions(-) (limited to 'src/stdlib') diff --git a/src/stdlib/cli.c b/src/stdlib/cli.c index 2d54d330..de3b804e 100644 --- a/src/stdlib/cli.c +++ b/src/stdlib/cli.c @@ -199,7 +199,6 @@ void _tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, const c if (argv[i][0] == '-' && argv[i][1] == '-') { if (argv[i][2] == '\0') { // "--" signals the rest of the arguments are literal used_args[i] = true; - i += 1; break; } diff --git a/src/stdlib/paths.c b/src/stdlib/paths.c index 76d0d629..73f62975 100644 --- a/src/stdlib/paths.c +++ b/src/stdlib/paths.c @@ -330,10 +330,11 @@ OptionalList_t Path$read_bytes(Path_t path, OptionalInt_t count) { // be closed by GC finalizers. GC_gcollect(); fd = open(path_str, O_RDONLY); - if (fd == -1) return NONE_LIST; } } + if (fd == -1) return NONE_LIST; + struct stat sb; if (fstat(fd, &sb) != 0) return NONE_LIST; diff --git a/src/stdlib/stacktrace.c b/src/stdlib/stacktrace.c index 0953e660..b21d0cbc 100644 --- a/src/stdlib/stacktrace.c +++ b/src/stdlib/stacktrace.c @@ -34,7 +34,7 @@ static void fprint_context(FILE *out, const char *filename, int lineno, int cont num_width += 1; while ((nread = getline(&line, &size, f)) != -1) { - if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = '\0'; + if (line[nread - 1] == '\n') line[nread - 1] = '\0'; if (cur_line >= lineno - context_before) { int w = 1; diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c index 5204194b..dd6f804d 100644 --- a/src/stdlib/stdlib.c +++ b/src/stdlib/stdlib.c @@ -61,7 +61,8 @@ void tomo_init(void) { GC_INIT(); const char *color_env = getenv("COLOR"); USE_COLOR = color_env ? strcmp(color_env, "1") == 0 : isatty(STDOUT_FILENO); - if (getenv("NO_COLOR") && getenv("NO_COLOR")[0] != '\0') USE_COLOR = false; + const char *no_color_env = getenv("NO_COLOR"); + if (no_color_env && no_color_env[0] != '\0') USE_COLOR = false; setlocale(LC_ALL, ""); assert(getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0) == sizeof(TOMO_HASH_KEY)); diff --git a/src/stdlib/tables.c b/src/stdlib/tables.c index 3c78b770..45e3abb4 100644 --- a/src/stdlib/tables.c +++ b/src/stdlib/tables.c @@ -732,20 +732,20 @@ void Table$serialize(const void *obj, FILE *out, Table_t *pointers, const TypeIn if (t->fallback) Table$serialize(t->fallback, out, pointers, type); } -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wstack-protector" -#endif public void Table$deserialize(FILE *in, void *outval, List_t *pointers, const TypeInfo_t *type) { int64_t len; Int64$deserialize(in, &len, pointers, &Int$info); Table_t t = EMPTY_TABLE; + char keybuf[256], valbuf[256]; + char *key = + (size_t)type->TableInfo.key->size <= sizeof(keybuf) ? keybuf : GC_MALLOC((size_t)type->TableInfo.key->size); + char *value = + (size_t)type->TableInfo.value->size <= sizeof(valbuf) ? valbuf : GC_MALLOC((size_t)type->TableInfo.value->size); + for (int64_t i = 0; i < len; i++) { - char key[type->TableInfo.key->size]; _deserialize(in, key, pointers, type->TableInfo.key); - char value[type->TableInfo.value->size]; if (type->TableInfo.value->size > 0) _deserialize(in, value, pointers, type->TableInfo.value); Table$set(&t, key, value, type); } @@ -757,6 +757,3 @@ void Table$deserialize(FILE *in, void *outval, List_t *pointers, const TypeInfo_ *(Table_t *)outval = t; } -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif -- cgit v1.2.3