From 3cd3b20f58e9d2c6463d503be09e5d4cfaadee6c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 12 Oct 2025 14:05:22 -0400 Subject: Code cleanup and fixing minor issues --- src/stdlib/c_strings.c | 3 ++- src/stdlib/files.c | 8 +++++--- src/stdlib/fpconv.c | 3 --- src/stdlib/lists.c | 1 - src/stdlib/paths.c | 3 ++- src/stdlib/stdlib.c | 4 +++- src/stdlib/text.c | 11 +++++------ 7 files changed, 17 insertions(+), 16 deletions(-) (limited to 'src/stdlib') diff --git a/src/stdlib/c_strings.c b/src/stdlib/c_strings.c index 2946ce58..54ea1e40 100644 --- a/src/stdlib/c_strings.c +++ b/src/stdlib/c_strings.c @@ -28,7 +28,8 @@ PUREFUNC public int32_t CString$compare(const void *x, const void *y, const Type if (!*(const char **)x != !*(const char **)y) return (!*(const char **)y) - (!*(const char **)x); - return strcmp(*(const char **)x, *(const char **)y); + const char *x_str = *(const char **)x, *y_str = *(const char **)y; + return (x_str == NULL) || (y_str == NULL) ? (x_str != NULL) - (y_str != NULL) : strcmp(x_str, y_str); } PUREFUNC public bool CString$equal(const void *x, const void *y, const TypeInfo_t *type) { diff --git a/src/stdlib/files.c b/src/stdlib/files.c index 7fd4c2c7..88d78676 100644 --- a/src/stdlib/files.c +++ b/src/stdlib/files.c @@ -110,9 +110,11 @@ static file_t *_load_file(const char *filename, FILE *file) { // Convert to relative path (if applicable) char buf[PATH_MAX]; char *cwd = getcwd(buf, sizeof(buf)); - size_t cwd_len = strlen(cwd); - if (strncmp(cwd, filename, cwd_len) == 0 && filename[cwd_len] == '/') - ret->relative_filename = &filename[cwd_len + 1]; + if (cwd != NULL) { + size_t cwd_len = strlen(cwd); + if (strncmp(cwd, filename, cwd_len) == 0 && filename[cwd_len] == '/') + ret->relative_filename = &filename[cwd_len + 1]; + } } return ret; } diff --git a/src/stdlib/fpconv.c b/src/stdlib/fpconv.c index 0781295d..eaae959a 100644 --- a/src/stdlib/fpconv.c +++ b/src/stdlib/fpconv.c @@ -117,7 +117,6 @@ static Fp multiply(Fp *a, Fp *b) { static void round_digit(char *digits, int ndigits, uint64_t delta, uint64_t rem, uint64_t kappa, uint64_t frac) { while (rem < frac && delta - rem >= kappa && (rem + kappa < frac || frac - rem > rem + kappa - frac)) { - digits[ndigits - 1]--; rem += kappa; } @@ -153,7 +152,6 @@ static int generate_digits(Fp *fp, Fp *upper, Fp *lower, char *digits, int *K) { if (tmp <= delta) { *K += kappa; round_digit(digits, idx, delta, tmp, div << -one.exp, wfrac); - return idx; } } @@ -175,7 +173,6 @@ static int generate_digits(Fp *fp, Fp *upper, Fp *lower, char *digits, int *K) { if (part2 < delta) { *K += kappa; round_digit(digits, idx, delta, part2, one.frac, wfrac * *unit); - return idx; } diff --git a/src/stdlib/lists.c b/src/stdlib/lists.c index 516f37e2..db514671 100644 --- a/src/stdlib/lists.c +++ b/src/stdlib/lists.c @@ -156,7 +156,6 @@ void List$insert_all(List_t *list, List_t to_insert, Int_t int_index, int64_t pa if (list->stride == padded_item_size) { memcpy(p, list->data + padded_item_size * (index - 1), (size_t)(((int64_t)list->length - index + 1) * padded_item_size)); - p += ((int64_t)list->length - index + 1) * padded_item_size; } else { for (int64_t i = index - 1; i < (int64_t)list->length - 1; i++) { memcpy(p, list->data + list->stride * i, (size_t)padded_item_size); diff --git a/src/stdlib/paths.c b/src/stdlib/paths.c index b6152ed4..76d0d629 100644 --- a/src/stdlib/paths.c +++ b/src/stdlib/paths.c @@ -130,7 +130,8 @@ Path_t Path$_concat(int n, Path_t items[n]) { public Path_t Path$resolved(Path_t path, Path_t relative_to) { - if (path.type == PATHTYPE_RELATIVE && !(relative_to.type == PATHTYPE_RELATIVE && relative_to.components.length == 0)) { + if (path.type == PATHTYPE_RELATIVE + && !(relative_to.type == PATHTYPE_RELATIVE && relative_to.components.length == 0)) { Path_t result = {.type = relative_to.type}; result.components = relative_to.components; LIST_INCREF(result.components); diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c index 0756eab3..5204194b 100644 --- a/src/stdlib/stdlib.c +++ b/src/stdlib/stdlib.c @@ -59,7 +59,8 @@ static _Noreturn void signal_handler(int sig, siginfo_t *info, void *userdata) { public void tomo_init(void) { GC_INIT(); - USE_COLOR = getenv("COLOR") ? strcmp(getenv("COLOR"), "1") == 0 : isatty(STDOUT_FILENO); + 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; setlocale(LC_ALL, ""); @@ -194,6 +195,7 @@ OptionalText_t ask(Text_t prompt, bool bold, bool force_tty) { cleanup: if (out && out != stdout) fclose(out); if (in && in != stdin) fclose(in); + if (line != NULL) free(line); return ret; } diff --git a/src/stdlib/text.c b/src/stdlib/text.c index 0f40aef9..a91490e6 100644 --- a/src/stdlib/text.c +++ b/src/stdlib/text.c @@ -224,17 +224,18 @@ int32_t get_synthetic_grapheme(const ucs4_t *codepoints, int64_t utf32_len) { // synthetic graphemes store all of their information in a densely packed // area with good cache locality: static void *arena = NULL, *arena_end = NULL; - // Eat up any space needed to make arena 32-bit aligned: - if ((size_t)arena % __alignof__(ucs4_t) != 0) arena += __alignof__(ucs4_t) - ((size_t)arena % __alignof__(ucs4_t)); + if (arena != NULL && (size_t)arena % __alignof__(ucs4_t) != 0) + arena += __alignof__(ucs4_t) - ((size_t)arena % __alignof__(ucs4_t)); // If we have filled up this arena, allocate a new one: size_t needed_memory = sizeof(ucs4_t[1 + utf32_len]) + sizeof(uint8_t[u8_len + 1]); - if (arena + needed_memory > arena_end) { + if (arena == NULL || arena + needed_memory > arena_end) { // Do reasonably big chunks at a time, so most synthetic codepoints are // nearby each other in memory and cache locality is good. This is a // rough guess at a good size: size_t chunk_size = MAX(needed_memory, 512); arena = GC_MALLOC_ATOMIC(chunk_size); + assert(arena != NULL); arena_end = arena + chunk_size; } @@ -497,9 +498,8 @@ static Text_t concat2(Text_t a, Text_t b) { if (first_b < 0) { memcpy(dest, GRAPHEME_CODEPOINTS(first_b), sizeof(ucs4_t[NUM_GRAPHEME_CODEPOINTS(first_b)])); - dest += NUM_GRAPHEME_CODEPOINTS(first_b); } else { - *(dest++) = (ucs4_t)first_b; + *dest = (ucs4_t)first_b; } // Do a normalization run for these two codepoints and see if it looks different. @@ -582,7 +582,6 @@ static Text_t Text$repeat_to_width(Text_t to_repeat, int64_t target_width, Text_ int64_t w = (int64_t)u8_strwidth((const uint8_t *)Text$as_c_string(c), lang_str); if (repeated_width + w > target_width) { repeated = concat2(repeated, Text$repeat(Text(" "), I(target_width - repeated_width))); - repeated_width = target_width; break; } repeated = concat2(repeated, c); -- cgit v1.2.3