Repository: valkey-io/valkey
File: src/unit/test_rax.c (C)

Code before:
        if (i && !(i % 100)) {
            printf(".");
            if (!(i % 1000)) {
                printf("%d%% done", i / 1000);
            }
            fflush(stdout);
        }
    }
    printf("\n");

    if (errors) {
        printf("!!! WARNING !!!: %d errors found\n", errors);
    } else {
        printf("OK! \\o/\n");
    }
    return !!errors;
}

/* Test for crash in raxRemove when two keys share a common prefix in the same rax
 * structure. Upon deletion of one key, rax attempts to recompress the structure
 * back to its original form for other key. The crash occurs when the
 * recompression exceeds 512MB size limit due to a missing size check in the
 * compression logic.
 *
 * This test is disabled by default because it uses a lot of memory. */
int test_raxRemoveCrash(int argc, char **argv, int flags) {
    UNUSED(argc);
    UNUSED(argv);

    if (!(flags & UNIT_TEST_LARGE_MEMORY)) return 0;

    rax *rt = raxNew();

    /* Insert small keys */
    char small_key[32];
    const char *small_prefix = ",({5oM}";
    int i;
    for (i = 1; i <= 20; i++) {
        snprintf(small_key, sizeof(small_key), "%s%d", small_prefix, i);
        size_t keylen = strlen(small_key);
        raxInsert(rt, (unsigned char *)small_key, keylen,
                  (void *)(long)i, NULL);
    }

    /* Insert large key exceeding compressed node size limit */
    size_t max_keylen = ((1 << 29) - 1) + 100; // Compressed node limit + overflow
    const char *large_prefix = ",({ABC}";
    unsigned char *large_key = zmalloc(max_keylen + strlen(large_prefix));
    if (!large_key) {
        fprintf(stderr, "Failed to allocate memory for large key\n");
        raxFree(rt);

Review comment (bug):
"Remove crash" is not a very descriptive title. :D

The comment above says things like "The crash occurs" but it should instead describe that the test checks that a crash doesn't occur. It can say that in the past, there was a crash.

```suggestion
int test_raxRecompressHugeKey(int argc, char **argv, int flags) {
```

Code after:
        if (i && !(i % 100)) {
            printf(".");
            if (!(i % 1000)) {
                printf("%d%% done", i / 1000);
            }
            fflush(stdout);
        }
    }
    printf("\n");

    if (errors) {
        printf("!!! WARNING !!!: %d errors found\n", errors);
    } else {
        printf("OK! \\o/\n");
    }
    return !!errors;
}

/* This test verifies that raxRemove correctly handles compression when two keys
 * share a common prefix. Upon deletion of one key, rax attempts to recompress
 * the structure back to its original form for other key. Historically, there was
 * a crash when deleting one key because rax would attempt to recompress the
 * structure without checking the 512MB size limit.
 *
 * This test is disabled by default because it uses a lot of memory. */
int test_raxRecompressHugeKey(int argc, char **argv, int flags) {
    UNUSED(argc);
    UNUSED(argv);

    if (!(flags & UNIT_TEST_LARGE_MEMORY)) return 0;

    rax *rt = raxNew();

    /* Insert small keys */
    char small_key[32];
    const char *small_prefix = ",({5oM}";
    int i;
    for (i = 1; i <= 20; i++) {
        snprintf(small_key, sizeof(small_key), "%s%d", small_prefix, i);
        size_t keylen = strlen(small_key);
        raxInsert(rt, (unsigned char *)small_key, keylen,
                  (void *)(long)i, NULL);
    }

    /* Insert large key exceeding compressed node size limit */
    size_t max_keylen = ((1 << 29) - 1) + 100; // Compressed node limit + overflow
    const char *large_prefix = ",({ABC}";
    unsigned char *large_key = zmalloc(max_keylen + strlen(large_prefix));
    if (!large_key) {
        fprintf(stderr, "Failed to allocate memory for large key\n");
        raxFree(rt);