postgres/contrib/intarray
Andrew Gierth 2f30b311df Avoid crashes in contrib/intarray gist__int_ops (bug #15518)
1. Integer overflow in internal_size could result in memory corruption
in decompression since a zero-length array would be allocated and then
written to. This leads to crashes or corruption when traversing an
index which has been populated with sufficiently sparse values. Fix by
using int64 for computations and checking for overflow.

2. Integer overflow in g_int_compress could cause pessimal merge
choices, resulting in unnecessarily large ranges (which would in turn
trigger issue 1 above). Fix by using int64 again.

3. Even without overflow, array sizes could become large enough to
cause unexplained memory allocation errors. Fix by capping the sizes
to a safe limit and report actual errors pointing at gist__intbig_ops
as needed.

4. Large inputs to the compression function always consist of large
runs of consecutive integers, and the compression loop was processing
these one at a time in an O(N^2) manner with a lot of overhead. The
expected runtime of this function could easily exceed 6 months for a
single call as a result. Fix by performing a linear-time first pass,
which reduces the worst case to something on the order of seconds.

Backpatch all the way, since this has been wrong forever.

Per bug #15518 from report from irc user "dymk", analysis and patch by
me.

Discussion: https://postgr.es/m/15518-799e426c3b4f8358@postgresql.org
2018-11-24 08:40:02 +00:00
..
bench Fix -e option in contrib/intarray/bench/bench.pl. 2016-08-17 15:51:10 -04:00
data 1. Fixed error with empty array ( '{}' ), 2001-08-04 19:35:32 +00:00
expected Fix longstanding error in contrib/intarray's int[] & int[] operator. 2012-02-16 20:00:11 -05:00
sql Fix longstanding error in contrib/intarray's int[] & int[] operator. 2012-02-16 20:00:11 -05:00
_int_bool.c Prevent stack overflow in query-type functions. 2015-10-05 10:06:34 -04:00
_int_gin.c Create function prototype as part of PG_FUNCTION_INFO_V1 macro 2014-04-18 00:03:19 -04:00
_int_gist.c Avoid crashes in contrib/intarray gist__int_ops (bug #15518) 2018-11-24 08:40:02 +00:00
_int_op.c Create function prototype as part of PG_FUNCTION_INFO_V1 macro 2014-04-18 00:03:19 -04:00
_int_tool.c Avoid crashes in contrib/intarray gist__int_ops (bug #15518) 2018-11-24 08:40:02 +00:00
_int.h Predict integer overflow to avoid buffer overruns. 2014-02-17 09:33:31 -05:00
_intbig_gist.c Create function prototype as part of PG_FUNCTION_INFO_V1 macro 2014-04-18 00:03:19 -04:00
.gitignore Support "make check" in contrib 2011-04-25 22:27:11 +03:00
intarray--1.0.sql Throw a useful error message if an extension script file is fed to psql. 2011-10-12 15:45:03 -04:00
intarray--unpackaged--1.0.sql Fix typos in some error messages thrown by extension scripts when fed to psql. 2014-08-25 18:30:28 +02:00
intarray.control Convert contrib modules to use the extension facility. 2011-02-13 22:54:49 -05:00
Makefile Convert contrib modules to use the extension facility. 2011-02-13 22:54:49 -05:00