pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix integer-overflow problems in interval comparison.
7+ messages / 3 participants
[nested] [flat]

* pgsql: Fix integer-overflow problems in interval comparison.
@ 2017-04-06 03:51 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 7+ messages in thread

From: Tom Lane @ 2017-04-06 03:51 UTC (permalink / raw)
  To: pgsql-committers

Fix integer-overflow problems in interval comparison.

When using integer timestamps, the interval-comparison functions tried
to compute the overall magnitude of an interval as an int64 number of
microseconds.  As reported by Frazer McLean, this overflows for intervals
exceeding about 296000 years, which is bad since we nominally allow
intervals many times larger than that.  That results in wrong comparison
results, and possibly in corrupted btree indexes for columns containing
such large interval values.

To fix, compute the magnitude as int128 instead.  Although some compilers
have native support for int128 calculations, many don't, so create our
own support functions that can do 128-bit addition and multiplication
if the compiler support isn't there.  These support functions are designed
with an eye to allowing the int128 code paths in numeric.c to be rewritten
for use on all platforms, although this patch doesn't do that, or even
provide all the int128 primitives that will be needed for it.

Back-patch as far as 9.4.  Earlier releases did not guard against overflow
of interval values at all (commit 146604ec4 fixed that), so it seems not
very exciting to worry about overly-large intervals for them.

Before 9.6, we did not assume that unreferenced "static inline" functions
would not draw compiler warnings, so omit functions not directly referenced
by timestamp.c, the only present consumer of int128.h.  (We could have
omitted these functions in HEAD too, but since they were written and
debugged on the way to the present patch, and they look likely to be needed
by numeric.c, let's keep them in HEAD.)  I did not bother to try to prevent
such warnings in a --disable-integer-datetimes build, though.

Before 9.5, configure will never define HAVE_INT128, so the part of
int128.h that exploits a native int128 implementation is dead code in the
9.4 branch.  I didn't bother to remove it, thinking that keeping the file
looking similar in different branches is more useful.

In HEAD only, add a simple test harness for int128.h in src/tools/.

In back branches, this does not change the float-timestamps code path.
That's not subject to the same kind of overflow risk, since it computes
the interval magnitude as float8.  (No doubt, when this code was originally
written, overflow was disregarded for exactly that reason.)  There is a
precision hazard instead :-(, but we'll avert our eyes from that question,
since no complaints have been reported and that code's deprecated anyway.

Kyotaro Horiguchi and Tom Lane

Discussion: https://postgr.es/m/1490104629.422698.918452336.26FA96B7@webmail.messagingengine.com

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/df1a699e5ba3232f373790b2c9485ddf720c4a70

Modified Files
--------------
src/backend/utils/adt/timestamp.c      |  50 ++++--
src/include/common/int128.h            | 276 +++++++++++++++++++++++++++++++++
src/test/regress/expected/interval.out |  64 ++++++++
src/test/regress/sql/interval.sql      |  27 ++++
src/tools/testint128.c                 | 183 ++++++++++++++++++++++
5 files changed, 590 insertions(+), 10 deletions(-)


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Fix integer-overflow problems in interval comparison.
@ 2017-04-06 03:51 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 7+ messages in thread

From: Tom Lane @ 2017-04-06 03:51 UTC (permalink / raw)
  To: pgsql-committers

Fix integer-overflow problems in interval comparison.

When using integer timestamps, the interval-comparison functions tried
to compute the overall magnitude of an interval as an int64 number of
microseconds.  As reported by Frazer McLean, this overflows for intervals
exceeding about 296000 years, which is bad since we nominally allow
intervals many times larger than that.  That results in wrong comparison
results, and possibly in corrupted btree indexes for columns containing
such large interval values.

To fix, compute the magnitude as int128 instead.  Although some compilers
have native support for int128 calculations, many don't, so create our
own support functions that can do 128-bit addition and multiplication
if the compiler support isn't there.  These support functions are designed
with an eye to allowing the int128 code paths in numeric.c to be rewritten
for use on all platforms, although this patch doesn't do that, or even
provide all the int128 primitives that will be needed for it.

Back-patch as far as 9.4.  Earlier releases did not guard against overflow
of interval values at all (commit 146604ec4 fixed that), so it seems not
very exciting to worry about overly-large intervals for them.

Before 9.6, we did not assume that unreferenced "static inline" functions
would not draw compiler warnings, so omit functions not directly referenced
by timestamp.c, the only present consumer of int128.h.  (We could have
omitted these functions in HEAD too, but since they were written and
debugged on the way to the present patch, and they look likely to be needed
by numeric.c, let's keep them in HEAD.)  I did not bother to try to prevent
such warnings in a --disable-integer-datetimes build, though.

Before 9.5, configure will never define HAVE_INT128, so the part of
int128.h that exploits a native int128 implementation is dead code in the
9.4 branch.  I didn't bother to remove it, thinking that keeping the file
looking similar in different branches is more useful.

In HEAD only, add a simple test harness for int128.h in src/tools/.

In back branches, this does not change the float-timestamps code path.
That's not subject to the same kind of overflow risk, since it computes
the interval magnitude as float8.  (No doubt, when this code was originally
written, overflow was disregarded for exactly that reason.)  There is a
precision hazard instead :-(, but we'll avert our eyes from that question,
since no complaints have been reported and that code's deprecated anyway.

Kyotaro Horiguchi and Tom Lane

Discussion: https://postgr.es/m/1490104629.422698.918452336.26FA96B7@webmail.messagingengine.com

Branch
------
REL9_6_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/fd52b88343308fabb13d6355db3c2565cdf91302

Modified Files
--------------
src/backend/utils/adt/timestamp.c      |  65 ++++++--
src/include/common/int128.h            | 276 +++++++++++++++++++++++++++++++++
src/test/regress/expected/interval.out |  64 ++++++++
src/test/regress/sql/interval.sql      |  27 ++++
4 files changed, 421 insertions(+), 11 deletions(-)


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Fix integer-overflow problems in interval comparison.
@ 2017-04-06 03:51 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 7+ messages in thread

From: Tom Lane @ 2017-04-06 03:51 UTC (permalink / raw)
  To: pgsql-committers

Fix integer-overflow problems in interval comparison.

When using integer timestamps, the interval-comparison functions tried
to compute the overall magnitude of an interval as an int64 number of
microseconds.  As reported by Frazer McLean, this overflows for intervals
exceeding about 296000 years, which is bad since we nominally allow
intervals many times larger than that.  That results in wrong comparison
results, and possibly in corrupted btree indexes for columns containing
such large interval values.

To fix, compute the magnitude as int128 instead.  Although some compilers
have native support for int128 calculations, many don't, so create our
own support functions that can do 128-bit addition and multiplication
if the compiler support isn't there.  These support functions are designed
with an eye to allowing the int128 code paths in numeric.c to be rewritten
for use on all platforms, although this patch doesn't do that, or even
provide all the int128 primitives that will be needed for it.

Back-patch as far as 9.4.  Earlier releases did not guard against overflow
of interval values at all (commit 146604ec4 fixed that), so it seems not
very exciting to worry about overly-large intervals for them.

Before 9.6, we did not assume that unreferenced "static inline" functions
would not draw compiler warnings, so omit functions not directly referenced
by timestamp.c, the only present consumer of int128.h.  (We could have
omitted these functions in HEAD too, but since they were written and
debugged on the way to the present patch, and they look likely to be needed
by numeric.c, let's keep them in HEAD.)  I did not bother to try to prevent
such warnings in a --disable-integer-datetimes build, though.

Before 9.5, configure will never define HAVE_INT128, so the part of
int128.h that exploits a native int128 implementation is dead code in the
9.4 branch.  I didn't bother to remove it, thinking that keeping the file
looking similar in different branches is more useful.

In HEAD only, add a simple test harness for int128.h in src/tools/.

In back branches, this does not change the float-timestamps code path.
That's not subject to the same kind of overflow risk, since it computes
the interval magnitude as float8.  (No doubt, when this code was originally
written, overflow was disregarded for exactly that reason.)  There is a
precision hazard instead :-(, but we'll avert our eyes from that question,
since no complaints have been reported and that code's deprecated anyway.

Kyotaro Horiguchi and Tom Lane

Discussion: https://postgr.es/m/1490104629.422698.918452336.26FA96B7@webmail.messagingengine.com

Branch
------
REL9_5_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/d68a2b20ae2c3a55ba934a0d9a669592a212f351

Modified Files
--------------
src/backend/utils/adt/timestamp.c      |  65 ++++++++--
src/include/common/int128.h            | 231 +++++++++++++++++++++++++++++++++
src/test/regress/expected/interval.out |  64 +++++++++
src/test/regress/sql/interval.sql      |  27 ++++
4 files changed, 376 insertions(+), 11 deletions(-)


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Fix integer-overflow problems in interval comparison.
@ 2017-04-06 03:51 Tom Lane <tgl@sss.pgh.pa.us>
  2017-04-17 16:39 ` Re: pgsql: Fix integer-overflow problems in interval comparison. Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2017-04-06 03:51 UTC (permalink / raw)
  To: pgsql-committers

Fix integer-overflow problems in interval comparison.

When using integer timestamps, the interval-comparison functions tried
to compute the overall magnitude of an interval as an int64 number of
microseconds.  As reported by Frazer McLean, this overflows for intervals
exceeding about 296000 years, which is bad since we nominally allow
intervals many times larger than that.  That results in wrong comparison
results, and possibly in corrupted btree indexes for columns containing
such large interval values.

To fix, compute the magnitude as int128 instead.  Although some compilers
have native support for int128 calculations, many don't, so create our
own support functions that can do 128-bit addition and multiplication
if the compiler support isn't there.  These support functions are designed
with an eye to allowing the int128 code paths in numeric.c to be rewritten
for use on all platforms, although this patch doesn't do that, or even
provide all the int128 primitives that will be needed for it.

Back-patch as far as 9.4.  Earlier releases did not guard against overflow
of interval values at all (commit 146604ec4 fixed that), so it seems not
very exciting to worry about overly-large intervals for them.

Before 9.6, we did not assume that unreferenced "static inline" functions
would not draw compiler warnings, so omit functions not directly referenced
by timestamp.c, the only present consumer of int128.h.  (We could have
omitted these functions in HEAD too, but since they were written and
debugged on the way to the present patch, and they look likely to be needed
by numeric.c, let's keep them in HEAD.)  I did not bother to try to prevent
such warnings in a --disable-integer-datetimes build, though.

Before 9.5, configure will never define HAVE_INT128, so the part of
int128.h that exploits a native int128 implementation is dead code in the
9.4 branch.  I didn't bother to remove it, thinking that keeping the file
looking similar in different branches is more useful.

In HEAD only, add a simple test harness for int128.h in src/tools/.

In back branches, this does not change the float-timestamps code path.
That's not subject to the same kind of overflow risk, since it computes
the interval magnitude as float8.  (No doubt, when this code was originally
written, overflow was disregarded for exactly that reason.)  There is a
precision hazard instead :-(, but we'll avert our eyes from that question,
since no complaints have been reported and that code's deprecated anyway.

Kyotaro Horiguchi and Tom Lane

Discussion: https://postgr.es/m/1490104629.422698.918452336.26FA96B7@webmail.messagingengine.com

Branch
------
REL9_4_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/8851bcf8813baa0ea393ef9d2894d15b3f13f957

Modified Files
--------------
src/backend/utils/adt/timestamp.c      |  65 ++++++++--
src/include/common/int128.h            | 231 +++++++++++++++++++++++++++++++++
src/test/regress/expected/interval.out |  64 +++++++++
src/test/regress/sql/interval.sql      |  27 ++++
4 files changed, 376 insertions(+), 11 deletions(-)


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: Fix integer-overflow problems in interval comparison.
  2017-04-06 03:51 pgsql: Fix integer-overflow problems in interval comparison. Tom Lane <tgl@sss.pgh.pa.us>
@ 2017-04-17 16:39 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  2017-04-17 17:04   ` Re: pgsql: Fix integer-overflow problems in interval comparison. Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Peter Eisentraut @ 2017-04-17 16:39 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-committers

On 4/5/17 23:51, Tom Lane wrote:
> Fix integer-overflow problems in interval comparison.

> Branch
> ------
> REL9_4_STABLE
> 
> Details
> -------
> http://git.postgresql.org/pg/commitdiff/8851bcf8813baa0ea393ef9d2894d15b3f13f957
> 
> Modified Files
> --------------
> src/backend/utils/adt/timestamp.c      |  65 ++++++++--
> src/include/common/int128.h            | 231 +++++++++++++++++++++++++++++++++
> src/test/regress/expected/interval.out |  64 +++++++++
> src/test/regress/sql/interval.sql      |  27 ++++
> 4 files changed, 376 insertions(+), 11 deletions(-)

This is failing cpluspluscheck now because C++ does not have
_Static_assert, which is used in int128.h.

I suppose this header is not intended for public consumption, at least
in the back branches, so it would be OK to exclude it from the check.
Perhaps a different solution would be appropriate in master.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: Fix integer-overflow problems in interval comparison.
  2017-04-06 03:51 pgsql: Fix integer-overflow problems in interval comparison. Tom Lane <tgl@sss.pgh.pa.us>
  2017-04-17 16:39 ` Re: pgsql: Fix integer-overflow problems in interval comparison. Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
@ 2017-04-17 17:04   ` Tom Lane <tgl@sss.pgh.pa.us>
  2017-04-17 17:07     ` Re: pgsql: Fix integer-overflow problems in interval comparison. Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2017-04-17 17:04 UTC (permalink / raw)
  To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: pgsql-committers

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
> On 4/5/17 23:51, Tom Lane wrote:
>> Fix integer-overflow problems in interval comparison.

> This is failing cpluspluscheck now because C++ does not have
> _Static_assert, which is used in int128.h.

Hmm.  We could drop that assert, or move it to some .c file, or
wrap it in "#ifndef __cplusplus" ... but really it seems like a
more generic solution would be appropriate.  This won't be the
last time somebody tries to do that, what with our increasing
use of inline functions.

Maybe the definition of StaticAssertStmt should be tweaked based on
__cplusplus?  Or maybe the problem is cpluspluscheck's test methodology,
ie you ought to run configure with CC=c++ before trying to compile the
headers?

			regards, tom lane


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: pgsql: Fix integer-overflow problems in interval comparison.
  2017-04-06 03:51 pgsql: Fix integer-overflow problems in interval comparison. Tom Lane <tgl@sss.pgh.pa.us>
  2017-04-17 16:39 ` Re: pgsql: Fix integer-overflow problems in interval comparison. Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  2017-04-17 17:04   ` Re: pgsql: Fix integer-overflow problems in interval comparison. Tom Lane <tgl@sss.pgh.pa.us>
@ 2017-04-17 17:07     ` Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 7+ messages in thread

From: Andres Freund @ 2017-04-17 17:07 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; pgsql-committers

On 2017-04-17 13:04:31 -0400, Tom Lane wrote:
> Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
> > On 4/5/17 23:51, Tom Lane wrote:
> >> Fix integer-overflow problems in interval comparison.
> 
> > This is failing cpluspluscheck now because C++ does not have
> > _Static_assert, which is used in int128.h.
> 
> Hmm.  We could drop that assert, or move it to some .c file, or
> wrap it in "#ifndef __cplusplus" ... but really it seems like a
> more generic solution would be appropriate.  This won't be the
> last time somebody tries to do that, what with our increasing
> use of inline functions.
> 
> Maybe the definition of StaticAssertStmt should be tweaked based on
> __cplusplus?

That seems reasonable, possibly with a cplusplus version check and then
using static_assert()? Although I'd personally just mention that as a
todo for later.


> Or maybe the problem is cpluspluscheck's test methodology,
> ie you ought to run configure with CC=c++ before trying to compile the
> headers?

That seems unlikely to work well for the moment...

- Andres


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2017-04-17 17:07 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-04-06 03:51 pgsql: Fix integer-overflow problems in interval comparison. Tom Lane <tgl@sss.pgh.pa.us>
2017-04-06 03:51 pgsql: Fix integer-overflow problems in interval comparison. Tom Lane <tgl@sss.pgh.pa.us>
2017-04-06 03:51 pgsql: Fix integer-overflow problems in interval comparison. Tom Lane <tgl@sss.pgh.pa.us>
2017-04-06 03:51 pgsql: Fix integer-overflow problems in interval comparison. Tom Lane <tgl@sss.pgh.pa.us>
2017-04-17 16:39 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-04-17 17:04   ` Tom Lane <tgl@sss.pgh.pa.us>
2017-04-17 17:07     ` Andres Freund <andres@anarazel.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox