aboutsummaryrefslogtreecommitdiff
path: root/contrib/intarray/_int_tool.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/intarray/_int_tool.c')
-rw-r--r--contrib/intarray/_int_tool.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index ee8fb64a478..d86485dfa5c 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -220,7 +220,16 @@ ArrayType *
new_intArrayType(int num)
{
ArrayType *r;
- int nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int) * num;
+ int nbytes;
+
+ /* if no elements, return a zero-dimensional array */
+ if (num <= 0)
+ {
+ r = construct_empty_array(INT4OID);
+ return r;
+ }
+
+ nbytes = ARR_OVERHEAD_NONULLS(1) + sizeof(int) * num;
r = (ArrayType *) palloc0(nbytes);
@@ -237,11 +246,11 @@ new_intArrayType(int num)
ArrayType *
resize_intArrayType(ArrayType *a, int num)
{
- int nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num;
+ int nbytes;
int i;
/* if no elements, return a zero-dimensional array */
- if (num == 0)
+ if (num <= 0)
{
ARR_NDIM(a) = 0;
return a;
@@ -250,6 +259,8 @@ resize_intArrayType(ArrayType *a, int num)
if (num == ARRNELEMS(a))
return a;
+ nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num;
+
a = (ArrayType *) repalloc(a, nbytes);
SET_VARSIZE(a, nbytes);