Repository: redis/redis

Issue:
[BUG]redis-check-rdb can't check rdb with functions in unstable branch
[dumpV10.rdb.zip](https://github.com/redis/redis/files/7889863/dumpV10.rdb.zip)
**Describe the bug**

A short description of the bug.
```
Chens-MacBook-Pro:test leon$ ~/Workspace/redis/src/redis-check-rdb ./dumpV10.rdb
[offset 0] Checking RDB file ./dumpV10.rdb
[offset 32] AUX FIELD redis-ver = '255.255.255'
[offset 46] AUX FIELD redis-bits = '64'
[offset 58] AUX FIELD ctime = '1642516498'
[offset 73] AUX FIELD used-mem = '1733680'
[offset 91] AUX FIELD repl-stream-db = '0'
[offset 141] AUX FIELD repl-id = 'edc44ba1910aab71e20a847573a27454d3cd9880'
[offset 159] AUX FIELD repl-offset = '74954'
[offset 175] AUX FIELD aof-preamble = '0'
--- RDB ERROR DETECTED ---
[offset 176] Invalid object type: 246
[additional info] While doing: read-type
[additional info] Reading type 0 (string)
[info] 0 keys read
[info] 0 expires
[info] 0 already expired
```
**To reproduce**

see rdb in attachment

Steps to reproduce the behavior and/or a minimal code sample.

git revision number `99ab4236afb210dc118fad892210b9fbb369aa8e`

**Expected behavior**

A description of what you expected to happen.

no error

**Additional information**

Any additional information that is relevant to the problem.



Discussion:

Patch:
diff --git a/src/rdb.c b/src/rdb.c
index b13240f5b3a..ac5aa1f8692 100644
--- a/src/rdb.c
+++ b/src/rdb.c
@@ -2747,7 +2747,10 @@ void rdbLoadProgressCallback(rio *r, const void *buf, size_t len) {
 /* Save the given functions_ctx to the rdb.
  * The err output parameter is optional and will be set with relevant error
  * message on failure, it is the caller responsibility to free the error
- * message on failure. */
+ * message on failure.
+ *
+ * The lib_ctx argument is also optional. If NULL is given, only verify rdb
+ * structure with out performing the actual functions loading. */
 int rdbFunctionLoad(rio *rdb, int ver, functionsLibCtx* lib_ctx, int rdbflags, sds *err) {
     UNUSED(ver);
     sds name = NULL;
@@ -2782,11 +2785,13 @@ int rdbFunctionLoad(rio *rdb, int ver, functionsLibCtx* lib_ctx, int rdbflags, s
         goto error;
     }
 
-    if (functionsCreateWithLibraryCtx(name, engine_name, desc, blob, rdbflags & RDBFLAGS_ALLOW_DUP, &error, lib_ctx) != C_OK) {
-        if (!error) {
-            error = sdsnew("Failed creating the library");
+    if (lib_ctx) {
+        if (functionsCreateWithLibraryCtx(name, engine_name, desc, blob, rdbflags & RDBFLAGS_ALLOW_DUP, &error, lib_ctx) != C_OK) {
+            if (!error) {
+                error = sdsnew("Failed creating the library");
+            }
+            goto error;
         }
-        goto error;
     }
 
     res = C_OK;
diff --git a/src/redis-check-rdb.c b/src/redis-check-rdb.c
index eb41b0f6614..7fc798e45d0 100644
--- a/src/redis-check-rdb.c
+++ b/src/redis-check-rdb.c
@@ -303,6 +303,14 @@ int redis_check_rdb(char *rdbfilename, FILE *fp) {
             robj *o = rdbLoadCheckModuleValue(&rdb,name);
             decrRefCount(o);
             continue; /* Read type again. */
+        } else if (type == RDB_OPCODE_FUNCTION) {
+            sds err = NULL;
+            if (rdbFunctionLoad(&rdb, rdbver, NULL, 0, &err) != C_OK) {
+                rdbCheckError("Failed loading library, %s", err);
+                sdsfree(err);
+                goto err;
+            }
+            continue;
         } else {
             if (!rdbIsObjectType(type)) {
                 rdbCheckError("Invalid object type: %d", type);