Skip to content

Commit 48d67fd

Browse files
committed
Fix race condition in psql \e's detection of file modification.
psql's editing commands decide whether the user has edited the file by checking for change of modification timestamp. This is probably fine for a pre-existing file, but with a temporary file that is created within the command, it's possible for a fast typist to save-and-exit in less than the one-second granularity of stat(2) timestamps. On Windows FAT filesystems the granularity is even worse, 2 seconds, making the race a bit easier to hit. To fix, try to set the temp file's mod time to be two seconds ago. It's unlikely this would fail, but then again the race condition itself is unlikely, so just ignore any error. Also, we might as well check the file size as well as its mod time. While this is a difficult bug to hit, it still seems worth back-patching, to ensure that users' edits aren't lost. Laurenz Albe, per gripe from Jacob Champion; based on fix suggestions from Jacob and myself Discussion: https://postgr.es/m/0ba3f2a658bac6546d9934ab6ba63a805d46a49b.camel@cybertec.at
1 parent f52c5d6 commit 48d67fd

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/bin/psql/command.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <ctype.h>
1111
#include <time.h>
1212
#include <pwd.h>
13+
#include <utime.h>
1314
#ifndef WIN32
1415
#include <sys/stat.h> /* for stat() */
1516
#include <fcntl.h> /* open() flags */
@@ -3709,7 +3710,6 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
37093710
const char *fname;
37103711
bool error = false;
37113712
int fd;
3712-
37133713
struct stat before,
37143714
after;
37153715

@@ -3734,13 +3734,13 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
37343734
!ret ? strerror(errno) : "");
37353735
return false;
37363736
}
3737+
#endif
37373738

37383739
/*
37393740
* No canonicalize_path() here. EDIT.EXE run from CMD.EXE prepends the
37403741
* current directory to the supplied path unless we use only
37413742
* backslashes, so we do that.
37423743
*/
3743-
#endif
37443744
#ifndef WIN32
37453745
snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
37463746
"/", (int) getpid());
@@ -3790,6 +3790,24 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
37903790
pg_log_error("%s: %m", fname);
37913791
error = true;
37923792
}
3793+
else
3794+
{
3795+
struct utimbuf ut;
3796+
3797+
/*
3798+
* Try to set the file modification time of the temporary file
3799+
* a few seconds in the past. Otherwise, the low granularity
3800+
* (one second, or even worse on some filesystems) that we can
3801+
* portably measure with stat(2) could lead us to not
3802+
* recognize a modification, if the user typed very quickly.
3803+
*
3804+
* This is a rather unlikely race condition, so don't error
3805+
* out if the utime(2) call fails --- that would make the cure
3806+
* worse than the disease.
3807+
*/
3808+
ut.modtime = ut.actime = time(NULL) - 2;
3809+
(void) utime(fname, &ut);
3810+
}
37933811
}
37943812
}
37953813

@@ -3809,7 +3827,10 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
38093827
error = true;
38103828
}
38113829

3812-
if (!error && before.st_mtime != after.st_mtime)
3830+
/* file was edited if the size or modification time has changed */
3831+
if (!error &&
3832+
(before.st_size != after.st_size ||
3833+
before.st_mtime != after.st_mtime))
38133834
{
38143835
stream = fopen(fname, PG_BINARY_R);
38153836
if (!stream)

0 commit comments

Comments
 (0)