Bug in the kernel

Feedback? Questions? Comments? All discussions on the articles and tutorials hosted or developed by us go in here.

Moderator: Moderators

Bug in the kernel

Postby Andyhhp » Thu Mar 27, 2008 11:54 pm

Hi,

in string.cpp of the 'lib' part of the kernel, you have the following function is defined
Code: Select all
//! copies string s2 to s1
char *strcpy(char *s1, const char *s2)
{
    char *s1_p = s1;
    while (*s1++ = *s2++);
    return s1_p;
}


Surely you need either a length parameter or a test for a null terminator otherwise you will write your way to the end of memory.

My suggesting would be:
Code: Select all
//! copies string s2 to s1
char *strcpy(char *s1, const char *s2)
{
    char *s1_p = s1;
    while (*s1)
        *s1++ = *s2++;
    return s1_p;
}


Andrew
Image
Andyhhp
Moderator
 
Posts: 387
Joined: Tue Oct 23, 2007 10:05 am
Location: 127.0.0.1

Postby Mike » Fri Mar 28, 2008 5:36 am

Hey,

The routine within the series does work (It has been tested). Its basically the same thing your routine does, but written in a more condensed form. (We test for the null terminator inside of the while loop.)

It can be written cleaner, though. Thanks for letting us know!
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com
User avatar
Mike
Site Admin
 
Posts: 463
Joined: Sat Oct 20, 2007 7:58 pm


Return to Article Feedback

Who is online

Users browsing this forum: Exabot [Bot] and 1 guest

cron