Re: [BUG FIX] Compare returned value by socket() against PGINVALID_SOCKET instead of < 0

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Cc: Joel Jacobson <joel(at)trustly(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [BUG FIX] Compare returned value by socket() against PGINVALID_SOCKET instead of < 0
Date: 2014-04-10 11:51:43
Message-ID: 20140410115143.GA3562@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Apr 10, 2014 at 11:05:49AM +0530, Amit Kapila wrote:
> On Tue, Apr 8, 2014 at 11:32 PM, Bruce Momjian <bruce(at)momjian(dot)us> wrote:
> > On Sun, Apr 6, 2014 at 11:45:59AM +0530, Amit Kapila wrote:
> > In fact, this C program compiled by gcc on Debian issues no compiler
> > warnings and returns 'hello', showing that -1 and ~0 compare as equal:
> >
> > int
> > main(int argc, char **argv)
> > {
> > int i;
> > unsigned int j;
> >
> > i = -1;
> > j = ~0;
> >
> > if (i == j)
> > printf("hello\n");
> >
> > return 0;
> > }
>
> I have add below code to check it's usage as per PG:
>
> if (j < 0)
> printf("hello-1\n");
>
> It doesn't print hello-1, which means that all the check's in code
> for <sock_desc> < 0 can have problem.

Ah, yes, good point. This is going to require backpatching then.

> >> 1.
> >> int
> >> pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
> >> sock = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
> >> if (sock == SOCKET_ERROR)
> >
> > Well, the actual problem here is that WSASocket() returns INVALID_SOCKET
> > per the documentation, not SOCKET_ERROR. I did not use PGINVALID_SOCKET
> > here because this is Windows-specific code, defining 'sock' as SOCKET.
> > We could have sock defined as pgsocket, but because this is Windows code
> > already, it doesn't seem wise to mix portability code in there.
>
> I think it's better to use check like below, just for matter of
> consistency with other place
> if (sock == INVALID_SOCKET)

Agreed. That is how I have coded the patch.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ Everyone has their own god. +

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Olivier Lalonde 2014-04-10 13:13:47 Adding unsigned 256 bit integers
Previous Message Gregory Smith 2014-04-10 11:38:22 Re: WIP patch (v2) for updatable security barrier views