* Convert array dimensions into number of elements * * This must do overflow checking, since it is used to validate that a user * dimensionality request doesn't overflow what we can handle. * * The multiplication overflow check only works on machines that have int64 * arithmetic, but that is nearly all platforms these days, and doing check * divides for those that don't seems way too expens
| 69 | * divides for those that don't seems way too expensive. |
| 70 | */ |
| 71 | int |
| 72 | ArrayGetNItems(int ndim, const int *dims) |
| 73 | { |
| 74 | int32 ret; |
| 75 | int i; |
| 76 | |
| 77 | if (ndim <= 0) |
| 78 | return 0; |
| 79 | ret = 1; |
| 80 | for (i = 0; i < ndim; i++) |
| 81 | { |
| 82 | int64 prod; |
| 83 | |
| 84 | /* A negative dimension implies that UB-LB overflowed ... */ |
| 85 | if (dims[i] < 0) |
| 86 | ereport(ERROR, |
| 87 | (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 88 | errmsg("array size exceeds the maximum allowed (%d)", |
| 89 | (int) MaxArraySize))); |
| 90 | |
| 91 | prod = (int64) ret * (int64) dims[i]; |
| 92 | |
| 93 | ret = (int32) prod; |
| 94 | if ((int64) ret != prod) |
| 95 | ereport(ERROR, |
| 96 | (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 97 | errmsg("array size exceeds the maximum allowed (%d)", |
| 98 | (int) MaxArraySize))); |
| 99 | } |
| 100 | Assert(ret >= 0); |
| 101 | if ((Size) ret > MaxArraySize) |
| 102 | ereport(ERROR, |
| 103 | (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 104 | errmsg("array size exceeds the maximum allowed (%d)", |
| 105 | (int) MaxArraySize))); |
| 106 | return (int) ret; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Verify sanity of proposed lower-bound values for an array |