Skip to content

Commit 20fe870

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 f9b50b7 commit 20fe870

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
@@ -566,31 +566,29 @@ pgstat_reset_remove_files(const char *directory)
566566
dir = AllocateDir(directory);
567567
while ((entry = ReadDir(dir, directory)) != NULL)
568568
{
569-
int nitems;
570-
Oid tmp_oid;
571-
char tmp_type[8];
572-
char tmp_rest[2];
573-
574-
if (strncmp(entry->d_name, ".", 2) == 0 ||
575-
strncmp(entry->d_name, "..", 3) == 0)
576-
continue;
569+
int nchars;
570+
Oid tmp_oid;
577571

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

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

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

0 commit comments

Comments
 (0)