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.
This commit is contained in:
Tom Lane 2013-08-19 19:36:04 -04:00
parent f9b50b7c18
commit 20fe870753
1 changed files with 14 additions and 16 deletions

View File

@ -566,31 +566,29 @@ pgstat_reset_remove_files(const char *directory)
dir = AllocateDir(directory); dir = AllocateDir(directory);
while ((entry = ReadDir(dir, directory)) != NULL) while ((entry = ReadDir(dir, directory)) != NULL)
{ {
int nitems; int nchars;
Oid tmp_oid; Oid tmp_oid;
char tmp_type[8];
char tmp_rest[2];
if (strncmp(entry->d_name, ".", 2) == 0 ||
strncmp(entry->d_name, "..", 3) == 0)
continue;
/* /*
* Skip directory entries that don't match the file names we write. * Skip directory entries that don't match the file names we write.
* See get_dbstat_filename for the database-specific pattern. * See get_dbstat_filename for the database-specific pattern.
*/ */
nitems = sscanf(entry->d_name, "db_%u.%5s%1s", if (strncmp(entry->d_name, "global.", 7) == 0)
&tmp_oid, tmp_type, tmp_rest); nchars = 7;
if (nitems != 2) else
{ {
nitems = sscanf(entry->d_name, "global.%5s%1s", nchars = 0;
tmp_type, tmp_rest); (void) sscanf(entry->d_name, "db_%u.%n",
if (nitems != 1) &tmp_oid, &nchars);
if (nchars <= 0)
continue;
/* %u allows leading whitespace, so reject that */
if (strchr("0123456789", entry->d_name[3]) == NULL)
continue; continue;
} }
if (strncmp(tmp_type, "tmp", 4) != 0 && if (strcmp(entry->d_name + nchars, "tmp") != 0 &&
strncmp(tmp_type, "stat", 5) != 0) strcmp(entry->d_name + nchars, "stat") != 0)
continue; continue;
snprintf(fname, MAXPGPATH, "%s/%s", directory, snprintf(fname, MAXPGPATH, "%s/%s", directory,