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

Code before:
    }

    /* If the instance is a replica we need a connected primary
     * in order to fetch the currently selected DB. */
    if (server.primary) {
        rsi->repl_stream_db = server.primary->db->id;
        return rsi;
    }

    /* If we have a cached primary we can use it in order to populate the
     * replication selected DB info inside the RDB file: the replica can
     * increment the primary_repl_offset only from data arriving from the
     * primary, so if we are disconnected the offset in the cached primary
     * is valid. */
    if (server.cached_primary) {
        rsi->repl_stream_db = server.cached_primary->db->id;
        return rsi;
    }
    return NULL;
}

/* Restore the replication ID / offset from the RDB file
 * return 1 if rdbSaveInfo is valid */
int replicationRestoreOffsetFromSaveInfo(rdbSaveInfo *rsi, bool is_aof_preamble) {
    int rsi_is_valid = 0;
    if (rsi == NULL) return rsi_is_valid;
    if (rsi->repl_id_is_set && rsi->repl_offset != -1 && rsi->repl_stream_db != -1) {
        /* Note that older implementations may save a repl_stream_db
         * of -1 inside the RDB file in a wrong way, see more
         * information in function rdbPopulateSaveInfo. */
        rsi_is_valid = 1;
        if (!iAmPrimary()) {
            memcpy(server.replid, rsi->repl_id, sizeof(server.replid));
            server.primary_repl_offset = rsi->repl_offset;
            if (!is_aof_preamble || (!server.primary && !server.cached_primary)) {
                /* If this is a replica, create a cached primary from this
                 * information, in order to allow partial resynchronizations
                 * with primaries. For AOF, only cache the primary if replica
                 * has not synced to its primary node yet. */
                replicationCachePrimaryUsingMyself();
                selectDb(server.cached_primary, rsi->repl_stream_db);
            }
        } else {
            /* If this is a primary, we can save the replication info
             * as secondary ID and offset, in order to allow replicas
             * to partial resynchronizations with primaries. */
            memcpy(server.replid2, rsi->repl_id, sizeof(server.replid));
            server.second_replid_offset = rsi->repl_offset + 1;
            /* Rebase primary_repl_offset from rsi.repl_offset. */
            server.primary_repl_offset += rsi->repl_offset;
            serverAssert(server.repl_backlog);

Review comment (suggestion):
We always pass a valid `rsi` pointer, don't we? No need to allow passing NULL. We can do an assert instead.

```suggestion
    serverAssert(rsi != NULL);
```

Code after:
    }

    /* If the instance is a replica we need a connected primary
     * in order to fetch the currently selected DB. */
    if (server.primary) {
        rsi->repl_stream_db = server.primary->db->id;
        return rsi;
    }

    /* If we have a cached primary we can use it in order to populate the
     * replication selected DB info inside the RDB file: the replica can
     * increment the primary_repl_offset only from data arriving from the
     * primary, so if we are disconnected the offset in the cached primary
     * is valid. */
    if (server.cached_primary) {
        rsi->repl_stream_db = server.cached_primary->db->id;
        return rsi;
    }
    return NULL;
}

/* Restore the replication ID / offset from the RDB file
 * return 1 if replication ID and offset were restored from the rdbSaveInfo */
int rdbRestoreOffsetFromSaveInfo(rdbSaveInfo *rsi, bool is_aof_preamble) {
    int rsi_is_valid = 0;
    serverAssert(rsi != NULL);
    if (rsi->repl_id_is_set && rsi->repl_offset != -1 && rsi->repl_stream_db != -1) {
        /* Note that older implementations may save a repl_stream_db
         * of -1 inside the RDB file in a wrong way, see more
         * information in function rdbPopulateSaveInfo. */
        rsi_is_valid = 1;
        if (!iAmPrimary()) {
            memcpy(server.replid, rsi->repl_id, sizeof(server.replid));
            server.primary_repl_offset = rsi->repl_offset;
            if (!is_aof_preamble || (!server.primary && !server.cached_primary)) {
                /* If this is a replica, create a cached primary from this
                 * information, in order to allow partial resynchronizations
                 * with primaries. For AOF, only cache the primary if replica
                 * has not synced to its primary node yet. */
                replicationCachePrimaryUsingMyself();
                selectDb(server.cached_primary, rsi->repl_stream_db);
            }
        } else {
            /* If this is a primary, we can save the replication info
             * as secondary ID and offset, in order to allow replicas
             * to partial resynchronizations with primaries. */
            memcpy(server.replid2, rsi->repl_id, sizeof(server.replid));
            server.second_replid_offset = rsi->repl_offset + 1;
            /* Rebase primary_repl_offset from rsi.repl_offset. */
            server.primary_repl_offset += rsi->repl_offset;
            serverAssert(server.repl_backlog);