Re: Check for integer overflow in datetime functions

Lists: pgsql-patches
From: Michael Fuhr <mike(at)fuhr(dot)org>
To: pgsql-patches(at)postgresql(dot)org
Subject: Check for integer overflow in datetime functions
Date: 2005-12-01 02:36:20
Message-ID: 20051201023620.GA54456@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Check integer conversion for overflow in datetime functions. Problem
reported by Christopher Kings-Lynne:

http://archives.postgresql.org/pgsql-hackers/2005-11/msg01385.php

In one place (line 60 in the patch) the code sets errno to 0 when
it should still be 0 after similar code a few lines above (otherwise
the function would have returned an error). I wasn't sure what
would be preferred: clearing errno again "just to be sure," a comment
explaining why it isn't necessary, or nothing at all.

This patch should apply cleanly against HEAD and 8.1. If the patch
looks good then I'll submit patches for earlier branches when I get
a chance to build and test those versions.

--
Michael Fuhr

Attachment Content-Type Size
datetime.c.patch text/plain 2.9 KB

From: Neil Conway <neilc(at)samurai(dot)com>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: Check for integer overflow in datetime functions
Date: 2005-12-01 08:12:01
Message-ID: 1133424721.10985.22.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

On Wed, 2005-11-30 at 19:36 -0700, Michael Fuhr wrote:
> Check integer conversion for overflow in datetime functions.

It seems a bit laborious to always manually set errno to zero before
invoking strtol() (both in the places added by the patch, and all the
places that already did that). While it's only a minor notational
improvement, I wonder if it would be worth adding a pg_strtol() that did
the errno assignment so that each call-site wouldn't need to worry about
it?

-Neil


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, pgsql-patches(at)postgresql(dot)org
Subject: Re: Check for integer overflow in datetime functions
Date: 2005-12-01 15:38:45
Message-ID: 11055.1133451525@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Neil Conway <neilc(at)samurai(dot)com> writes:
> It seems a bit laborious to always manually set errno to zero before
> invoking strtol() (both in the places added by the patch, and all the
> places that already did that). While it's only a minor notational
> improvement, I wonder if it would be worth adding a pg_strtol() that did
> the errno assignment so that each call-site wouldn't need to worry about
> it?

Don't see the point really, since the check of errno would still have to
be in the calling code (since pg_strtol wouldn't know what the
appropriate error action is). So the choice is between

errno = 0;
foo = strtol(...);
if (errno == ERANGE)
... something ...

and

foo = pg_strtol(...);
if (errno == ERANGE)
... something ...

I don't think there's less cognitive load in the second case,
particularly not to someone who's familiar with the standard behavior
of strtol() --- you'll be constantly thinking "what if errno is already
ERANGE ... oh, pg_strtol resets it ..."

I'd be in favor of encapsulating the whole sequence including an
ereport(ERROR) into one function, except it seems we'd not use it in
very many places.

BTW, Neil, were you looking at this with intention to commit it?
I'll pick it up if not, but don't want to duplicate effort if you've
already started on it.

regards, tom lane


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: Check for integer overflow in datetime functions
Date: 2005-12-01 16:54:41
Message-ID: 11870.1133456081@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Michael Fuhr <mike(at)fuhr(dot)org> writes:
> Check integer conversion for overflow in datetime functions.

Looks reasonable, will check and apply.

> This patch should apply cleanly against HEAD and 8.1. If the patch
> looks good then I'll submit patches for earlier branches when I get
> a chance to build and test those versions.

Don't worry about that, I'll take care of it. I prefer committing all
the branches at once when doing a multi-branch fix (less clutter in
the CVS logs).

regards, tom lane


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, pgsql-patches(at)postgresql(dot)org
Subject: Re: Check for integer overflow in datetime functions
Date: 2005-12-01 17:12:11
Message-ID: 20051201171211.GC31881@surnet.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Tom Lane wrote:

> Don't worry about that, I'll take care of it. I prefer committing all
> the branches at once when doing a multi-branch fix (less clutter in
> the CVS logs).

How do you do that? I have multiple checked-out trees, I assume you do
the same and just handle the simultaneous-ness by hand?

BTW, has anyone checked Command Prompt's Subversion repository? It's a
mirror of our anonymous CVS (AFAICT). I'm using it for reading diffs
lately, and it's much nicer to look at the whole patch as a single diff
rather than going a single file at a time.

http://projects.commandprompt.com/projects/public/pgsql/browser/trunk/pgsql

It has the additional advantage over our current CVSweb that it's set
with tabs to 4 spaces, so it looks just like our code is supposed to ...

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, pgsql-patches(at)postgresql(dot)org
Subject: Re: Check for integer overflow in datetime functions
Date: 2005-12-01 17:18:09
Message-ID: 12164.1133457489@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> Tom Lane wrote:
>> Don't worry about that, I'll take care of it. I prefer committing all
>> the branches at once when doing a multi-branch fix (less clutter in
>> the CVS logs).

> How do you do that? I have multiple checked-out trees, I assume you do
> the same and just handle the simultaneous-ness by hand?

Well, they're not *exactly* simultaneous of course. I set up the
modified files in each tree and then commit, commit, commit (the -F
option helps if the commit message is long). I use cvs2cl to extract
CVS history, and it will fold together commits in different branches
if they have the same commit message and are within some time delta
of each other ... I think it's 5 minutes or so.

regards, tom lane


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: Check for integer overflow in datetime functions
Date: 2005-12-01 17:59:16
Message-ID: 15156.1133459956@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Michael Fuhr <mike(at)fuhr(dot)org> writes:
> Check integer conversion for overflow in datetime functions.

Applied back to 7.4. The patch would not work in 7.3 because we didn't
have the DTERR_ convention back then, and it seems not worth the effort
to try to deal with the problem that far back.

> In one place (line 60 in the patch) the code sets errno to 0 when
> it should still be 0 after similar code a few lines above (otherwise
> the function would have returned an error). I wasn't sure what
> would be preferred: clearing errno again "just to be sure," a comment
> explaining why it isn't necessary, or nothing at all.

I left it as-is ...

regards, tom lane


From: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Michael Fuhr <mike(at)fuhr(dot)org>, pgsql-patches(at)postgresql(dot)org
Subject: Re: Check for integer overflow in datetime functions
Date: 2005-12-02 01:45:13
Message-ID: 438FA729.4070602@familyhealth.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

> BTW, has anyone checked Command Prompt's Subversion repository? It's a
> mirror of our anonymous CVS (AFAICT). I'm using it for reading diffs
> lately, and it's much nicer to look at the whole patch as a single diff
> rather than going a single file at a time.
>
> http://projects.commandprompt.com/projects/public/pgsql/browser/trunk/pgsql
>
> It has the additional advantage over our current CVSweb that it's set
> with tabs to 4 spaces, so it looks just like our code is supposed to ...

Does anyone else find the PostgreSQL "TRUNK" funny? :D