Re: [BUG FIX] Compare returned value by socket() against PGINVALID_SOCKET instead of < 0 - Mailing list pgsql-hackers

From Amit Kapila
Subject Re: [BUG FIX] Compare returned value by socket() against PGINVALID_SOCKET instead of < 0
Date
Msg-id CAA4eK1JtkJ-XR2DVzC70AiGGeDQpdb2+D5y9z+pN+LZhoc--Hg@mail.gmail.com
Whole thread Raw
In response to Re: [BUG FIX] Compare returned value by socket() against PGINVALID_SOCKET instead of < 0  (Bruce Momjian <bruce@momjian.us>)
Responses Re: [BUG FIX] Compare returned value by socket() against PGINVALID_SOCKET instead of < 0  (Bruce Momjian <bruce@momjian.us>)
List pgsql-hackers
On Tue, Apr 8, 2014 at 11:32 PM, Bruce Momjian <bruce@momjian.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.

>> 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)

With Regards,
Amit Kapila.
EnterpriseDB: http://www.enterprisedb.com



pgsql-hackers by date:

Previous
From: Rajeev rastogi
Date:
Subject: Re: Autonomous Transaction (WIP)
Next
From: Pavan Deolasee
Date:
Subject: Re: Autonomous Transaction (WIP)