diff options
| author | 0x221E | 2026-04-22 21:12:13 +0200 |
|---|---|---|
| committer | 0x221E | 2026-04-22 21:12:13 +0200 |
| commit | 9043e9f85f5e028771cbb4e61a24102a488d8628 (patch) | |
| tree | 51624cf30657bc8a4119939280014df353852cf8 /src | |
| parent | 9a7f8f1b99d8510fca4446dba3075998ca9f8075 (diff) | |
global: refactor: change strcmp,strncmp with sv_equal()
All equalities in the codebase now uses the string_view equality
check to ensure memory-safe comparison between strings. This is
the case, unless the comparison is between two null-terminated
C-style string, in which case they will not be using sv_equal()
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 22 | ||||
| -rw-r--r-- | src/um.c | 6 |
2 files changed, 17 insertions, 11 deletions
@@ -54,17 +54,19 @@ int cmd_install(int argv, char **cmd) parser_init(&parser, &mem, &backend, (void*)&userdata); parser_parse(&parser); + struct string_view pkg = (struct string_view) { + .buf = cmd[0], + .len = strlen(cmd[0]) + }; + int found = 0; LL_FOREACH(manifest, &userdata.manifest) { if (!current->data.key.buf) continue; - if (strncmp(cmd[0], - current->data.key.buf, - current->data.key.len) == 0) { + if (sv_equal(&pkg, ¤t->data.key)) found = 1; - } } if (!found) { @@ -80,14 +82,14 @@ typedef int (*cmd_fn)(int, char**); struct cmd_entry { - const char *key; + struct string_view key; cmd_fn func; }; // First-level command table struct cmd_entry table[] = { - {"install", cmd_install}, - {NULL, NULL}, + { SV("install"), cmd_install }, + { SV(NULL), NULL }, }; #define ARRAY_SIZE(x) sizeof((x)) / sizeof((x)[0]) @@ -111,10 +113,12 @@ int main(int argc, char **argv) cmd_fn cmd_func = NULL; + struct string_view argv1 = SV(argv[1]); + for (int i = 0; i < ARRAY_SIZE(table); i++) { - if (table[i].key == NULL) + if (table[i].key.buf == NULL) break; - if (strcmp(argv[1], table[i].key) == 0) + if (sv_equal(&argv1, &table[i].key)) cmd_func = table[i].func; } @@ -15,6 +15,8 @@ struct um_context { static struct um_context context; +static struct string_view manifest_sv = SV("MANIFEST"); + struct parser_backend um_backend() { return (struct parser_backend) { @@ -35,7 +37,7 @@ void um_block_start_cb(struct string_view *block, void *userdata) assert(block != NULL); assert(userdata != NULL); - if (strncmp("MANIFEST", block->buf, block->len) == 0) { + if (sv_equal(&manifest_sv, block)) { context.in_manifest = 1; } else { fprintf(stderr, "[UPSTREAM ERROR] Unknown block: %.*s\n", @@ -51,7 +53,7 @@ void um_block_end_cb(struct string_view *block, void *userdata) assert(block != NULL); assert(userdata != NULL); - if (strncmp("MANIFEST", block->buf, block->len) == 0) { + if (sv_equal(&manifest_sv, block)) { context.in_manifest = 0; } else { fprintf(stderr, "[UPSTREAM ERROR] Unknown block: %.*s\n", |
