postgresql/contrib/ltree
Heikki Linnakangas f528a5606a Fix int32 overflow in ltree_compare()
The expression (len_diff * 10 * (an + 1)) used as the return value of
ltree_compare() is computed at int32 width.  With LTREE_MAX_LEVELS =
65535, the product can exceed INT32_MAX once an ltree has more than
~14,653 levels, which causes the result to wrap and invert its sign.
That corrupts btree ordering as well as the "magnitude" consumed by
ltree_penalty() for GiST page splits.

To fix, split ltree_compare() into two functions.  The new
ltree_compare_distance() function returns a float, which won't
overflow.  It's used by the ltree_penalty() caller.  All the other
callers only care about the sign of the return value, i.e. which of
the arguments is greater, so change ltree_compare() to not multiply
the result with (10 * (an + 1)), which avoids the overflow for those
callers.

Existing btree or GiST indexes on ltree columns containing values with
more than ~14,653 levels may be corrupt and should be REINDEXed.

Add a regression test based on the reporter's PoC.

Author: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Reported-by: 王跃林 <violin0613@tju.edu.cn>
Discussion: https://www.postgresql.org/message-id/AI6AnABgKW93Qbx1jVzi84r9.8.1781322625756.Hmail.3020001251%40tju.edu.cn
Backpatch-through: 14
2026-06-16 09:31:23 +03:00
..
data Add ltree data type to contrib, from Teodor Sigaev and Oleg Bartunov. 2002-07-30 16:40:34 +00:00
expected Fix int32 overflow in ltree_compare() 2026-06-16 09:31:23 +03:00
sql Fix int32 overflow in ltree_compare() 2026-06-16 09:31:23 +03:00
.gitignore Support "make check" in contrib 2011-04-25 22:27:11 +03:00
_ltree_gist.c Initial pgindent and pgperltidy run for v13. 2020-05-14 13:06:50 -04:00
_ltree_op.c Make DatumGetFoo/PG_GETARG_FOO/PG_RETURN_FOO macro names more consistent. 2017-09-18 15:21:23 -04:00
crc32.c Add support for binary I/O of ltree, lquery, and ltxtquery types. 2020-04-01 17:31:29 -04:00
crc32.h Add support for binary I/O of ltree, lquery, and ltxtquery types. 2020-04-01 17:31:29 -04:00
lquery_op.c Fix more multibyte issues in ltree. 2026-02-26 12:26:32 -08:00
ltree--1.0--1.1.sql Make contrib modules' installation scripts more secure. 2020-08-10 10:44:42 -04:00
ltree--1.1--1.2.sql Add support for binary I/O of ltree, lquery, and ltxtquery types. 2020-04-01 17:31:29 -04:00
ltree--1.1.sql Update extensions with GIN/GIST support for parallel query. 2016-06-14 13:34:37 -04:00
ltree.control Implement operator class parameters 2020-03-30 19:17:23 +03:00
ltree.h Fix int32 overflow in ltree_compare() 2026-06-16 09:31:23 +03:00
ltree_gist.c Fix int32 overflow in ltree_compare() 2026-06-16 09:31:23 +03:00
ltree_io.c ltree: Fix overflows with lquery parsing 2026-05-11 05:13:51 -07:00
ltree_op.c Fix int32 overflow in ltree_compare() 2026-06-16 09:31:23 +03:00
ltreetest.sql Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
ltxtquery_io.c Guard against overflow in "left" fields of query_int and ltxtquery. 2026-05-11 05:13:51 -07:00
ltxtquery_op.c Fix more multibyte issues in ltree. 2026-02-26 12:26:32 -08:00
Makefile Implement operator class parameters 2020-03-30 19:17:23 +03:00