srdja / Collections-C

A library of generic data structures for the C language.
http://srdja.github.io/Collections-C
GNU Lesser General Public License v3.0
2.8k stars 328 forks source link

Treetable: Segfault when calling get_successor_node on root if it is the greatest key #173

Closed JaviMuller closed 10 months ago

JaviMuller commented 10 months ago

When calling the function get_successor_node on the root, if it is the node with the greatest key, it will try to access x->parent->right. Here is a minimal reproducibility example (get_greater_than will call get_successor_node):

#include "cc_treetable.h"
#include <stdio.h>

int cmp(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

int main() {
    CC_TreeTable *table;
    int (*comp)(const void *, const void *) = &cmp;
    if (cc_treetable_new(comp, &table) != CC_OK)
        return -1;

    void *out;

    int key = 1;

    cc_treetable_add(table, &key, &key);
    cc_treetable_get_greater_than(table, &key1, &out); // SEGFAULT
    cc_treetable_destroy(table);
}
JaviMuller commented 10 months ago

Tried to dereference out