diff options
Diffstat (limited to 'contrib/intarray/_int_tool.c')
-rw-r--r-- | contrib/intarray/_int_tool.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c index d86485dfa5c..e12d15c21e8 100644 --- a/contrib/intarray/_int_tool.c +++ b/contrib/intarray/_int_tool.c @@ -3,6 +3,8 @@ */ #include "postgres.h" +#include <limits.h> + #include "catalog/pg_type.h" #include "_int.h" @@ -225,6 +227,7 @@ new_intArrayType(int num) /* if no elements, return a zero-dimensional array */ if (num <= 0) { + Assert(num == 0); r = construct_empty_array(INT4OID); return r; } @@ -252,6 +255,7 @@ resize_intArrayType(ArrayType *a, int num) /* if no elements, return a zero-dimensional array */ if (num <= 0) { + Assert(num == 0); ARR_NDIM(a) = 0; return a; } @@ -288,16 +292,18 @@ copy_intArrayType(ArrayType *a) int internal_size(int *a, int len) { - int i, - size = 0; + int i; + int64 size = 0; for (i = 0; i < len; i += 2) { if (!i || a[i] != a[i - 1]) /* do not count repeated range */ - size += a[i + 1] - a[i] + 1; + size += (int64)(a[i + 1]) - (int64)(a[i]) + 1; } - return size; + if (size > (int64)INT_MAX || size < (int64)INT_MIN) + return -1; /* overflow */ + return (int) size; } /* unique-ify elements of r in-place ... r must be sorted already */ |