Skip to content

Commit 8bdb36a

Browse files
committed
Clean up some questionable usages of DatumGet* macros
This tidies up some questionable coding which made use of DatumGetPointer() for Datums being passed into functions where the parameter is expected to be a cstring. We saw no compiler warnings with the old code as the Pointer type used in DatumGetPointer() happens to be a char * rather than a void *. However, that's no excuse and we should be using the correct macro for the job. Here we also make use of OutputFunctionCall() rather than using FunctionCall1() directly to call the type's output function. OutputFunctionCall() is the standard way to do this. It casts the returned value to a cstring for us. In passing get rid of a duplicate call to strlen(). Most compilers will likely optimize away the 2nd call, but there may be some that won't. In any case, this just aligns the code to some other nearby code that already does this. Discussion: https://postgr.es/m/CAApHDvq1D=ehZ8hey8Hz67N+_Zth0GHO5wiVCfv1YcGPMXJq0A@mail.gmail.com
1 parent e453938 commit 8bdb36a

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/backend/access/brin/brin_minmax_multi.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ range_serialize(Ranges *range)
634634
for (i = 0; i < nvalues; i++)
635635
{
636636
/* don't forget to include the null terminator ;-) */
637-
len += strlen(DatumGetPointer(range->values[i])) + 1;
637+
len += strlen(DatumGetCString(range->values[i])) + 1;
638638
}
639639
}
640640
else /* fixed-length types (even by-reference) */
@@ -695,9 +695,9 @@ range_serialize(Ranges *range)
695695
}
696696
else if (typlen == -2) /* cstring */
697697
{
698-
int tmp = strlen(DatumGetPointer(range->values[i])) + 1;
698+
int tmp = strlen(DatumGetCString(range->values[i])) + 1;
699699

700-
memcpy(ptr, DatumGetPointer(range->values[i]), tmp);
700+
memcpy(ptr, DatumGetCString(range->values[i]), tmp);
701701
ptr += tmp;
702702
}
703703

@@ -780,8 +780,10 @@ range_deserialize(int maxvalues, SerializedRanges *serialized)
780780
}
781781
else if (typlen == -2) /* cstring */
782782
{
783-
datalen += MAXALIGN(strlen(DatumGetPointer(ptr)) + 1);
784-
ptr += strlen(DatumGetPointer(ptr)) + 1;
783+
Size slen = strlen(DatumGetCString(ptr)) + 1;
784+
785+
datalen += MAXALIGN(slen);
786+
ptr += slen;
785787
}
786788
}
787789

@@ -830,7 +832,7 @@ range_deserialize(int maxvalues, SerializedRanges *serialized)
830832

831833
memcpy(dataptr, ptr, slen);
832834
dataptr += MAXALIGN(slen);
833-
ptr += (slen);
835+
ptr += slen;
834836
}
835837

836838
/* make sure we haven't overflown the buffer end */
@@ -3032,19 +3034,17 @@ brin_minmax_multi_summary_out(PG_FUNCTION_ARGS)
30323034
idx = 0;
30333035
for (i = 0; i < ranges_deserialized->nranges; i++)
30343036
{
3035-
Datum a,
3036-
b;
3037+
char *a,
3038+
*b;
30373039
text *c;
30383040
StringInfoData str;
30393041

30403042
initStringInfo(&str);
30413043

3042-
a = FunctionCall1(&fmgrinfo, ranges_deserialized->values[idx++]);
3043-
b = FunctionCall1(&fmgrinfo, ranges_deserialized->values[idx++]);
3044+
a = OutputFunctionCall(&fmgrinfo, ranges_deserialized->values[idx++]);
3045+
b = OutputFunctionCall(&fmgrinfo, ranges_deserialized->values[idx++]);
30443046

3045-
appendStringInfo(&str, "%s ... %s",
3046-
DatumGetPointer(a),
3047-
DatumGetPointer(b));
3047+
appendStringInfo(&str, "%s ... %s", a, b);
30483048

30493049
c = cstring_to_text(str.data);
30503050

@@ -3084,7 +3084,7 @@ brin_minmax_multi_summary_out(PG_FUNCTION_ARGS)
30843084

30853085
a = FunctionCall1(&fmgrinfo, ranges_deserialized->values[idx++]);
30863086

3087-
appendStringInfoString(&str, DatumGetPointer(a));
3087+
appendStringInfoString(&str, DatumGetCString(a));
30883088

30893089
b = cstring_to_text(str.data);
30903090

0 commit comments

Comments
 (0)