Skip to content

Commit b8f2da0

Browse files
committed
Refactor logic to remove trailing CR/LF characters from strings
b654714 has reworked the way trailing CR/LF characters are removed from strings. This commit introduces a new routine in common/string.c and refactors the code so as the logic is in a single place, mostly. Author: Michael Paquier Reviewed-by: Bruce Momjian Discussion: https://postgr.es/m/20190801031820.GF29334@paquier.xyz
1 parent 28b901f commit b8f2da0

File tree

8 files changed

+45
-45
lines changed

8 files changed

+45
-45
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <sys/stat.h>
2323
#include <unistd.h>
2424

25+
#include "common/string.h"
2526
#include "libpq/libpq.h"
2627
#include "storage/fd.h"
2728

@@ -112,11 +113,8 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf,
112113
goto error;
113114
}
114115

115-
/* strip trailing newline, including \r in case we're on Windows */
116-
len = strlen(buf);
117-
while (len > 0 && (buf[len - 1] == '\n' ||
118-
buf[len - 1] == '\r'))
119-
buf[--len] = '\0';
116+
/* strip trailing newline and carriage return */
117+
len = pg_strip_crlf(buf);
120118

121119
error:
122120
pfree(command.data);

src/bin/pg_ctl/pg_ctl.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "common/controldata_utils.h"
2828
#include "common/file_perm.h"
2929
#include "common/logging.h"
30+
#include "common/string.h"
3031
#include "getopt_long.h"
3132
#include "utils/pidfile.h"
3233

@@ -2176,7 +2177,6 @@ adjust_data_dir(void)
21762177
filename[MAXPGPATH],
21772178
*my_exec_path;
21782179
FILE *fd;
2179-
int len;
21802180

21812181
/* do nothing if we're working without knowledge of data dir */
21822182
if (pg_config == NULL)
@@ -2219,12 +2219,8 @@ adjust_data_dir(void)
22192219
pclose(fd);
22202220
free(my_exec_path);
22212221

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';
2222+
/* strip trailing newline and carriage return */
2223+
(void) pg_strip_crlf(filename);
22282224

22292225
free(pg_data);
22302226
pg_data = pg_strdup(filename);

src/bin/pg_resetwal/pg_resetwal.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "common/file_perm.h"
5555
#include "common/logging.h"
5656
#include "common/restricted_token.h"
57+
#include "common/string.h"
5758
#include "storage/large_object.h"
5859
#include "pg_getopt.h"
5960
#include "getopt_long.h"
@@ -538,7 +539,6 @@ CheckDataVersion(void)
538539
const char *ver_file = "PG_VERSION";
539540
FILE *ver_fd;
540541
char rawline[64];
541-
int len;
542542

543543
if ((ver_fd = fopen(ver_file, "r")) == NULL)
544544
{
@@ -557,12 +557,8 @@ CheckDataVersion(void)
557557
exit(1);
558558
}
559559

560-
/* remove trailing newline, handling Windows newlines as well */
561-
len = strlen(rawline);
562-
while (len > 0 &&
563-
(rawline[len - 1] == '\n' ||
564-
rawline[len - 1] == '\r'))
565-
rawline[--len] = '\0';
560+
/* strip trailing newline and carriage return */
561+
(void) pg_strip_crlf(rawline);
566562

567563
if (strcmp(rawline, PG_MAJORVERSION) != 0)
568564
{

src/bin/pg_upgrade/option.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#endif
1616

1717
#include "getopt_long.h"
18+
#include "common/string.h"
1819
#include "utils/pidfile.h"
1920

2021
#include "pg_upgrade.h"
@@ -411,7 +412,6 @@ adjust_data_dir(ClusterInfo *cluster)
411412
cmd_output[MAX_STRING];
412413
FILE *fp,
413414
*output;
414-
int len;
415415

416416
/* Initially assume config dir and data dir are the same */
417417
cluster->pgconfig = pg_strdup(cluster->pgdata);
@@ -452,12 +452,8 @@ adjust_data_dir(ClusterInfo *cluster)
452452

453453
pclose(output);
454454

455-
/* Remove trailing newline, handling Windows newlines as well */
456-
len = strlen(cmd_output);
457-
while (len > 0 &&
458-
(cmd_output[len - 1] == '\n' ||
459-
cmd_output[len - 1] == '\r'))
460-
cmd_output[--len] = '\0';
455+
/* strip trailing newline and carriage return */
456+
(void) pg_strip_crlf(cmd_output);
461457

462458
cluster->pgdata = pg_strdup(cmd_output);
463459

@@ -518,15 +514,9 @@ get_sock_dir(ClusterInfo *cluster, bool live_check)
518514
sscanf(line, "%hu", &old_cluster.port);
519515
if (lineno == LOCK_FILE_LINE_SOCKET_DIR)
520516
{
521-
int len;
522-
517+
/* strip trailing newline and carriage return */
523518
cluster->sockdir = pg_strdup(line);
524-
/* strip off newline, handling Windows newlines as well */
525-
len = strlen(cluster->sockdir);
526-
while (len > 0 &&
527-
(cluster->sockdir[len - 1] == '\n' ||
528-
cluster->sockdir[len - 1] == '\r'))
529-
cluster->sockdir[--len] = '\0';
519+
(void) pg_strip_crlf(cluster->sockdir);
530520
}
531521
}
532522
fclose(fp);

src/bin/psql/prompt.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "prompt.h"
2323
#include "settings.h"
2424

25+
#include "common/string.h"
2526

2627
/*--------------------------
2728
* get_prompt
@@ -264,7 +265,6 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
264265
FILE *fd;
265266
char *file = pg_strdup(p + 1);
266267
int cmdend;
267-
int buflen;
268268

269269
cmdend = strcspn(file, "`");
270270
file[cmdend] = '\0';
@@ -275,10 +275,10 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
275275
buf[0] = '\0';
276276
pclose(fd);
277277
}
278-
buflen = strlen(buf);
279-
while (buflen > 0 && (buf[buflen - 1] == '\n' ||
280-
buf[buflen - 1] == '\r'))
281-
buf[--buflen] = '\0';
278+
279+
/* strip trailing newline and carriage return */
280+
(void) pg_strip_crlf(buf);
281+
282282
free(file);
283283
p += cmdend + 1;
284284
break;

src/common/string.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,25 @@ pg_clean_ascii(char *str)
9090
*p = '?';
9191
}
9292
}
93+
94+
95+
/*
96+
* pg_strip_crlf -- Remove any trailing newline and carriage return
97+
*
98+
* Removes any trailing newline and carriage return characters (\r on
99+
* Windows) in the input string, zero-terminating it.
100+
*
101+
* The passed in string must be zero-terminated. This function returns
102+
* the new length of the string.
103+
*/
104+
int
105+
pg_strip_crlf(char *str)
106+
{
107+
int len = strlen(str);
108+
109+
while (len > 0 && (str[len - 1] == '\n' ||
110+
str[len - 1] == '\r'))
111+
str[--len] = '\0';
112+
113+
return len;
114+
}

src/include/common/string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ extern bool pg_str_endswith(const char *str, const char *end);
1414
extern int strtoint(const char *pg_restrict str, char **pg_restrict endptr,
1515
int base);
1616
extern void pg_clean_ascii(char *str);
17+
extern int pg_strip_crlf(char *str);
1718

1819
#endif /* COMMON_STRING_H */

src/interfaces/libpq/fe-connect.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options,
7373
#include "common/ip.h"
7474
#include "common/link-canary.h"
7575
#include "common/scram-common.h"
76+
#include "common/string.h"
7677
#include "mb/pg_wchar.h"
7778
#include "port/pg_bswap.h"
7879

@@ -6911,12 +6912,8 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname,
69116912
if (fgets(buf, sizeof(buf), fp) == NULL)
69126913
break;
69136914

6914-
len = strlen(buf);
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'))
6919-
buf[--len] = '\0';
6915+
/* strip trailing newline and carriage return */
6916+
len = pg_strip_crlf(buf);
69206917

69216918
if (len == 0)
69226919
continue;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy