Skip to content

Commit 940d136

Browse files
committed
Centralize single quote escaping in src/port/quotes.c
For code-reuse in upcoming functionality in pg_basebackup. Zoltan Boszormenyi
1 parent fc87450 commit 940d136

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed

src/bin/initdb/initdb.c

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,18 @@ pg_strdup(const char *s)
354354
return result;
355355
}
356356

357+
static char *
358+
escape_quotes(const char *src)
359+
{
360+
char *result = escape_single_quotes_ascii(src);
361+
if (!result)
362+
{
363+
fprintf(stderr, _("%s: out of memory\n"), progname);
364+
exit(1);
365+
}
366+
return result;
367+
}
368+
357369
/*
358370
* make a copy of the array of lines, with token replaced by replacement
359371
* the first time it occurs on each line.
@@ -2415,35 +2427,6 @@ check_ok(void)
24152427
}
24162428
}
24172429

2418-
/*
2419-
* Escape (by doubling) any single quotes or backslashes in given string
2420-
*
2421-
* Note: this is used to process both postgresql.conf entries and SQL
2422-
* string literals. Since postgresql.conf strings are defined to treat
2423-
* backslashes as escapes, we have to double backslashes here. Hence,
2424-
* when using this for a SQL string literal, use E'' syntax.
2425-
*
2426-
* We do not need to worry about encoding considerations because all
2427-
* valid backend encodings are ASCII-safe.
2428-
*/
2429-
static char *
2430-
escape_quotes(const char *src)
2431-
{
2432-
int len = strlen(src),
2433-
i,
2434-
j;
2435-
char *result = pg_malloc(len * 2 + 1);
2436-
2437-
for (i = 0, j = 0; i < len; i++)
2438-
{
2439-
if (SQL_STR_DOUBLE(src[i], true))
2440-
result[j++] = src[i];
2441-
result[j++] = src[i];
2442-
}
2443-
result[j] = '\0';
2444-
return result;
2445-
}
2446-
24472430
/* Hack to suppress a warning about %x from some versions of gcc */
24482431
static inline size_t
24492432
my_strftime(char *s, size_t max, const char *fmt, const struct tm * tm)

src/include/port.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,7 @@ extern int pg_check_dir(const char *dir);
465465
/* port/pgmkdirp.c */
466466
extern int pg_mkdir_p(char *path, int omode);
467467

468+
/* port/quotes.c */
469+
extern char *escape_single_quotes_ascii(const char *src);
470+
468471
#endif /* PG_PORT_H */

src/port/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LIBS += $(PTHREAD_LIBS)
3232

3333
OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \
3434
noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \
35-
pgstrcasecmp.o qsort.o qsort_arg.o sprompt.o tar.o thread.o
35+
pgstrcasecmp.o qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o
3636

3737
# foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
3838
OBJS_SRV = $(OBJS:%.o=%_srv.o)

src/port/quotes.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "c.h"
2+
3+
/*
4+
* Escape (by doubling) any single quotes or backslashes in given string
5+
*
6+
* Note: this is used to process postgresql.conf entries and to quote
7+
* string literals in pg_basebackup for creating recovery.conf.
8+
* Since postgresql.conf strings are defined to treat backslashes as escapes,
9+
* we have to double backslashes here.
10+
*
11+
* Since this function is only used for parsing or creating configuration
12+
* files, we do not care about encoding considerations.
13+
*
14+
* Returns a malloced() string that it's the responsibility of the caller
15+
* to free.
16+
*/
17+
char *
18+
escape_single_quotes_ascii(const char *src)
19+
{
20+
int len = strlen(src),
21+
i,
22+
j;
23+
char *result = malloc(len * 2 + 1);
24+
25+
if (!result)
26+
return NULL;
27+
28+
for (i = 0, j = 0; i < len; i++)
29+
{
30+
if (SQL_STR_DOUBLE(src[i], true))
31+
result[j++] = src[i];
32+
result[j++] = src[i];
33+
}
34+
result[j] = '\0';
35+
return result;
36+
}

src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ sub mkvcbuild
5959
chklocale.c crypt.c fls.c fseeko.c getrusage.c inet_aton.c random.c
6060
srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
6161
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c exec.c noblock.c path.c
62-
pgcheckdir.c pg_crc.c pgmkdirp.c pgsleep.c pgstrcasecmp.c qsort.c qsort_arg.c
62+
pgcheckdir.c pg_crc.c pgmkdirp.c pgsleep.c pgstrcasecmp.c qsort.c qsort_arg.c quotes.c
6363
sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c rint.c win32env.c
6464
win32error.c win32setlocale.c);
6565

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