Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb654714

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() wouldconvert \r\n to just \n. But if libpq were running inside a program thatchanges the default fopen mode to binary, it would see the \r's and thinkthey were data. In any case, it's project policy to ignore \r in textfiles unconditionally, because people sometimes try to use files withDOS-style newlines on Unix machines, where the C library won't hide thatfrom us.Hence, adjust parseServiceFile() to ignore \r as well as \n at the end ofthe line. In HEAD, go a little further and make it ignore all trailingwhitespace, to match what it's always done with leading whitespace.In HEAD, also run around and fix up everyplace where we havenewline-chomping code to make all those places look consistent anduniformly drop \r. It is not clear whether any of those changes arefixing live bugs. Most of the non-cosmetic changes are in places thatare reading popen output, and the jury is still out as to whether popenon Windows can return \r\n. (The Windows-specific code in pipe_read_lineseems to think so, but our lack of support for this elsewhere suggestsmaybe it's not a problem in practice.) Hence, I desisted from applyingthose changes to back branches, except in run_ssl_passphrase_command()which is new enough and little-tested enough that we'd probably not haveheard 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 parent20e99cd commitb654714

File tree

7 files changed

+49
-33
lines changed

7 files changed

+49
-33
lines changed

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

Lines changed: 3 additions & 2 deletions
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
gotoerror;
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

Lines changed: 7 additions & 3 deletions
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+
intlen;
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 15 additions & 6 deletions
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+
intlen;
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+
intlen;
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

Lines changed: 5 additions & 2 deletions
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
intcmdend;
267+
intbuflen;
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

Lines changed: 11 additions & 12 deletions
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+
intlen;
5024+
50235025
linenr++;
50245026

50255027
if (strlen(line) >=sizeof(buf)-1)
@@ -5032,16 +5034,17 @@ parseServiceFile(const char *serviceFile,
50325034
return2;
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((unsignedchar)line[len-1]))
5040+
line[--len]='\0';
50385041

5039-
/* ignore leadingblanks */
5042+
/* ignore leadingwhitespace too */
50405043
while (*line&&isspace((unsignedchar)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

Lines changed: 5 additions & 3 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp