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

Code before:
            /* Load encoded strings */
            if ((field = rdbGenericLoadStringObject(rdb, RDB_LOAD_SDS, NULL)) == NULL) {
                decrRefCount(o);
                return NULL;
            }
            if ((value = rdbGenericLoadStringObject(rdb, RDB_LOAD_SDS, NULL)) == NULL) {
                sdsfree(field);
                decrRefCount(o);
                return NULL;
            }

            /* Also load the entry expiry */
            long long itemexpiry = EXPIRY_NONE;
            if (rdbtype == RDB_TYPE_HASH_2) {
                itemexpiry = rdbLoadMillisecondTime(rdb, RDB_VERSION);
                if (itemexpiry < EXPIRY_NONE || rioGetReadError(rdb)) {
                    sdsfree(field);
                    sdsfree(value);
                    decrRefCount(o);
                    return NULL;
                }
            }

            /* If this is a non-preamble RDB being loaded on the primary, and this
             * field is already expired relative to 'now', skip it. */
            if (iAmPrimary() && !(rdbflags & RDBFLAGS_AOF_PREAMBLE) && now != 0 &&
                itemexpiry != EXPIRY_NONE && itemexpiry < now) {
                /* Emit HDEL to replicas. */
                if ((rdbflags & RDBFLAGS_FEED_REPL) && server.repl_backlog) {
                    robj keyobj, fieldobj;
                    initStaticStringObject(keyobj, key);
                    initStaticStringObject(fieldobj, field);
                    robj *argv[3];
                    argv[0] = shared.hdel; /* shared.hdel should exist */
                    argv[1] = &keyobj;
                    argv[2] = &fieldobj;
                    replicationFeedReplicas(dbid, argv, 3);
                }
                /* free the pair */
                sdsfree(field);
                sdsfree(value);
                /* continue to next field */
                continue;
            }

            /* Add pair to hash table */
            entry *entry = entryCreate(field, value, itemexpiry);
            sdsfree(field);
            if (!hashtableAdd((hashtable *)objectGetVal(o), entry)) {
                rdbReportCorruptRDB("Duplicate hash fields detected");
                entryFree(entry);

Review comment (suggestion):
another concern here, is that the object might be left empty follow the load. we need to handle the case of an object loaded empty when all it's fields expired (and add a test for that)

Code after:
            /* Load encoded strings */
            if ((field = rdbGenericLoadStringObject(rdb, RDB_LOAD_SDS, NULL)) == NULL) {
                decrRefCount(o);
                return NULL;
            }
            if ((value = rdbGenericLoadStringObject(rdb, RDB_LOAD_SDS, NULL)) == NULL) {
                sdsfree(field);
                decrRefCount(o);
                return NULL;
            }

            /* Also load the entry expiry */
            long long itemexpiry = EXPIRY_NONE;
            if (rdbtype == RDB_TYPE_HASH_2) {
                itemexpiry = rdbLoadMillisecondTime(rdb, RDB_VERSION);
                if (itemexpiry < EXPIRY_NONE || rioGetReadError(rdb)) {
                    sdsfree(field);
                    sdsfree(value);
                    decrRefCount(o);
                    return NULL;
                }
            }

            /* If this is a non-preamble RDB being loaded on the primary, and this
             * field is already expired relative to 'now', skip it. */
            if (iAmPrimary() && !(rdbflags & RDBFLAGS_AOF_PREAMBLE) && now != 0 &&
                itemexpiry != EXPIRY_NONE && itemexpiry < now) {
                /* Emit HDEL to replicas. */
                if ((rdbflags & RDBFLAGS_FEED_REPL) && server.repl_backlog) {
                    robj keyobj, fieldobj;
                    initStaticStringObject(keyobj, key);
                    initStaticStringObject(fieldobj, field);
                    robj *argv[3];
                    argv[0] = shared.hdel;
                    argv[1] = &keyobj;
                    argv[2] = &fieldobj;
                    replicationFeedReplicas(dbid, argv, 3);
                }
                sdsfree(field);
                sdsfree(value);
                continue;
            }

            /* Add pair to hash table */
            entry *entry = entryCreate(field, value, itemexpiry);
            sdsfree(field);
            if (!hashtableAdd((hashtable *)objectGetVal(o), entry)) {
                rdbReportCorruptRDB("Duplicate hash fields detected");
                entryFree(entry);
                decrRefCount(o);
                return NULL;