waldump: fix use-after-free in search_directory().
authorAndres Freund
Wed, 23 Mar 2022 23:38:43 +0000 (16:38 -0700)
committerAndres Freund
Mon, 28 Mar 2022 01:15:14 +0000 (18:15 -0700)
After closedir() dirent->d_name is not valid anymore. As there alerady are a
few places relying on the limited lifetime of pg_waldump, do so here as well,
and just pg_strdup() the string.

The bug was introduced in fc49e24fa69a.

Found by UBSan, run locally.

Backpatch: 11-, like fc49e24fa69 itself.

src/bin/pg_waldump/pg_waldump.c

index 3730156e100e089cb9e71c0df7b5995a29e12e10..f6cce2442c0be7753d1699fa919ecf7e0a542d1f 100644 (file)
@@ -177,7 +177,7 @@ search_directory(const char *directory, const char *fname)
            if (IsXLogFileName(xlde->d_name))
            {
                fd = open_file_in_directory(directory, xlde->d_name);
-               fname = xlde->d_name;
+               fname = pg_strdup(xlde->d_name);
                break;
            }
        }