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

Code before:
    sds command_response = aggregateClientOutputBuffer(caching_client);
    deleteCachedResponseClient(caching_client);
    return command_response;
}

int verifyCachedCommandResponse(sds cached_response, int resp) {
    sds generated_response = generateCommandResponse(resp);
    int is_equal = !sdscmp(generated_response, cached_response);
    /* Here, we use LL_WARNING so this gets printed when debug assertions are enabled and the system is about to crash. */
    if (!is_equal)
        serverLog(LL_WARNING, "\ngenerated_response:\n%s\n\ncached_response:\n%s", generated_response, cached_response);
    sdsfree(generated_response);
    return is_equal;
}

/* COMMAND (no args) */
void commandCommand(client *c) {
    /* Use cached response if available for the client's protocol version */
    int cache_idx = RESP_CACHE_INDEX(c->resp);
    sds cache = server.command_response_cache[cache_idx];

    if (!cache) {
        cache = generateCommandResponse(c->resp);
        server.command_response_cache[cache_idx] = cache;
    } else {
        debugServerAssertWithInfo(c, NULL, verifyCachedCommandResponse(cache, c->resp) == 1);
    }

    addReplyProto(c, cache, sdslen(cache));
}

/* COMMAND COUNT */
void commandCountCommand(client *c) {
    addReplyLongLong(c, hashtableSize(server.commands));
}

typedef enum {
    COMMAND_LIST_FILTER_MODULE,
    COMMAND_LIST_FILTER_ACLCAT,
    COMMAND_LIST_FILTER_PATTERN,
} commandListFilterType;

typedef struct {
    commandListFilterType type;
    sds arg;
    struct {
        int valid;
        union {
            uint64_t aclcat;
            void *module_handle;
        } u;

Review comment (bug):
```suggestion
        debugServerAssertWithInfo(c, NULL, verifyCachedCommandResponse(cache, c->resp));
```

Code after:
    sds command_response = aggregateClientOutputBuffer(caching_client);
    deleteCachedResponseClient(caching_client);
    return command_response;
}

int verifyCachedCommandResponse(sds cached_response, int resp) {
    sds generated_response = generateCommandResponse(resp);
    int is_equal = !sdscmp(generated_response, cached_response);
    /* Here, we use LL_WARNING so this gets printed when debug assertions are enabled and the system is about to crash. */
    if (!is_equal)
        serverLog(LL_WARNING, "\ngenerated_response:\n%s\n\ncached_response:\n%s", generated_response, cached_response);
    sdsfree(generated_response);
    return is_equal;
}

/* COMMAND (no args) */
void commandCommand(client *c) {
    /* Use cached response if available for the client's protocol version */
    int cache_idx = RESP_CACHE_INDEX(c->resp);
    sds cache = server.command_response_cache[cache_idx];

    if (!cache) {
        cache = generateCommandResponse(c->resp);
        server.command_response_cache[cache_idx] = cache;
    } else {
        debugServerAssertWithInfo(c, NULL, verifyCachedCommandResponse(cache, c->resp));
    }

    addReplyProto(c, cache, sdslen(cache));
}

/* COMMAND COUNT */
void commandCountCommand(client *c) {
    addReplyLongLong(c, hashtableSize(server.commands));
}

typedef enum {
    COMMAND_LIST_FILTER_MODULE,
    COMMAND_LIST_FILTER_ACLCAT,
    COMMAND_LIST_FILTER_PATTERN,
} commandListFilterType;

typedef struct {
    commandListFilterType type;
    sds arg;
    struct {
        int valid;
        union {
            uint64_t aclcat;
            void *module_handle;
        } u;