From 8e4af72622e4a9bb3383085fce28c55444a9d8ee Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Fri, 16 Oct 2026 00:50:48 +0200 Subject: always check the result of rb3_insert(), i.e. detect collisions in RB trees This was already done for the goto table, but by first looking up whether a label already exists in the table. Since the insert operation already reports entries with the same key in the tree, we should rather check that. This saves one goto table lookup per label definition, but on the other hand results in excess allocations in case of encountering a label twice. In teco_help_set(), we did not check for collisions at all. Even thouch they are very unlikely, we could encounter them if a topic is defined on exactly the same position as in .SCITECO_TOPIC foo .SCITECO_TOPIC foo In this case, we leaked memory. --- src/goto.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/goto.c') diff --git a/src/goto.c b/src/goto.c index 42eb964..36002e6 100644 --- a/src/goto.c +++ b/src/goto.c @@ -167,12 +167,14 @@ teco_goto_table_find(teco_goto_table_t *ctx, const gchar *name, gsize len) gssize teco_goto_table_set(teco_goto_table_t *ctx, const gchar *name, gsize len, gsize pc) { - gssize existing_pc = teco_goto_table_find(ctx, name, len); - if (existing_pc >= 0) - return existing_pc; + teco_goto_label_t *label, *found; - teco_goto_label_t *label = teco_goto_label_new(name, len, pc); - teco_rb3str_insert(&ctx->tree, TRUE, &label->head); + label = teco_goto_label_new(name, len, pc); + found = (teco_goto_label_t *)teco_rb3str_insert(&ctx->tree, TRUE, &label->head); + if (found) { + teco_goto_label_free(label); + return found->pc; + } #ifdef DEBUG teco_goto_table_dump(ctx); -- cgit v1.2.3