Skip to content

Commit 0f5ade7

Browse files
committed
Fix varatt versus Datum type confusions
Macros like VARDATA() and VARSIZE() should be thought of as taking values of type pointer to struct varlena or some other related struct. The way they are implemented, you can pass anything to it and it will cast it right. But this is in principle incorrect. To fix, add the required DatumGetPointer() calls. Or in a couple of cases, remove superfluous PointerGetDatum() calls. It is planned in a subsequent patch to change macros like VARDATA() and VARSIZE() to inline functions, which will enforce stricter typing. This is in preparation for that. Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://www.postgresql.org/message-id/flat/928ea48f-77c6-417b-897c-621ef16685a6%40eisentraut.org
1 parent 2ad6e80 commit 0f5ade7

File tree

23 files changed

+65
-66
lines changed

23 files changed

+65
-66
lines changed

contrib/hstore/hstore_gin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ gin_extract_hstore_query(PG_FUNCTION_ARGS)
127127
/* Nulls in the array are ignored, cf hstoreArrayToPairs */
128128
if (key_nulls[i])
129129
continue;
130-
item = makeitem(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ, KEYFLAG);
130+
item = makeitem(VARDATA(DatumGetPointer(key_datums[i])), VARSIZE(DatumGetPointer(key_datums[i])) - VARHDRSZ, KEYFLAG);
131131
entries[j++] = PointerGetDatum(item);
132132
}
133133

contrib/hstore/hstore_gist.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ ghstore_consistent(PG_FUNCTION_ARGS)
576576

577577
if (key_nulls[i])
578578
continue;
579-
crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
579+
crc = crc32_sz(VARDATA(DatumGetPointer(key_datums[i])), VARSIZE(DatumGetPointer(key_datums[i])) - VARHDRSZ);
580580
if (!(GETBIT(sign, HASHVAL(crc, siglen))))
581581
res = false;
582582
}
@@ -599,7 +599,7 @@ ghstore_consistent(PG_FUNCTION_ARGS)
599599

600600
if (key_nulls[i])
601601
continue;
602-
crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
602+
crc = crc32_sz(VARDATA(DatumGetPointer(key_datums[i])), VARSIZE(DatumGetPointer(key_datums[i])) - VARHDRSZ);
603603
if (GETBIT(sign, HASHVAL(crc, siglen)))
604604
res = true;
605605
}

contrib/hstore/hstore_io.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -684,22 +684,22 @@ hstore_from_arrays(PG_FUNCTION_ARGS)
684684

685685
if (!value_nulls || value_nulls[i])
686686
{
687-
pairs[i].key = VARDATA(key_datums[i]);
687+
pairs[i].key = VARDATA(DatumGetPointer(key_datums[i]));
688688
pairs[i].val = NULL;
689689
pairs[i].keylen =
690-
hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
690+
hstoreCheckKeyLen(VARSIZE(DatumGetPointer(key_datums[i])) - VARHDRSZ);
691691
pairs[i].vallen = 4;
692692
pairs[i].isnull = true;
693693
pairs[i].needfree = false;
694694
}
695695
else
696696
{
697-
pairs[i].key = VARDATA(key_datums[i]);
698-
pairs[i].val = VARDATA(value_datums[i]);
697+
pairs[i].key = VARDATA(DatumGetPointer(key_datums[i]));
698+
pairs[i].val = VARDATA(DatumGetPointer(value_datums[i]));
699699
pairs[i].keylen =
700-
hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
700+
hstoreCheckKeyLen(VARSIZE(DatumGetPointer(key_datums[i])) - VARHDRSZ);
701701
pairs[i].vallen =
702-
hstoreCheckValLen(VARSIZE(value_datums[i]) - VARHDRSZ);
702+
hstoreCheckValLen(VARSIZE(DatumGetPointer(value_datums[i])) - VARHDRSZ);
703703
pairs[i].isnull = false;
704704
pairs[i].needfree = false;
705705
}
@@ -778,22 +778,22 @@ hstore_from_array(PG_FUNCTION_ARGS)
778778

779779
if (in_nulls[i * 2 + 1])
780780
{
781-
pairs[i].key = VARDATA(in_datums[i * 2]);
781+
pairs[i].key = VARDATA(DatumGetPointer(in_datums[i * 2]));
782782
pairs[i].val = NULL;
783783
pairs[i].keylen =
784-
hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
784+
hstoreCheckKeyLen(VARSIZE(DatumGetPointer(in_datums[i * 2])) - VARHDRSZ);
785785
pairs[i].vallen = 4;
786786
pairs[i].isnull = true;
787787
pairs[i].needfree = false;
788788
}
789789
else
790790
{
791-
pairs[i].key = VARDATA(in_datums[i * 2]);
792-
pairs[i].val = VARDATA(in_datums[i * 2 + 1]);
791+
pairs[i].key = VARDATA(DatumGetPointer(in_datums[i * 2]));
792+
pairs[i].val = VARDATA(DatumGetPointer(in_datums[i * 2 + 1]));
793793
pairs[i].keylen =
794-
hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
794+
hstoreCheckKeyLen(VARSIZE(DatumGetPointer(in_datums[i * 2])) - VARHDRSZ);
795795
pairs[i].vallen =
796-
hstoreCheckValLen(VARSIZE(in_datums[i * 2 + 1]) - VARHDRSZ);
796+
hstoreCheckValLen(VARSIZE(DatumGetPointer(in_datums[i * 2 + 1])) - VARHDRSZ);
797797
pairs[i].isnull = false;
798798
pairs[i].needfree = false;
799799
}

contrib/hstore/hstore_op.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ hstoreArrayToPairs(ArrayType *a, int *npairs)
107107
{
108108
if (!key_nulls[i])
109109
{
110-
key_pairs[j].key = VARDATA(key_datums[i]);
111-
key_pairs[j].keylen = VARSIZE(key_datums[i]) - VARHDRSZ;
110+
key_pairs[j].key = VARDATA(DatumGetPointer(key_datums[i]));
111+
key_pairs[j].keylen = VARSIZE(DatumGetPointer(key_datums[i])) - VARHDRSZ;
112112
key_pairs[j].val = NULL;
113113
key_pairs[j].vallen = 0;
114114
key_pairs[j].needfree = 0;

contrib/test_decoding/test_decoding.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ tuple_to_stringinfo(StringInfo s, TupleDesc tupdesc, HeapTuple tuple, bool skip_
581581
/* print data */
582582
if (isnull)
583583
appendStringInfoString(s, "null");
584-
else if (typisvarlena && VARATT_IS_EXTERNAL_ONDISK(origval))
584+
else if (typisvarlena && VARATT_IS_EXTERNAL_ONDISK(DatumGetPointer(origval)))
585585
appendStringInfoString(s, "unchanged-toast-datum");
586586
else if (!typisvarlena)
587587
print_literal(s, typid,

src/backend/access/brin/brin_minmax_multi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ brin_range_serialize(Ranges *range)
624624

625625
for (i = 0; i < nvalues; i++)
626626
{
627-
len += VARSIZE_ANY(range->values[i]);
627+
len += VARSIZE_ANY(DatumGetPointer(range->values[i]));
628628
}
629629
}
630630
else if (typlen == -2) /* cstring */

src/backend/access/common/heaptuple.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ getmissingattr(TupleDesc tupleDesc,
189189
if (att->attlen > 0)
190190
key.len = att->attlen;
191191
else
192-
key.len = VARSIZE_ANY(attrmiss->am_value);
192+
key.len = VARSIZE_ANY(DatumGetPointer(attrmiss->am_value));
193193
key.value = attrmiss->am_value;
194194

195195
entry = hash_search(missing_cache, &key, HASH_ENTER, &found);
@@ -901,9 +901,9 @@ expand_tuple(HeapTuple *targetHeapTuple,
901901
att->attlen,
902902
attrmiss[attnum].am_value);
903903

904-
targetDataLen = att_addlength_pointer(targetDataLen,
905-
att->attlen,
906-
attrmiss[attnum].am_value);
904+
targetDataLen = att_addlength_datum(targetDataLen,
905+
att->attlen,
906+
attrmiss[attnum].am_value);
907907
}
908908
else
909909
{

src/backend/access/common/reloptions.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,8 +1190,8 @@ transformRelOptions(Datum oldOptions, List *defList, const char *namspace,
11901190

11911191
for (i = 0; i < noldoptions; i++)
11921192
{
1193-
char *text_str = VARDATA(oldoptions[i]);
1194-
int text_len = VARSIZE(oldoptions[i]) - VARHDRSZ;
1193+
char *text_str = VARDATA(DatumGetPointer(oldoptions[i]));
1194+
int text_len = VARSIZE(DatumGetPointer(oldoptions[i])) - VARHDRSZ;
11951195

11961196
/* Search for a match in defList */
11971197
foreach(cell, defList)
@@ -1456,8 +1456,8 @@ parseRelOptionsInternal(Datum options, bool validate,
14561456

14571457
for (i = 0; i < noptions; i++)
14581458
{
1459-
char *text_str = VARDATA(optiondatums[i]);
1460-
int text_len = VARSIZE(optiondatums[i]) - VARHDRSZ;
1459+
char *text_str = VARDATA(DatumGetPointer(optiondatums[i]));
1460+
int text_len = VARSIZE(DatumGetPointer(optiondatums[i])) - VARHDRSZ;
14611461
int j;
14621462

14631463
/* Search for a match in reloptions */

src/backend/access/common/toast_internals.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ toast_save_datum(Relation rel, Datum value,
144144
int num_indexes;
145145
int validIndex;
146146

147-
Assert(!VARATT_IS_EXTERNAL(value));
147+
Assert(!VARATT_IS_EXTERNAL(dval));
148148

149149
/*
150150
* Open the toast relation and its indexes. We can use the index to check

src/backend/access/gin/gininsert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,7 @@ _gin_build_tuple(OffsetNumber attrnum, unsigned char category,
22332233
else if (typlen > 0)
22342234
keylen = typlen;
22352235
else if (typlen == -1)
2236-
keylen = VARSIZE_ANY(key);
2236+
keylen = VARSIZE_ANY(DatumGetPointer(key));
22372237
else if (typlen == -2)
22382238
keylen = strlen(DatumGetPointer(key)) + 1;
22392239
else

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