Skip to content

Commit 33aa7d1

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 6c34f18 commit 33aa7d1

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
@@ -11,6 +11,7 @@
1111
#include <ctype.h>
1212
#include <time.h>
1313
#include <pwd.h>
14+
#include <utime.h>
1415
#ifndef WIN32
1516
#include <sys/stat.h> /* for stat() */
1617
#include <fcntl.h> /* open() flags */
@@ -3499,7 +3500,6 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
34993500
const char *fname;
35003501
bool error = false;
35013502
int fd;
3502-
35033503
struct stat before,
35043504
after;
35053505

@@ -3524,13 +3524,13 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
35243524
!ret ? strerror(errno) : "");
35253525
return false;
35263526
}
3527+
#endif
35273528

35283529
/*
35293530
* No canonicalize_path() here. EDIT.EXE run from CMD.EXE prepends the
35303531
* current directory to the supplied path unless we use only
35313532
* backslashes, so we do that.
35323533
*/
3533-
#endif
35343534
#ifndef WIN32
35353535
snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
35363536
"/", (int) getpid());
@@ -3579,6 +3579,24 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
35793579
psql_error("%s: %s\n", fname, strerror(errno));
35803580
error = true;
35813581
}
3582+
else
3583+
{
3584+
struct utimbuf ut;
3585+
3586+
/*
3587+
* Try to set the file modification time of the temporary file
3588+
* a few seconds in the past. Otherwise, the low granularity
3589+
* (one second, or even worse on some filesystems) that we can
3590+
* portably measure with stat(2) could lead us to not
3591+
* recognize a modification, if the user typed very quickly.
3592+
*
3593+
* This is a rather unlikely race condition, so don't error
3594+
* out if the utime(2) call fails --- that would make the cure
3595+
* worse than the disease.
3596+
*/
3597+
ut.modtime = ut.actime = time(NULL) - 2;
3598+
(void) utime(fname, &ut);
3599+
}
35823600
}
35833601
}
35843602

@@ -3598,7 +3616,10 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
35983616
error = true;
35993617
}
36003618

3601-
if (!error && before.st_mtime != after.st_mtime)
3619+
/* file was edited if the size or modification time has changed */
3620+
if (!error &&
3621+
(before.st_size != after.st_size ||
3622+
before.st_mtime != after.st_mtime))
36023623
{
36033624
stream = fopen(fname, PG_BINARY_R);
36043625
if (!stream)

0 commit comments

Comments
 (0)