|
-
January 16th, 2013, 07:00 PM
#6
Re: Windows strcpy overflow question
 Originally Posted by Eri523
Right, however, this is Win32 API. To avoid dependency on Win32, strcpy_s() from the C++ runtime library can be used as an alternative. It's still MS-specific (AFAIK), however.
Yes, it's an MS-specific recommendation that never made it to the new C11 standard. The C standard has had strncpy for years. Use that, if you desire fixed length fields... Just be aware that unlike the non-portable strcpy_s, strncpy might not append a null terminator to the end of your string. If you want that guarantee, end your fixed length field one byte earlier and put the terminator there yourself.
Code:
#include <string.h>
int main(void) {
char fubar[6];
strncpy(fubar, "hello world", sizeof (fubar) - 1);
fubar[sizeof (fubar) - 1] = '\0';
printf("fubar contains: %s\n", fubar);
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|