postgresql/contrib/ltree
Heikki Linnakangas 3f32804918 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:29:47 +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:29:47 +03:00
sql Fix int32 overflow in ltree_compare() 2026-06-16 09:29:47 +03:00
.gitignore Support "make check" in contrib 2011-04-25 22:27:11 +03:00
_ltree_gist.c Mark commented out code as unused 2026-01-22 12:44:07 +01:00
_ltree_op.c Use more palloc_object() and palloc_array() in contrib/ 2025-12-05 16:40:26 +09:00
crc32.c Replace pg_mblen() with bounds-checked versions. 2026-02-09 12:44:04 +13: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:23:22 -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--1.2--1.3.sql Add hash support functions and hash opclass for contrib/ltree. 2024-03-21 18:27:49 -04:00
ltree.control Add hash support functions and hash opclass for contrib/ltree. 2024-03-21 18:27:49 -04:00
ltree.h Fix int32 overflow in ltree_compare() 2026-06-16 09:29:47 +03:00
ltree_gist.c Fix int32 overflow in ltree_compare() 2026-06-16 09:29:47 +03:00
ltree_io.c ltree: Fix overflows with lquery parsing 2026-05-11 05:13:46 -07:00
ltree_op.c Fix int32 overflow in ltree_compare() 2026-06-16 09:29:47 +03:00
ltreetest.sql Add hash support functions and hash opclass for contrib/ltree. 2024-03-21 18:27:49 -04:00
ltxtquery_io.c Guard against overflow in "left" fields of query_int and ltxtquery. 2026-05-11 05:13:46 -07:00
ltxtquery_op.c Fix more multibyte issues in ltree. 2026-02-26 12:23:22 -08:00
Makefile Add hash support functions and hash opclass for contrib/ltree. 2024-03-21 18:27:49 -04:00
meson.build Update copyright for 2026 2026-01-01 13:24:10 -05:00