PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Bug in ClassEStd::StrCpy?

laughingkiwi · 01 Jun 2010, 22:13 UTC
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:
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;
}


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?
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.
laughingkiwi · 13 Jun 2010, 00:39 UTC
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.