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

Commit b5d1608

Browse files
committed
Fix varlena.c routines to allow 1-byte-header text values. This is now
demonstrably necessary for text_substring() since regexp_split functions may pass it such a value; and we might as well convert the whole file at once. Per buildfarm results (though I wonder why most machines aren't showing a failure).
1 parent 7583f9a commit b5d1608

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

src/backend/utils/adt/varlena.c

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.157 2007/07/19 20:34:20 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.158 2007/09/22 00:36:38 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -689,15 +689,16 @@ text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
689689
slice = (text *) DatumGetPointer(str);
690690

691691
/* see if we got back an empty string */
692-
if ((VARSIZE(slice) - VARHDRSZ) == 0)
692+
if (VARSIZE_ANY_EXHDR(slice) == 0)
693693
{
694694
if (slice != (text *) DatumGetPointer(str))
695695
pfree(slice);
696696
return PG_STR_GET_TEXT("");
697697
}
698698

699699
/* Now we can get the actual length of the slice in MB characters */
700-
slice_strlen = pg_mbstrlen_with_len(VARDATA(slice), VARSIZE(slice) - VARHDRSZ);
700+
slice_strlen = pg_mbstrlen_with_len(VARDATA_ANY(slice),
701+
VARSIZE_ANY_EXHDR(slice));
701702

702703
/*
703704
* Check that the start position wasn't > slice_strlen. If so, SQL99
@@ -722,7 +723,7 @@ text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
722723
/*
723724
* Find the start position in the slice; remember S1 is not zero based
724725
*/
725-
p = VARDATA(slice);
726+
p = VARDATA_ANY(slice);
726727
for (i = 0; i < S1 - 1; i++)
727728
p += pg_mblen(p);
728729

@@ -762,8 +763,8 @@ text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
762763
Datum
763764
textpos(PG_FUNCTION_ARGS)
764765
{
765-
text *str = PG_GETARG_TEXT_P(0);
766-
text *search_str = PG_GETARG_TEXT_P(1);
766+
text *str = PG_GETARG_TEXT_PP(0);
767+
text *search_str = PG_GETARG_TEXT_PP(1);
767768

768769
PG_RETURN_INT32((int32) text_position(str, search_str));
769770
}
@@ -808,15 +809,15 @@ text_position(text *t1, text *t2)
808809
static void
809810
text_position_setup(text *t1, text *t2, TextPositionState *state)
810811
{
811-
int len1 = VARSIZE(t1) - VARHDRSZ;
812-
int len2 = VARSIZE(t2) - VARHDRSZ;
812+
int len1 = VARSIZE_ANY_EXHDR(t1);
813+
int len2 = VARSIZE_ANY_EXHDR(t2);
813814

814815
if (pg_database_encoding_max_length() == 1)
815816
{
816817
/* simple case - single byte encoding */
817818
state->use_wchar = false;
818-
state->str1 = VARDATA(t1);
819-
state->str2 = VARDATA(t2);
819+
state->str1 = VARDATA_ANY(t1);
820+
state->str2 = VARDATA_ANY(t2);
820821
state->len1 = len1;
821822
state->len2 = len2;
822823
}
@@ -827,9 +828,9 @@ text_position_setup(text *t1, text *t2, TextPositionState *state)
827828
*p2;
828829

829830
p1 = (pg_wchar *) palloc((len1 + 1) * sizeof(pg_wchar));
830-
len1 = pg_mb2wchar_with_len(VARDATA(t1), p1, len1);
831+
len1 = pg_mb2wchar_with_len(VARDATA_ANY(t1), p1, len1);
831832
p2 = (pg_wchar *) palloc((len2 + 1) * sizeof(pg_wchar));
832-
len2 = pg_mb2wchar_with_len(VARDATA(t2), p2, len2);
833+
len2 = pg_mb2wchar_with_len(VARDATA_ANY(t2), p2, len2);
833834

834835
state->use_wchar = true;
835836
state->wstr1 = p1;
@@ -2094,7 +2095,7 @@ byteacmp(PG_FUNCTION_ARGS)
20942095
static void
20952096
appendStringInfoText(StringInfo str, const text *t)
20962097
{
2097-
appendBinaryStringInfo(str, VARDATA(t), VARSIZE(t) - VARHDRSZ);
2098+
appendBinaryStringInfo(str, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
20982099
}
20992100

21002101
/*
@@ -2108,9 +2109,9 @@ appendStringInfoText(StringInfo str, const text *t)
21082109
Datum
21092110
replace_text(PG_FUNCTION_ARGS)
21102111
{
2111-
text *src_text = PG_GETARG_TEXT_P(0);
2112-
text *from_sub_text = PG_GETARG_TEXT_P(1);
2113-
text *to_sub_text = PG_GETARG_TEXT_P(2);
2112+
text *src_text = PG_GETARG_TEXT_PP(0);
2113+
text *from_sub_text = PG_GETARG_TEXT_PP(1);
2114+
text *to_sub_text = PG_GETARG_TEXT_PP(2);
21142115
int src_text_len;
21152116
int from_sub_text_len;
21162117
TextPositionState state;
@@ -2148,7 +2149,7 @@ replace_text(PG_FUNCTION_ARGS)
21482149
}
21492150

21502151
/* start_ptr points to the start_posn'th character of src_text */
2151-
start_ptr = (char *) VARDATA(src_text);
2152+
start_ptr = VARDATA_ANY(src_text);
21522153

21532154
initStringInfo(&str);
21542155

@@ -2172,7 +2173,7 @@ replace_text(PG_FUNCTION_ARGS)
21722173
while (curr_posn > 0);
21732174

21742175
/* copy trailing data */
2175-
chunk_len = ((char *) src_text + VARSIZE(src_text)) - start_ptr;
2176+
chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
21762177
appendBinaryStringInfo(&str, start_ptr, chunk_len);
21772178

21782179
text_position_cleanup(&state);
@@ -2191,8 +2192,8 @@ replace_text(PG_FUNCTION_ARGS)
21912192
static bool
21922193
check_replace_text_has_escape_char(const text *replace_text)
21932194
{
2194-
const char *p = VARDATA(replace_text);
2195-
const char *p_end = p + (VARSIZE(replace_text) - VARHDRSZ);
2195+
const char *p = VARDATA_ANY(replace_text);
2196+
const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
21962197

21972198
if (pg_database_encoding_max_length() == 1)
21982199
{
@@ -2226,8 +2227,8 @@ appendStringInfoRegexpSubstr(StringInfo str, text *replace_text,
22262227
regmatch_t *pmatch,
22272228
char *start_ptr, int data_pos)
22282229
{
2229-
const char *p = VARDATA(replace_text);
2230-
const char *p_end = p + (VARSIZE(replace_text) - VARHDRSZ);
2230+
const char *p = VARDATA_ANY(replace_text);
2231+
const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
22312232
int eml = pg_database_encoding_max_length();
22322233

22332234
for (;;)
@@ -2332,7 +2333,7 @@ replace_text_regexp(text *src_text, void *regexp,
23322333
{
23332334
text *ret_text;
23342335
regex_t *re = (regex_t *) regexp;
2335-
int src_text_len = VARSIZE(src_text) - VARHDRSZ;
2336+
int src_text_len = VARSIZE_ANY_EXHDR(src_text);
23362337
StringInfoData buf;
23372338
regmatch_t pmatch[REGEXP_REPLACE_BACKREF_CNT];
23382339
pg_wchar *data;
@@ -2346,13 +2347,13 @@ replace_text_regexp(text *src_text, void *regexp,
23462347

23472348
/* Convert data string to wide characters. */
23482349
data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
2349-
data_len = pg_mb2wchar_with_len(VARDATA(src_text), data, src_text_len);
2350+
data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
23502351

23512352
/* Check whether replace_text has escape char. */
23522353
have_escape = check_replace_text_has_escape_char(replace_text);
23532354

23542355
/* start_ptr points to the data_pos'th character of src_text */
2355-
start_ptr = (char *) VARDATA(src_text);
2356+
start_ptr = (char *) VARDATA_ANY(src_text);
23562357
data_pos = 0;
23572358

23582359
search_start = 0;
@@ -2439,7 +2440,7 @@ replace_text_regexp(text *src_text, void *regexp,
24392440
{
24402441
int chunk_len;
24412442

2442-
chunk_len = ((char *) src_text + VARSIZE(src_text)) - start_ptr;
2443+
chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
24432444
appendBinaryStringInfo(&buf, start_ptr, chunk_len);
24442445
}
24452446

@@ -2459,8 +2460,8 @@ replace_text_regexp(text *src_text, void *regexp,
24592460
Datum
24602461
split_text(PG_FUNCTION_ARGS)
24612462
{
2462-
text *inputstring = PG_GETARG_TEXT_P(0);
2463-
text *fldsep = PG_GETARG_TEXT_P(1);
2463+
text *inputstring = PG_GETARG_TEXT_PP(0);
2464+
text *fldsep = PG_GETARG_TEXT_PP(1);
24642465
int fldnum = PG_GETARG_INT32(2);
24652466
int inputstring_len;
24662467
int fldsep_len;
@@ -2559,8 +2560,8 @@ split_text(PG_FUNCTION_ARGS)
25592560
Datum
25602561
text_to_array(PG_FUNCTION_ARGS)
25612562
{
2562-
text *inputstring = PG_GETARG_TEXT_P(0);
2563-
text *fldsep = PG_GETARG_TEXT_P(1);
2563+
text *inputstring = PG_GETARG_TEXT_PP(0);
2564+
text *fldsep = PG_GETARG_TEXT_PP(1);
25642565
int inputstring_len;
25652566
int fldsep_len;
25662567
TextPositionState state;
@@ -2601,7 +2602,7 @@ text_to_array(PG_FUNCTION_ARGS)
26012602

26022603
start_posn = 1;
26032604
/* start_ptr points to the start_posn'th character of inputstring */
2604-
start_ptr = (char *) VARDATA(inputstring);
2605+
start_ptr = VARDATA_ANY(inputstring);
26052606

26062607
for (fldnum = 1;; fldnum++) /* field number is 1 based */
26072608
{
@@ -2612,7 +2613,7 @@ text_to_array(PG_FUNCTION_ARGS)
26122613
if (end_posn == 0)
26132614
{
26142615
/* fetch last field */
2615-
chunk_len = ((char *) inputstring + VARSIZE(inputstring)) - start_ptr;
2616+
chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
26162617
}
26172618
else
26182619
{

0 commit comments

Comments
 (0)