Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#536 undefined behavior in time_str (RtsUtils.c)

6.4.1
open
nobody
5
2005-11-17
2005-11-17
Anonymous
No

code looks like this:

char *
time_str(void)
{
static time_t now = 0;
static char nowstr[26];

if (now == 0) {
time(&now);
strcpy(nowstr, ctime(&now));
strcpy(nowstr+16,nowstr+19); /* this is undefined
behavior as buffers overlap, probably
will show undesired effects if compiler utilise
copy optimisations */
nowstr[21] = '\0';
}
return nowstr;
}

corrected should look like this:

char *
time_str(void)
{
static time_t now = 0;
static char nowstr[26];

if (now == 0) {
time(&now);
/* ctime_r(&now,nowstr); this is better if available */
strcpy(nowstr,ctime(&now));
memmove(nowstr+16,nowstr+19,7);
}
return nowstr;
}

--
bmaxa@eunet.yu , Branimir Maksimovic

Discussion