On 1/20/22 11:14 AM,
Mut...@dastardlyhq.com wrote:
> On Thu, 20 Jan 2022 11:43:11 -0000 (UTC)
> Juha Nieminen <nos...@thanks.invalid> wrote:
...
>> (This might be the reason for those infamous and hilarious
>> "Error: No error" messages seen sometimes. It's not that there was no
>> error, but that 'errno' (or whatever the program uses) was changed
>> in between the actual error and showing the message.)
>
> AFAIK the standard C library only sets errno if an error occurs.
Oddly enough, that's not the case:
"The value of errno in the initial thread is zero at program startup
(the initial value of errno in other threads is an indeterminate value),
but is never set to zero by any library function. 218) The value of
errno may be set to nonzero by a library function call whether or not
there is an error, provided the use of errno is not documented in the
description of the function in this document." (C standard 7.5p3).
Implicit in that statement is the fact that "provided the use of errno
is ... documented in the description of the function in this document.",
then that function may only use errno as specified by that documentation.
> ... It never
> resets it to zero if there was no error.
As specified above, it never sets it to zero at all; that occurs only at
program startup, and only for the initial thread.
> ... One would assume the C++ standard
> libraries behave the same way so "Error: no error" would only occur if the
> program has mistakenly seen a return value as a system error which would
> set errno when it might actually be a higher level error which doesn't.
The C++ standard prohibits the routines specified in <system_error>,
from changing the value of errno (19.5p2). Otherwise, it never says
anything to either mandate or prohibit changing errno. I would hope that
what the C standard says about the use of errno by the C standard
library can, by extension, be applied to the rest of the C++ standard
library, but there's nothing that says so explicitly.