Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f455fcf

Browse files
committed
Avoid unportable strftime() behavior in pg_dump/pg_dumpall.
Commit ad5d46a thought that we could get around the known portability issues of strftime's %Z specifier by using %z instead. However, that idea seems to have been innocent of any actual research, as it certainly missed the facts that (1) %z is not portable to pre-C99 systems, and (2) %z doesn't actually act differently from %Z on Windows anyway. Per failures on buildfarm member hamerkop. While at it, centralize the code defining what strftime format we want to use in pg_dump; three copies of that string seems a bit much.
1 parent 9711fa0 commit f455fcf

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/bin/pg_dump/dumputils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ typedef struct SimpleStringList
4949

5050
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
5151

52+
/*
53+
* Preferred strftime(3) format specifier for printing timestamps in pg_dump
54+
* and friends.
55+
*
56+
* We don't print the timezone on Windows, because the names are long and
57+
* localized, which means they may contain characters in various random
58+
* encodings; this has been seen to cause encoding errors when reading the
59+
* dump script. Think not to get around that by using %z, because
60+
* (1) %z is not portable to pre-C99 systems, and
61+
* (2) %z doesn't actually act differently from %Z on Windows anyway.
62+
*/
63+
#ifndef WIN32
64+
#define PGDUMP_STRFTIME_FMT "%Y-%m-%d %H:%M:%S %Z"
65+
#else
66+
#define PGDUMP_STRFTIME_FMT "%Y-%m-%d %H:%M:%S"
67+
#endif
68+
5269
extern int quote_all_identifiers;
5370
extern PQExpBuffer (*getLocalPQExpBuffer) (void);
5471

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,14 +1047,16 @@ PrintTOCSummary(Archive *AHX, RestoreOptions *ropt)
10471047
teSection curSection;
10481048
OutputContext sav;
10491049
const char *fmtName;
1050-
struct tm *tm = localtime(&AH->createDate);
10511050
char stamp_str[64];
10521051

10531052
sav = SaveOutput(AH);
10541053
if (ropt->filename)
10551054
SetOutput(AH, ropt->filename, 0 /* no compression */ );
10561055

1057-
strftime(stamp_str, sizeof(stamp_str), "%Y-%m-%d %H:%M:%S %z", tm);
1056+
if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT,
1057+
localtime(&AH->createDate)) == 0)
1058+
strcpy(stamp_str, "[unknown]");
1059+
10581060
ahprintf(AH, ";\n; Archive created at %s\n", stamp_str);
10591061
ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
10601062
AH->archdbname, AH->tocCount, AH->compression);
@@ -3544,7 +3546,7 @@ dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim)
35443546
{
35453547
char buf[64];
35463548

3547-
if (strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %z", localtime(&tim)) != 0)
3549+
if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&tim)) != 0)
35483550
ahprintf(AH, "-- %s %s\n\n", msg, buf);
35493551
}
35503552

src/bin/pg_dump/pg_dumpall.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem,
4848
const char *type, const char *name, const char *type2,
4949
const char *name2);
5050
static void dumpDatabases(PGconn *conn);
51-
static void dumpTimestamp(char *msg);
51+
static void dumpTimestamp(const char *msg);
5252
static void doShellQuoting(PQExpBuffer buf, const char *str);
5353
static void doConnStrQuoting(PQExpBuffer buf, const char *str);
5454

@@ -2058,12 +2058,12 @@ executeCommand(PGconn *conn, const char *query)
20582058
* dumpTimestamp
20592059
*/
20602060
static void
2061-
dumpTimestamp(char *msg)
2061+
dumpTimestamp(const char *msg)
20622062
{
20632063
char buf[64];
20642064
time_t now = time(NULL);
20652065

2066-
if (strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %z", localtime(&now)) != 0)
2066+
if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
20672067
fprintf(OPF, "-- %s %s\n\n", msg, buf);
20682068
}
20692069

0 commit comments

Comments
 (0)