Skip to content

undefined behavior in time_str (RtsUtils.c)

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
Edited by Simon Marlow
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information