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

Code before:
}

int verifyCachedCommandInfoResponse(struct serverCommand *cmd, sds cached_response, int resp) {
    sds generated_response = generateCommandInfoResponse(cmd, 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;
}

/* Output the representation of a server command. Used by the COMMAND command and COMMAND INFO. */
void addReplyCommandInfo(client *c, struct serverCommand *cmd) {
    if (!cmd) {
        addReplyNull(c);
    } else {
        /* Use cached response if available for the client's protocol version */
        int cache_idx = RESP_CACHE_INDEX(c->resp);
        sds cache = cmd->info_cache[cache_idx];

        if (cache == NULL) {
            cache = generateCommandInfoResponse(cmd, c->resp);
            cmd->info_cache[cache_idx] = cache;
        } else {
            debugServerAssertWithInfo(c, NULL, verifyCachedCommandInfoResponse(cmd, cache, c->resp) == 1);
        }

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

/* Output the representation of a server command. Used by the COMMAND DOCS. */
void addReplyCommandDocs(client *c, struct serverCommand *cmd) {
    /* Count our reply len so we don't have to use deferred reply. */
    long maplen = 1;
    if (cmd->summary) maplen++;
    if (cmd->since) maplen++;
    if (cmd->flags & CMD_MODULE) maplen++;
    if (cmd->complexity) maplen++;
    if (cmd->doc_flags) maplen++;
    if (cmd->deprecated_since) maplen++;
    if (cmd->replaced_by) maplen++;
    if (cmd->history) maplen++;
#ifdef LOG_REQ_RES
    if (cmd->reply_schema) maplen++;
#endif
    if (cmd->args) maplen++;
    if (cmd->subcommands_ht) maplen++;
    addReplyMapLen(c, maplen);


Review comment (bug):
The return value of verifyCachedCommandInfoResponse is to be interpreted as a boolean (success/failure). I don't care if we use int or bool for this, but there's no need to compare that it's == 1.

```suggestion
            debugServerAssertWithInfo(c, NULL, verifyCachedCommandInfoResponse(cmd, cache, c->resp));
```

Code after:
}

int verifyCachedCommandInfoResponse(struct serverCommand *cmd, sds cached_response, int resp) {
    sds generated_response = generateCommandInfoResponse(cmd, 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;
}

/* Output the representation of a server command. Used by the COMMAND command and COMMAND INFO. */
void addReplyCommandInfo(client *c, struct serverCommand *cmd) {
    if (!cmd) {
        addReplyNull(c);
    } else {
        /* Use cached response if available for the client's protocol version */
        int cache_idx = RESP_CACHE_INDEX(c->resp);
        sds cache = cmd->info_cache[cache_idx];

        if (cache == NULL) {
            cache = generateCommandInfoResponse(cmd, c->resp);
            cmd->info_cache[cache_idx] = cache;
        } else {
            debugServerAssertWithInfo(c, NULL, verifyCachedCommandInfoResponse(cmd, cache, c->resp));
        }

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

/* Output the representation of a server command. Used by the COMMAND DOCS. */
void addReplyCommandDocs(client *c, struct serverCommand *cmd) {
    /* Count our reply len so we don't have to use deferred reply. */
    long maplen = 1;
    if (cmd->summary) maplen++;
    if (cmd->since) maplen++;
    if (cmd->flags & CMD_MODULE) maplen++;
    if (cmd->complexity) maplen++;
    if (cmd->doc_flags) maplen++;
    if (cmd->deprecated_since) maplen++;
    if (cmd->replaced_by) maplen++;
    if (cmd->history) maplen++;
#ifdef LOG_REQ_RES
    if (cmd->reply_schema) maplen++;
#endif
    if (cmd->args) maplen++;
    if (cmd->subcommands_ht) maplen++;
    addReplyMapLen(c, maplen);