BUG in strftime() ?

it seems that strftime() set errno to 3 !? Just try:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>

int main( void )
{
time_t time_of_day;
char buffer[ 80 ];

time_of_day = time( NULL );

printf(“errno = %d\n”,errno);

strftime( buffer, 80, “Today is %A %B %d, %Y”,
localtime( &time_of_day ) );

printf(“errno = %d\n”,errno);

printf( “%s\n”, buffer);

return EXIT_SUCCESS;
}

It sounds like this kind of coding practice:
errno = error1; // set this just in case
if( test_for_error1 )
return -1; // indicate the error
errno = error2; // set this just in case
if( test_for_error2 )
return -1; // indicate the error
return 0; // return no error
// this errno should be ignored

This is perfectly accestable. Don’t bother looking at the errno unless
the return value first tells you there was an error.


Colin Burgess <cburgess@qnx.com> wrote:
CB > Why a bug - errno is undefined if the function succeeds.

CB > For example

CB > int afunc(int fd)
CB > {
CB > if ( isatty(fd) ) {
CB > return do_this(fd);
CB > } else {
CB > return do_that(fd);
CB > }
CB > }

CB > Will potentially set errno (if the isatty fails) but will still return 0

CB > Alain Bonnefoy wrote:

it seems that strftime() set errno to 3 !? Just try:

#include <stdio.h
#include <stdlib.h
#include <time.h
#include <errno.h

int main( void )
{
time_t time_of_day;
char buffer[ 80 ];

time_of_day = time( NULL );

printf(“errno = %d\n”,errno);

strftime( buffer, 80, “Today is %A %B %d, %Y”,
localtime( &time_of_day ) );

printf(“errno = %d\n”,errno);

printf( “%s\n”, buffer);

return EXIT_SUCCESS;
}

CB > –
CB > cburgess@qnx.com


Bill Caroselli – Q-TPS Consulting
1-(708) 308-4956 <== Note: New Number
qtps@earthlink.net

Why a bug - errno is undefined if the function succeeds.

For example

int afunc(int fd)
{
if ( isatty(fd) ) {
return do_this(fd);
} else {
return do_that(fd);
}
}

Will potentially set errno (if the isatty fails) but will still return 0

Alain Bonnefoy wrote:

it seems that strftime() set errno to 3 !? Just try:

#include <stdio.h
#include <stdlib.h
#include <time.h
#include <errno.h

int main( void )
{
time_t time_of_day;
char buffer[ 80 ];

time_of_day = time( NULL );

printf(“errno = %d\n”,errno);

strftime( buffer, 80, “Today is %A %B %d, %Y”,
localtime( &time_of_day ) );

printf(“errno = %d\n”,errno);

printf( “%s\n”, buffer);

return EXIT_SUCCESS;
}


cburgess@qnx.com

“accestable” - adjective.
“Acceptable but detestable”

:v)

Bill Caroselli wrote:

It sounds like this kind of coding practice:
errno = error1; // set this just in case
if( test_for_error1 )
return -1; // indicate the error
errno = error2; // set this just in case
if( test_for_error2 )
return -1; // indicate the error
return 0; // return no error
// this errno should be ignored

This is perfectly accestable. Don’t bother looking at the errno unless
the return value first tells you there was an error.


Colin Burgess <> cburgess@qnx.com> > wrote:
CB > Why a bug - errno is undefined if the function succeeds.

CB > For example

CB > int afunc(int fd)
CB > {
CB > if ( isatty(fd) ) {
CB > return do_this(fd);
CB > } else {
CB > return do_that(fd);
CB > }
CB > }

CB > Will potentially set errno (if the isatty fails) but will still return 0

CB > Alain Bonnefoy wrote:

it seems that strftime() set errno to 3 !? Just try:

#include <stdio.h
#include <stdlib.h
#include <time.h
#include <errno.h

int main( void )
{
time_t time_of_day;
char buffer[ 80 ];

time_of_day = time( NULL );

printf(“errno = %d\n”,errno);

strftime( buffer, 80, “Today is %A %B %d, %Y”,
localtime( &time_of_day ) );

printf(“errno = %d\n”,errno);

printf( “%s\n”, buffer);

return EXIT_SUCCESS;
}




CB > –
CB > > cburgess@qnx.com


cburgess@qnx.com

Alain Bonnefoy <alain.bonnefoy@rieter.com> wrote:

it seems that strftime() set errno to 3 !? Just try:

I remember trying to track down whether or not a function was
allowed/not allowed to modify errno in the case of a successful
return a while back.

I seem to recall that ANSI C, C99, and POSIX all had slightly
differing things to say about it.

It is, generally, good coding practice to assume:

if error return
if function is doc’ed as setting errno
errno is valid
else
errno value is undefined
else
errno value is undefined

Though, for some classifications of functions (might have been
POSIX), errno was not allowed to be changed unless the function
failed.

-David

#include <stdio.h
#include <stdlib.h
#include <time.h
#include <errno.h

int main( void )
{
time_t time_of_day;
char buffer[ 80 ];

time_of_day = time( NULL );

printf(“errno = %d\n”,errno);

strftime( buffer, 80, “Today is %A %B %d, %Y”,
localtime( &time_of_day ) );

printf(“errno = %d\n”,errno);

printf( “%s\n”, buffer);

return EXIT_SUCCESS;
}


Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

I didn’t know. Ok.
Alain
David Gibbs wrote:

Alain Bonnefoy <> alain.bonnefoy@rieter.com> > wrote:

it seems that strftime() set errno to 3 !? Just try:

I remember trying to track down whether or not a function was
allowed/not allowed to modify errno in the case of a successful
return a while back.

I seem to recall that ANSI C, C99, and POSIX all had slightly
differing things to say about it.

It is, generally, good coding practice to assume:

if error return
if function is doc’ed as setting errno
errno is valid
else
errno value is undefined
else
errno value is undefined

Though, for some classifications of functions (might have been
POSIX), errno was not allowed to be changed unless the function
failed.

-David

#include <stdio.h
#include <stdlib.h
#include <time.h
#include <errno.h

int main( void )
{
time_t time_of_day;
char buffer[ 80 ];

time_of_day = time( NULL );

printf(“errno = %dn”,errno);

strftime( buffer, 80, “Today is %A %B %d, %Y”,
localtime( &time_of_day ) );

printf(“errno = %dn”,errno);

printf( “%sn”, buffer);

return EXIT_SUCCESS;
}


Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

David Gibbs <dagibbs@qnx.com> wrote:

DG > I remember trying to track down whether or not a function was
DG > allowed/not allowed to modify errno in the case of a successful
DG > return a while back.

DG > I seem to recall that ANSI C, C99, and POSIX all had slightly
DG > differing things to say about it.

The best thing about standards is that there are so many different ones
to choose from.

“David Gibbs” <dagibbs@qnx.com> wrote in message
news:c80325$9i9$1@inn.qnx.com

I seem to recall that ANSI C, C99, and POSIX all had slightly
differing things to say about it.

In both C99 and POSIX (I don’t know about C89), a function whose description
doesn’t mention setting errno is allowed to set errno to any nonzero value.

In C99, a function that is required to set errno in some conditions is not
allowed to touch errno in any other circumstances. In POSIX, when a
function
succeeds, it’s allowed to modify errno.

C99 (7.5#3):

The value of errno is zero at program startup, but is never set to zero by
any library function. 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 International
Standard.

POSIX (http://www.opengroup.org/onlinepubs/009695399/functions/errno.html):

The value of errno shall be defined only after a call to a function for
which it is explicitly stated to be set and until it is changed by the
next
function call or if the application assigns it a value. The value of
errno
should only be examined when it is indicated to be valid by a function’s
return value. Applications shall obtain the definition of errno by the
inclusion of <errno.h>. No function in this volume of IEEE Std
1003.1-2001
shall set errno to 0. The setting of errno after a successful call to a
function is unspecified unless the description of that function specifies
that errno shall not be modified.