Does anyone knows that in which case recvfrom a UDP socket returns -1 but errno is 0 ?
Asked
Active
Viewed 332 times
2 Answers
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