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

Code before:
        rdb_loading_ctx->dbarray[0] = createDatabase(0);
    }
    serverDb *db = rdb_loading_ctx->dbarray[0];
    char buf[1024];
    int error;
    long long empty_keys_skipped = 0;
    bool is_valkey_magic = false, is_redis_magic = false;

    rdb->update_cksum = rdbLoadProgressCallback;
    rdb->max_processing_chunk = server.loading_process_events_interval_bytes;
    if (rioRead(rdb, buf, 9) == 0) goto eoferr;
    buf[9] = '\0';
    if (memcmp(buf, "REDIS0", 6) == 0) {
        is_redis_magic = true;
    } else if (memcmp(buf, "VALKEY", 6) == 0) {
        is_valkey_magic = true;
    } else {
        serverLog(LL_WARNING, "Wrong signature trying to load DB from file");
        // Return this error so we know to terminate the sync gracefully without emptyData()
        return RDB_INCOMPATIBLE; 
    }
    rdbver = atoi(buf + 6);
    if (!rdbIsVersionAccepted(rdbver, is_valkey_magic, is_redis_magic)) {
        serverLog(LL_WARNING, "Can't handle RDB format version %d", rdbver);
        return RDB_INCOMPATIBLE;
    }

    // Only empty data if empty data flag is set
    if (rdbflags & RDBFLAGS_EMPTY_DATA) {
        int empty_db_flags = server.repl_replica_lazy_flush ? EMPTYDB_ASYNC : EMPTYDB_NO_FLAGS;
        serverLog(LL_NOTICE, "RDB compatability check complete, flushing data %d", rdbver);
        emptyData(-1, empty_db_flags, replicationEmptyDbCallback);
    }

    /* Key-specific attributes, set by opcodes before the key type. */
    long long lru_idle = -1, lfu_freq = -1, expiretime = -1, now = mstime();
    long long lru_clock = LRU_CLOCK();

    while (1) {
        sds key;
        robj *val;

        /* Read type. */
        if ((type = rdbLoadType(rdb)) == -1) goto eoferr;

        /* Safeguard for unknown foreign opcode interpretations. */
        if (is_redis_magic && type >= RDB_FOREIGN_TYPE_MIN && type <= RDB_FOREIGN_TYPE_MAX) {
            serverLog(LL_WARNING, "Can't handle foreign type or opcode %d in RDB with version %d",
                      type, rdbver);
            return C_ERR;
        }

Review comment (bug):
Does this cover all cases? From a quick question, these flags match what is used in caller sites from replication.c. Caller sites in module.c and debug.c just use `EMPTYDB_NO_FLAGS` regardless of the config

Code after:
    int error;
    long long empty_keys_skipped = 0;
    bool is_valkey_magic = false, is_redis_magic = false;

    rdb->update_cksum = rdbLoadProgressCallback;
    rdb->max_processing_chunk = server.loading_process_events_interval_bytes;
    if (rioRead(rdb, buf, 9) == 0) goto eoferr;
    buf[9] = '\0';
    if (memcmp(buf, "REDIS0", 6) == 0) {
        is_redis_magic = true;
    } else if (memcmp(buf, "VALKEY", 6) == 0) {
        is_valkey_magic = true;
    } else {
        serverLog(LL_WARNING, "Wrong signature trying to load DB from file: %.9s", buf);
        /* Signal to terminate the rdbLoad without clearing existing data */
        return RDB_INCOMPATIBLE;
    }
    rdbver = atoi(buf + 6);
    if (!rdbIsVersionAccepted(rdbver, is_valkey_magic, is_redis_magic)) {
        serverLog(LL_WARNING, "Can't handle RDB format version %d", rdbver);
        return RDB_INCOMPATIBLE;
    }

    /* Only empty data if RDBFLAGS_EMPTY_DATA is set */
    if (rdbflags & RDBFLAGS_EMPTY_DATA) {
        int empty_db_flags = server.repl_replica_lazy_flush ? EMPTYDB_ASYNC : EMPTYDB_NO_FLAGS;
        serverLog(LL_NOTICE, "RDB signature and version check passed. Flushing old data");
        emptyData(-1, empty_db_flags, replicationEmptyDbCallback);

        /* functionsLibCtx is cleared when we call emptyData, reinitialize here. */
        rdb_loading_ctx->functions_lib_ctx = functionsLibCtxGetCurrent();
    }

    if (rdb_loading_ctx->dbarray[0] == NULL) {
        rdb_loading_ctx->dbarray[0] = createDatabase(0);
    }
    serverDb *db = rdb_loading_ctx->dbarray[0];

    /* Key-specific attributes, set by opcodes before the key type. */
    long long lru_idle = -1, lfu_freq = -1, expiretime = -1, now = mstime();
    long long lru_clock = LRU_CLOCK();

    while (1) {
        sds key;
        robj *val;

        /* Read type. */
        if ((type = rdbLoadType(rdb)) == -1) goto eoferr;

        /* Safeguard for unknown foreign opcode interpretations. */
        if (is_redis_magic && type >= RDB_FOREIGN_TYPE_MIN && type <= RDB_FOREIGN_TYPE_MAX) {