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

Code before:
            }
        } else {
            freqs[i] = reply->integer;
        }
        freeReplyObject(reply);
    }
}

static void findHotKeys(void) {
    redisReply *keys, *reply;
    unsigned long long *counters = NULL;
    sds *hotkeys = NULL;
    unsigned long long sampled = 0, total_keys, *freqs = NULL, it = 0, scan_loops = 0;
    unsigned int arrsize = 0, i, k;
    double pct;

    counters = zrealloc(counters, sizeof(unsigned long long) * config.hotkeys_count);
    if (!counters) {
        fprintf(stderr, "Memory allocated failed!\n");
        exit(1);
    }
    hotkeys = zrealloc(hotkeys, sizeof(sds) * config.hotkeys_count);
    if (!hotkeys) {
        fprintf(stderr, "Memory allocated failed!\n");
        exit(1);
    }
    unsigned long long nums;
    for (nums = 0; nums < config.hotkeys_count; nums++) {
        counters[nums] = 0;
        hotkeys[nums] = NULL;
    }

    signal(SIGINT, longStatLoopModeStop);
    /* Total keys pre scanning */
    total_keys = getDbSize();

    /* Status message */
    printf("\n# Scanning the entire keyspace to find hot keys as well as\n");
    printf("# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec\n");
    printf("# per 100 SCAN commands (not usually needed).\n\n");

    /* Use readonly in cluster */
    sendReadOnly();

    /* SCAN loop */
    do {
        /* Calculate approximate percentage completion */
        pct = 100 * (double)sampled / total_keys;

        /* Grab some keys and point to the keys array */
        reply = sendScan(&it);

Review comment (bug):
`zrealloc()` never returns NULL. If it fails to allocate, it calls the oom handler which exits the program:

```
static void zmalloc_default_oom(size_t size) {
    fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n", size);
    fflush(stderr);
    abort();
}
```

So we can delete the error handling here.

```suggestion
    counters = zrealloc(counters, sizeof(unsigned long long) * config.hotkeys_count);
    hotkeys = zrealloc(hotkeys, sizeof(sds) * config.hotkeys_count);
```

Code after:
            }
        } else {
            freqs[i] = reply->integer;
        }
        freeReplyObject(reply);
    }
}

static void findHotKeys(void) {
    redisReply *keys, *reply;
    unsigned long long *counters = NULL;
    sds *hotkeys = NULL;
    unsigned long long sampled = 0, total_keys, *freqs = NULL, it = 0, scan_loops = 0;
    unsigned int arrsize = 0, i, k;
    double pct;

    counters = zrealloc(counters, sizeof(unsigned long long) * config.hotkeys_count);
    hotkeys = zrealloc(hotkeys, sizeof(sds) * config.hotkeys_count);
    unsigned long long nums;
    for (nums = 0; nums < config.hotkeys_count; nums++) {
        counters[nums] = 0;
        hotkeys[nums] = NULL;
    }

    signal(SIGINT, longStatLoopModeStop);
    /* Total keys pre scanning */
    total_keys = getDbSize();

    /* Status message */
    printf("\n# Scanning the entire keyspace to find hot keys as well as\n");
    printf("# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec\n");
    printf("# per 100 SCAN commands (not usually needed).\n\n");

    /* Use readonly in cluster */
    sendReadOnly();

    /* SCAN loop */
    do {
        /* Calculate approximate percentage completion */
        pct = 100 * (double)sampled / total_keys;

        /* Grab some keys and point to the keys array */
        reply = sendScan(&it);
        scan_loops++;
        keys = reply->element[1];

        /* Reallocate our freqs array if we need to */
        if (keys->elements > arrsize) {
            freqs = zrealloc(freqs, sizeof(unsigned long long) * keys->elements);

            if (!freqs) {