Skip to content

Commit 220db7c

Browse files
committed
Simplify and standardize conversions between TEXT datums and ordinary C
strings. This patch introduces four support functions cstring_to_text, cstring_to_text_with_len, text_to_cstring, and text_to_cstring_buffer, and two macros CStringGetTextDatum and TextDatumGetCString. A number of existing macros that provided variants on these themes were removed. Most of the places that need to make such conversions now require just one function or macro call, in place of the multiple notational layers that used to be needed. There are no longer any direct calls of textout or textin, and we got most of the places that were using handmade conversions via memcpy (there may be a few still lurking, though). This commit doesn't make any serious effort to eliminate transient memory leaks caused by detoasting toasted text objects before they reach text_to_cstring. We changed PG_GETARG_TEXT_P to PG_GETARG_TEXT_PP in a few places where it was easy, but much more could be done. Brendan Jurd and Tom Lane
1 parent f948197 commit 220db7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+770
-1210
lines changed

contrib/adminpack/adminpack.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Author: Andreas Pflug <[email protected]>
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.10 2008/01/01 19:45:45 momjian Exp $
11+
* $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.11 2008/03/25 22:42:41 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -23,6 +23,7 @@
2323
#include "miscadmin.h"
2424
#include "postmaster/syslogger.h"
2525
#include "storage/fd.h"
26+
#include "utils/builtins.h"
2627
#include "utils/datetime.h"
2728

2829

@@ -68,11 +69,7 @@ typedef struct
6869
static char *
6970
convert_and_check_filename(text *arg, bool logAllowed)
7071
{
71-
int input_len = VARSIZE(arg) - VARHDRSZ;
72-
char *filename = palloc(input_len + 1);
73-
74-
memcpy(filename, VARDATA(arg), input_len);
75-
filename[input_len] = '\0';
72+
char *filename = text_to_cstring(arg);
7673

7774
canonicalize_path(filename); /* filename can change length here */
7875

contrib/chkpass/chkpass.c

+9-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
* http://www.druid.net/darcy/
66
*
7-
* $PostgreSQL: pgsql/contrib/chkpass/chkpass.c,v 1.19 2007/02/27 23:48:05 tgl Exp $
7+
* $PostgreSQL: pgsql/contrib/chkpass/chkpass.c,v 1.20 2008/03/25 22:42:41 tgl Exp $
88
* best viewed with tabs set to 4
99
*/
1010

@@ -17,6 +17,7 @@
1717
#endif
1818

1919
#include "fmgr.h"
20+
#include "utils/builtins.h"
2021

2122
PG_MODULE_MAGIC;
2223

@@ -124,15 +125,8 @@ Datum
124125
chkpass_rout(PG_FUNCTION_ARGS)
125126
{
126127
chkpass *password = (chkpass *) PG_GETARG_POINTER(0);
127-
text *result;
128-
int slen;
129128

130-
slen = strlen(password->password);
131-
result = (text *) palloc(VARHDRSZ + slen);
132-
SET_VARSIZE(result, VARHDRSZ + slen);
133-
memcpy(VARDATA(result), password->password, slen);
134-
135-
PG_RETURN_TEXT_P(result);
129+
PG_RETURN_TEXT_P(cstring_to_text(password->password));
136130
}
137131

138132

@@ -145,13 +139,10 @@ Datum
145139
chkpass_eq(PG_FUNCTION_ARGS)
146140
{
147141
chkpass *a1 = (chkpass *) PG_GETARG_POINTER(0);
148-
text *a2 = (text *) PG_GETARG_TEXT_P(1);
149-
char str[10];
150-
int sz;
142+
text *a2 = PG_GETARG_TEXT_PP(1);
143+
char str[9];
151144

152-
sz = Min(VARSIZE(a2) - VARHDRSZ, 8);
153-
memcpy(str, VARDATA(a2), sz);
154-
str[sz] = '\0';
145+
text_to_cstring_buffer(a2, str, sizeof(str));
155146
PG_RETURN_BOOL(strcmp(a1->password, crypt(str, a1->password)) == 0);
156147
}
157148

@@ -160,12 +151,9 @@ Datum
160151
chkpass_ne(PG_FUNCTION_ARGS)
161152
{
162153
chkpass *a1 = (chkpass *) PG_GETARG_POINTER(0);
163-
text *a2 = (text *) PG_GETARG_TEXT_P(1);
164-
char str[10];
165-
int sz;
154+
text *a2 = PG_GETARG_TEXT_PP(1);
155+
char str[9];
166156

167-
sz = Min(VARSIZE(a2) - VARHDRSZ, 8);
168-
memcpy(str, VARDATA(a2), sz);
169-
str[sz] = '\0';
157+
text_to_cstring_buffer(a2, str, sizeof(str));
170158
PG_RETURN_BOOL(strcmp(a1->password, crypt(str, a1->password)) != 0);
171159
}

0 commit comments

Comments
 (0)