Strange bug with Ctime

Posted by Dace K on Sun 12 Mar 2006 07:24 PM — 6 posts, 24,270 views.

Canada #0
For some reason, ctime won't show any time but the character's logon time in score, but updates just fine on the do_time command.


void do_time( CHAR_DATA *ch, char *argument )
{
    extern char str_boot_time[];
    extern char reboot_time[];
    char *suf;
    int day;

    day     = time_info.day + 1;

	 if ( day > 4 && day <  20 ) suf = "th";
    else if ( day % 10 ==  1       ) suf = "st";
    else if ( day % 10 ==  2       ) suf = "nd";
    else if ( day % 10 ==  3       ) suf = "rd";
    else                             suf = "th";

    set_char_color( AT_YELLOW, ch );
    ch_printf( ch,
	"It is %d o'clock %s, Day of %s, %d%s the Month of %s.\n\r"
        "The mud started up at:    %s\r"
        "The system time (E.S.T.): %s\r"
        "Next Reboot is set for:   %s\r",

	(time_info.hour % 12 == 0) ? 12 : time_info.hour % 12,
	time_info.hour >= 12 ? "pm" : "am",
	day_name[day % 7],
	day, suf,
	month_name[time_info.month],
	str_boot_time,
	(char *) ctime( &current_time ),
	reboot_time
	);

    return;
}

The mud started up at: Sun Mar 12 16:19:47 2006
The system time (E.S.T.): Sun Mar 12 16:19:57 2006
Next Reboot is set for: Tue Mar 14 06:00:00 2006

void do_score(CHAR_DATA * ch, char *argument)
{
    char            buf[MAX_STRING_LENGTH];
    /*AFFECT_DATA    *paf;*/

    if (IS_NPC(ch))
    {
	do_oldscore(ch, argument);
	return;
    }
    set_pager_color(AT_SCORE, ch);

    pager_printf(ch, "\n\r&G%s&c%s&g.\n\r", ch->name, ch->pcdata->title);
    if ( get_trust( ch ) != ch->level )
	pager_printf( ch, "You are trusted at level %d.\n\r", get_trust( ch ) );

    send_to_pager("&W----------------------------------------------------------------------------\n\r", ch);

    pager_printf(ch, "&GLogin&g: &w%s&GCurrent Time&g: &w%s\r",
		ctime(&(ch->logon)), ctime( &current_time ));


    pager_printf(ch, "&GPlayed&g: &w%d hours      &GSaved&g:  &w%s\r",
	(get_age(ch) - 17) * 2), (ch->save_time ? ctime(&(ch->save_time)) : "no save this session\n");


Login: Sun Mar 12 16:19:47 2006
Current Time: Sun Mar 12 16:19:47 2006
Played: 780 hours Saved: Sun Mar 12 16:19:47 2006

Any thoughts?
Amended on Sun 12 Mar 2006 07:25 PM by Dace K
Canada #1
After some testing, I've found that ctime will only return the first value it's used for in a function, regardless of what it's being called for. (e.g. swapping the values for logon time and current time will result in current time being used for the rest of the values in score).
USA #2
That's not quite the problem. If you look at how ctime is implemented, it uses a static buffer and returns a pointer to that.

So, if you do:

printf("%s %s" ctime(...), ctime(...));

it will evaluate the first ctime, return a pointer to the static buffer, then it will evaluate the second ctime, and return a pointer to the same static buffer. In the end, you have two pointers to the same buffer.

As a technical note, I'm not sure if the function call arguments are evaluated first-to-last or last-to-first. In your case it seems to be last-to-first, because the first value is the one retained. I'm not sure if the standard defines this one way or the other, or if it's an implementation detail.

In any case, the solution is:
char buf[MAX_INPUT_LENGTH];
strcpy(buf, ctime(...));
printf("%s %s", buf, ctime(...));

That way, you're only calling ctime once in the expression, after saving its first result.
Canada #3
Thanks, worked a charm. I couldn't find ctime in the code at all :/

Simply breaking up the ctime calls into seperate lines also worked :)
Amended on Mon 13 Mar 2006 05:50 PM by Dace K
USA #4
Sorry -- it's actually not a SMAUG function, it's a C-library function.

From the 'man ctime' page:
Quote:
NOTES
The four functions asctime(), ctime(), gmtime() and localtime() return a pointer to static data and
hence are not thread-safe. Thread-safe versions asctime_r(), ctime_r(), gmtime_r() and local-
time_r() are specified by SUSv2, and available since libc 5.2.5.


ctime_r is the same as ctime except that it takes a second parameter, which is the buffer in which data is stored. It then returns a pointer to that buffer.

So you could do:
char buf1[MAX_INPUT_LENGTH];
char buf2[MAX_INPUT_LENGTH];
printf("%s %s", ctime_r(..., buf1), ctime_r(..., buf2));


Don't you just love little tricks like this. :-) I ran into this some time ago with a function in SMAUG that converted by using a static buffer, and returning pointers to that.

All this is solved so easily in C++ by simply returning a std::string. The problem in C is that you want to return a non-allocated string, for convenience, so that the caller doesn't have to keep freeing these strings. But as soon as you do that, you only have this one static buffer lying around. And then you get confusing cases like this one you ran into.

In C++ you just return a std::string which manages its own memory and as soon as it leaves the scope, everything is happy and the memory is freed automatically.
USA #5
Hey, that's a pretty sneaky use of the thread-safe functions. I'm not sure I would have thought to even try that myself :)

I think we've all run into the "don't use 2 static buffers in one call" problem once in our lives. Damn confusing when you don't realize why it's happening.