Re: intarray: fix an edge case int32 overflow bug

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: intarray: fix an edge case int32 overflow bug
Date: 2026-01-04 23:42:52
Message-ID: 20CB8220-61E0-45CF-B4B9-7C2497901EEC@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Jan 4, 2026, at 15:35, David Rowley <dgrowleyml(at)gmail(dot)com> wrote:
>
> On Sun, 4 Jan 2026 at 19:28, David Rowley <dgrowleyml(at)gmail(dot)com> wrote:
>> I can take care of the overflow issue. I feel the test is a step too
>> far as it seems unlikely ever to be rebroken, but thanks for the
>> SQL-based test case to demonstrate the issue.
>
> Pushed.
>
> David

I searched over the source tree to see if there is a similar buggy bsearch comparator and didn’t find any. However, I found a set of comparators in common/int.h, so I think it’s better to use them here:

```
diff --git a/contrib/intarray/_int_selfuncs.c b/contrib/intarray/_int_selfuncs.c
index c3e19cdf27f..80faac8ae71 100644
--- a/contrib/intarray/_int_selfuncs.c
+++ b/contrib/intarray/_int_selfuncs.c
@@ -19,6 +19,7 @@
#include "catalog/pg_operator.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "utils/fmgrprotos.h"
#include "utils/lsyscache.h"
@@ -331,10 +332,5 @@ compare_val_int4(const void *a, const void *b)
int32 key = *(int32 *) a;
int32 value = DatumGetInt32(*(const Datum *) b);

- if (key < value)
- return -1;
- else if (key > value)
- return 1;
- else
- return 0;
+ return pg_cmp_s32(key, value);
}
```

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Smith 2026-01-05 00:12:13 Re: DOCS - "\d mytable" also shows any publications that publish mytable
Previous Message Tom Lane 2026-01-04 23:32:14 Re: Typos in the code and README