Skip to content

Commit 4e54d23

Browse files
committed
pg_upgrade: further tweaking of make_outputdirs().
Use the same error message for all cases of pathname overrun, since users aren't going to much care which one was too long. Add missing newline to said error (as pg_upgrade's version of pg_fatal requires that). Add pathname overrun checks for the individual log files, not just the directories. Remove initial newline in log files; the new scheme here guarantees that we'll never be appending to an old file. Kyotaro Horiguchi and Tom Lane Discussion: https://postgr.es/m/20220613.120551.729848632120189555.horikyota.ntt@gmail.com
1 parent 19408aa commit 4e54d23

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/bin/pg_upgrade/pg_upgrade.c

+14-10
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ make_outputdirs(char *pgdata)
228228
log_opts.rootdir = (char *) pg_malloc0(MAXPGPATH);
229229
len = snprintf(log_opts.rootdir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
230230
if (len >= MAXPGPATH)
231-
pg_fatal("buffer for root directory too small");
231+
pg_fatal("directory path for new cluster is too long\n");
232232

233233
/* BASE_OUTPUTDIR/$timestamp/ */
234234
gettimeofday(&time, NULL);
@@ -241,21 +241,21 @@ make_outputdirs(char *pgdata)
241241
len = snprintf(log_opts.basedir, MAXPGPATH, "%s/%s", log_opts.rootdir,
242242
timebuf);
243243
if (len >= MAXPGPATH)
244-
pg_fatal("buffer for base directory too small");
244+
pg_fatal("directory path for new cluster is too long\n");
245245

246246
/* BASE_OUTPUTDIR/$timestamp/dump/ */
247247
log_opts.dumpdir = (char *) pg_malloc0(MAXPGPATH);
248248
len = snprintf(log_opts.dumpdir, MAXPGPATH, "%s/%s/%s", log_opts.rootdir,
249249
timebuf, DUMP_OUTPUTDIR);
250250
if (len >= MAXPGPATH)
251-
pg_fatal("buffer for dump directory too small");
251+
pg_fatal("directory path for new cluster is too long\n");
252252

253253
/* BASE_OUTPUTDIR/$timestamp/log/ */
254254
log_opts.logdir = (char *) pg_malloc0(MAXPGPATH);
255255
len = snprintf(log_opts.logdir, MAXPGPATH, "%s/%s/%s", log_opts.rootdir,
256256
timebuf, LOG_OUTPUTDIR);
257257
if (len >= MAXPGPATH)
258-
pg_fatal("buffer for log directory too small");
258+
pg_fatal("directory path for new cluster is too long\n");
259259

260260
/*
261261
* Ignore the error case where the root path exists, as it is kept the
@@ -270,21 +270,25 @@ make_outputdirs(char *pgdata)
270270
if (mkdir(log_opts.logdir, pg_dir_create_mode) < 0)
271271
pg_fatal("could not create directory \"%s\": %m\n", log_opts.logdir);
272272

273-
snprintf(filename_path, sizeof(filename_path), "%s/%s", log_opts.logdir,
274-
INTERNAL_LOG_FILE);
273+
len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
274+
log_opts.logdir, INTERNAL_LOG_FILE);
275+
if (len >= sizeof(filename_path))
276+
pg_fatal("directory path for new cluster is too long\n");
277+
275278
if ((log_opts.internal = fopen_priv(filename_path, "a")) == NULL)
276279
pg_fatal("could not open log file \"%s\": %m\n", filename_path);
277280

278281
/* label start of upgrade in logfiles */
279282
for (filename = output_files; *filename != NULL; filename++)
280283
{
281-
snprintf(filename_path, sizeof(filename_path), "%s/%s",
282-
log_opts.logdir, *filename);
284+
len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
285+
log_opts.logdir, *filename);
286+
if (len >= sizeof(filename_path))
287+
pg_fatal("directory path for new cluster is too long\n");
283288
if ((fp = fopen_priv(filename_path, "a")) == NULL)
284289
pg_fatal("could not write to log file \"%s\": %m\n", filename_path);
285290

286-
/* Start with newline because we might be appending to a file. */
287-
fprintf(fp, "\n"
291+
fprintf(fp,
288292
"-----------------------------------------------------------------\n"
289293
" pg_upgrade run on %s"
290294
"-----------------------------------------------------------------\n\n",

0 commit comments

Comments
 (0)