This site is no longer active and is available for archival purposes only. Registration and login is disabled.

Bug in ClassEStd::StrCpy?


Bug in ClassEStd::StrCpy?

Postby laughingkiwi » Jun 1, 2010 @ 11:13pm

Hi Edge Dudes

I am currently porting a Windows game to Edge (with the intent of porting to multiple platforms) and have encountered a bug with the ClassEStd::StrCpy method. Given the following test code:
Code: Select all









10 
11 
12 
13 
14 
15 
16 
17 
int main(int argc, char** argv)
{
    char dest[4];
    const char* src = "ABCDEFGHIJKLMNOP";

    strncpy(dest, src, 4);
    cprintf("strncpy: Destination contains: %c%c%c%c", dest[0], dest[1], dest[2], dest[3]);

    ClassEStd::StrCpy(dest, src, 4);
    cprintf("\r\n\r\n");
    cprintf("ClassEStd::StrCpy: Destination contains: %c%c%c%c", dest[0], dest[1], dest[2], dest[3]);

    cprintf("\r\n\r\nPress any key to quit");
    getch();

    return 0;
}
17 lines; 8 keywds; 12 nums; 65 ops; 5 strs; 0 coms    Syntactic Coloring v0.4 - Dan East  


The results of strncpy(dest, src, 4) is an array containing "ABCD". The results of ClassEStd::StrCpy(dest, src, 4) is an array containing "ABC\0". ClassEStd::StrCpy terminates the copied buffer at the requested length with a null character, whereas strncpy does not.

Is this by design, and should I use strncpy across all platforms?
laughingkiwi
pm Member
 
Posts: 7
Joined: May 24, 2010 @ 12:22pm


Re: Bug in ClassEStd::StrCpy?

Postby edge » Jun 8, 2010 @ 5:21pm

Hi,

The bug occurs because the destination array is too small for the 4 characters and the required NULL character at the end. The character array containing "ABCD" needs to be at least 5 bytes big.
EDGELIB: Cross-platform mobile development at your fingertips
http://www.edgelib.com
User avatar
edge
pm Member
 
Posts: 1180
Joined: Aug 22, 2005 @ 3:42pm
Location: The Netherlands


Re: Bug in ClassEStd::StrCpy?

Postby laughingkiwi » Jun 13, 2010 @ 1:39am

So the answer is to use strncpy. I cannot increase the size of the buffer by 1 byte because it is part of a larger data structure. The only viable workaround is to use strncpy or possibly ClassEMemory::MemCpy instead of ClassEStd::StrCpy. I raised this as a possible bug to the different behaviour between StrCpy and strncpy which I thought would behave the same.
laughingkiwi
pm Member
 
Posts: 7
Joined: May 24, 2010 @ 12:22pm


Return to EDGELIB


Sort


Forum Description

Powerful and affordable C++ middleware solution covering true multi-platform 2D, 3D and network features for Apple iPhone, Windows Mobile, Symbian S60, UIQ, Linux and Windows desktop.

Moderator:

edge

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron