-1

Does anyone knows that in which case recvfrom a UDP socket returns -1 but errno is 0 ?

deepsky
  • 845
  • 1
  • 10
  • 24

2 Answers2

1

The case where you call some other function that sets errno to zero after recvfrom returns. This is surprisingly easy to do. To be safe, copy errno to some other variable immediately after recvfrom returns.

Wrong:

i = recvfrom (...);
if (i < 0)
{
     printf ("recvfrom error!\n");
     printf ("errno=%d\n", errno);
}

What happens if the first printf modifies errno?

Right:

i = recvfrom (...);
if (i < 0)
{
     int j = errno;
     printf ("recvfrom error!\n");
     printf ("errno=%d\n", j);
}

The ANSI spec says, "[A] program that uses errno for error checking should ... inspect it before a subsequent library function call."

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Well corrected, I was about to pounce. But surely it would be more to the point to reverse the order of the `printf()` calls, or to combine them? – user207421 Apr 15 '15 at 09:58
1

Never. That should not happen.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176