Fix broken and dangerous int array function

Instead of using unsafe libc functions (strcat) and cropping the
integers (givin snprintf a buffer too small for any integer) we use
postgres StringInfo as god intended.
This commit is contained in:
Anders Åstrand
2026-01-08 16:24:59 +01:00
committed by AndersAstrand
parent 67d6e0ed75
commit fef78a8f6f

View File

@@ -3768,30 +3768,27 @@ comp_location(const void *a, const void *b)
} }
#define MAX_STRING_LEN 1024 #define MAX_STRING_LEN 1024
/* Convert array into Text dataum */
/* Convert array of integers into Text datum */
static Datum static Datum
intarray_get_datum(int32 arr[], int len) intarray_get_datum(int32 arr[], int len)
{ {
int j; StringInfoData str;
char str[1024]; Datum datum;
char tmp[10];
str[0] = '\0'; if (len < 1)
return CStringGetTextDatum("");
/* Need to calculate the actual size, and avoid unnessary memory usage */ initStringInfo(&str);
for (j = 0; j < len; j++) appendStringInfo(&str, "%d", arr[0]);
{
if (!str[0])
{
snprintf(tmp, 10, "%d", arr[j]);
strcat(str, tmp);
continue;
}
snprintf(tmp, 10, ",%d", arr[j]);
strcat(str, tmp);
}
return CStringGetTextDatum(str);
for (int i = 1; i < len; i++)
appendStringInfo(&str, ",%d", arr[i]);
datum = CStringGetTextDatum(str.data);
pfree(str.data);
return datum;
} }