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

Commit b654714

Browse files
committed
Fix failures to ignore \r when reading Windows-style newlines.
libpq failed to ignore Windows-style newlines in connection service files. This normally wasn't a problem on Windows itself, because fgets() would convert \r\n to just \n. But if libpq were running inside a program that changes the default fopen mode to binary, it would see the \r's and think they were data. In any case, it's project policy to ignore \r in text files unconditionally, because people sometimes try to use files with DOS-style newlines on Unix machines, where the C library won't hide that from us. Hence, adjust parseServiceFile() to ignore \r as well as \n at the end of the line. In HEAD, go a little further and make it ignore all trailing whitespace, to match what it's always done with leading whitespace. In HEAD, also run around and fix up everyplace where we have newline-chomping code to make all those places look consistent and uniformly drop \r. It is not clear whether any of those changes are fixing live bugs. Most of the non-cosmetic changes are in places that are reading popen output, and the jury is still out as to whether popen on Windows can return \r\n. (The Windows-specific code in pipe_read_line seems to think so, but our lack of support for this elsewhere suggests maybe it's not a problem in practice.) Hence, I desisted from applying those changes to back branches, except in run_ssl_passphrase_command() which is new enough and little-tested enough that we'd probably not have heard about any problems there. Tom Lane and Michael Paquier, per bug #15827 from Jorge Gustavo Rocha. Back-patch the parseServiceFile() change to all supported branches, and the run_ssl_passphrase_command() change to v11 where that was added. Discussion: https://postgr.es/m/15827-e6ba53a3a7ed543c@postgresql.org
1 parent 20e99cd commit b654714

File tree

7 files changed

+49
-33
lines changed

7 files changed

+49
-33
lines changed

src/backend/libpq/be-secure-common.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf,
112112
goto error;
113113
}
114114

115-
/* strip trailing newline */
115+
/* strip trailing newline, including \r in case we're on Windows */
116116
len = strlen(buf);
117-
if (len > 0 && buf[len - 1] == '\n')
117+
while (len > 0 && (buf[len - 1] == '\n' ||
118+
buf[len - 1] == '\r'))
118119
buf[--len] = '\0';
119120

120121
error:

src/bin/pg_ctl/pg_ctl.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,7 @@ adjust_data_dir(void)
21762176
filename[MAXPGPATH],
21772177
*my_exec_path;
21782178
FILE *fd;
2179+
int len;
21792180

21802181
/* do nothing if we're working without knowledge of data dir */
21812182
if (pg_config == NULL)
@@ -2218,9 +2219,12 @@ adjust_data_dir(void)
22182219
pclose(fd);
22192220
free(my_exec_path);
22202221

2221-
/* Remove trailing newline */
2222-
if (strchr(filename, '\n') != NULL)
2223-
*strchr(filename, '\n') = '\0';
2222+
/* Remove trailing newline, handling Windows newlines as well */
2223+
len = strlen(filename);
2224+
while (len > 0 &&
2225+
(filename[len - 1] == '\n' ||
2226+
filename[len - 1] == '\r'))
2227+
filename[--len] = '\0';
22242228

22252229
free(pg_data);
22262230
pg_data = pg_strdup(filename);

src/bin/pg_resetwal/pg_resetwal.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,10 @@ CheckDataVersion(void)
559559

560560
/* remove trailing newline, handling Windows newlines as well */
561561
len = strlen(rawline);
562-
if (len > 0 && rawline[len - 1] == '\n')
563-
{
562+
while (len > 0 &&
563+
(rawline[len - 1] == '\n' ||
564+
rawline[len - 1] == '\r'))
564565
rawline[--len] = '\0';
565-
if (len > 0 && rawline[len - 1] == '\r')
566-
rawline[--len] = '\0';
567-
}
568566

569567
if (strcmp(rawline, PG_MAJORVERSION) != 0)
570568
{

src/bin/pg_upgrade/option.c

+15-6
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ adjust_data_dir(ClusterInfo *cluster)
405405
cmd_output[MAX_STRING];
406406
FILE *fp,
407407
*output;
408+
int len;
408409

409410
/* Initially assume config dir and data dir are the same */
410411
cluster->pgconfig = pg_strdup(cluster->pgdata);
@@ -445,9 +446,12 @@ adjust_data_dir(ClusterInfo *cluster)
445446

446447
pclose(output);
447448

448-
/* Remove trailing newline */
449-
if (strchr(cmd_output, '\n') != NULL)
450-
*strchr(cmd_output, '\n') = '\0';
449+
/* Remove trailing newline, handling Windows newlines as well */
450+
len = strlen(cmd_output);
451+
while (len > 0 &&
452+
(cmd_output[len - 1] == '\n' ||
453+
cmd_output[len - 1] == '\r'))
454+
cmd_output[--len] = '\0';
451455

452456
cluster->pgdata = pg_strdup(cmd_output);
453457

@@ -508,10 +512,15 @@ get_sock_dir(ClusterInfo *cluster, bool live_check)
508512
sscanf(line, "%hu", &old_cluster.port);
509513
if (lineno == LOCK_FILE_LINE_SOCKET_DIR)
510514
{
515+
int len;
516+
511517
cluster->sockdir = pg_strdup(line);
512-
/* strip off newline */
513-
if (strchr(cluster->sockdir, '\n') != NULL)
514-
*strchr(cluster->sockdir, '\n') = '\0';
518+
/* strip off newline, handling Windows newlines as well */
519+
len = strlen(cluster->sockdir);
520+
while (len > 0 &&
521+
(cluster->sockdir[len - 1] == '\n' ||
522+
cluster->sockdir[len - 1] == '\r'))
523+
cluster->sockdir[--len] = '\0';
515524
}
516525
}
517526
fclose(fp);

src/bin/psql/prompt.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
264264
FILE *fd;
265265
char *file = pg_strdup(p + 1);
266266
int cmdend;
267+
int buflen;
267268

268269
cmdend = strcspn(file, "`");
269270
file[cmdend] = '\0';
@@ -274,8 +275,10 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
274275
buf[0] = '\0';
275276
pclose(fd);
276277
}
277-
if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
278-
buf[strlen(buf) - 1] = '\0';
278+
buflen = strlen(buf);
279+
while (buflen > 0 && (buf[buflen - 1] == '\n' ||
280+
buf[buflen - 1] == '\r'))
281+
buf[--buflen] = '\0';
279282
free(file);
280283
p += cmdend + 1;
281284
break;

src/interfaces/libpq/fe-connect.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -5020,6 +5020,8 @@ parseServiceFile(const char *serviceFile,
50205020

50215021
while ((line = fgets(buf, sizeof(buf), f)) != NULL)
50225022
{
5023+
int len;
5024+
50235025
linenr++;
50245026

50255027
if (strlen(line) >= sizeof(buf) - 1)
@@ -5032,16 +5034,17 @@ parseServiceFile(const char *serviceFile,
50325034
return 2;
50335035
}
50345036

5035-
/* ignore EOL at end of line */
5036-
if (strlen(line) && line[strlen(line) - 1] == '\n')
5037-
line[strlen(line) - 1] = 0;
5037+
/* ignore whitespace at end of line, especially the newline */
5038+
len = strlen(line);
5039+
while (len > 0 && isspace((unsigned char) line[len - 1]))
5040+
line[--len] = '\0';
50385041

5039-
/* ignore leading blanks */
5042+
/* ignore leading whitespace too */
50405043
while (*line && isspace((unsigned char) line[0]))
50415044
line++;
50425045

50435046
/* ignore comments and empty lines */
5044-
if (strlen(line) == 0 || line[0] == '#')
5047+
if (line[0] == '\0' || line[0] == '#')
50455048
continue;
50465049

50475050
/* Check for right groupname */
@@ -6910,14 +6913,10 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
69106913

69116914
len = strlen(buf);
69126915

6913-
/* Remove trailing newline */
6914-
if (len > 0 && buf[len - 1] == '\n')
6915-
{
6916+
/* Remove trailing newline, including \r in case we're on Windows */
6917+
while (len > 0 && (buf[len - 1] == '\n' ||
6918+
buf[len - 1] == '\r'))
69166919
buf[--len] = '\0';
6917-
/* Handle DOS-style line endings, too, even when not on Windows */
6918-
if (len > 0 && buf[len - 1] == '\r')
6919-
buf[--len] = '\0';
6920-
}
69216920

69226921
if (len == 0)
69236922
continue;

src/port/sprompt.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
144144
} while (buflen > 0 && buf[buflen - 1] != '\n');
145145
}
146146

147-
if (length > 0 && destination[length - 1] == '\n')
148-
/* remove trailing newline */
149-
destination[length - 1] = '\0';
147+
/* strip trailing newline, including \r in case we're on Windows */
148+
while (length > 0 &&
149+
(destination[length - 1] == '\n' ||
150+
destination[length - 1] == '\r'))
151+
destination[--length] = '\0';
150152

151153
if (!echo)
152154
{

0 commit comments

Comments
 (0)