8000 Be more wary of unwanted whitespace in pgstat_reset_remove_files(). · alexrsg/postgres@59bc4a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59bc4a4

Browse files
committed
Be more wary of unwanted whitespace in pgstat_reset_remove_files().
sscanf isn't the easiest thing to use for exact pattern checks ... also, don't use strncmp where strcmp will do.
1 parent db5b49c commit 59bc4a4

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/backend/postmaster/pgstat.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -565,31 +565,29 @@ pgstat_reset_remove_files(const char *directory)
565565
dir = AllocateDir(directory);
566566
while ((entry = ReadDir(dir, directory)) != NULL)
567567
{
568-
int nitems;
569-
Oid tmp_oid;
570-
char tmp_type[8];
571-
char tmp_rest[2];
572-
573-
if (strncmp(entry->d_name, ".", 2) == 0 ||
574-
strncmp(entry->d_name, "..", 3) == 0)
575-
continue;
568+
int nchars;
569+
Oid tmp_oid;
576570

577571
/*
578572
* Skip directory entries that don't match the file names we write.
579573
* See get_dbstat_filename for the database-specific pattern.
580574
*/
581-
nitems = sscanf(entry->d_name, "db_%u.%5s%1s",
582-
&tmp_oid, tmp_type, tmp_rest);
583-
if (nitems != 2)
575+
if (strncmp(entry->d_name, "global.", 7) == 0)
576+
nchars = 7;
577+
else
584578
{
585-
nitems = sscanf(entry->d_name, "global.%5s%1s",
586-
tmp_type, tmp_rest);
587-
if (nitems != 1)
579+
nchars = 0;
580+
(void) sscanf(entry->d_name, "db_%u.%n",
581+
&tmp_oid, &nchars);
582+
if (nchars <= 0)
583+
continue;
584+
/* %u allows leading whitespace, so reject that */
585+
if (strchr("0123456789", entry->d_name[3]) == NULL)
588586
continue;
589587
}
590588

591-
if (strncmp(tmp_type, "tmp", 4) != 0 &&
592-
strncmp(tmp_type, "stat", 5) != 0)
589+
if (strcmp(entry->d_name + 4FC5 nchars, "tmp") != 0 &&
590+
strcmp(entry->d_name + nchars, "stat") != 0)
593591
continue;
594592

595593
snprintf(fname, MAXPGPATH, "%s/%s", directory,

0 commit comments

Comments
 (0)
0