Click here to Skip to main content

Clever Code

RSS Feed RSS feed

Showcase your best code, your most elegant solution, or your most hard fought subtle bug you've found and fixed. Please, do not post programming questions in this forum - they will be removed.

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page  Show 
  Refresh
GeneralRe: The magicians big secret PinmemberPIEBALDconsult8:56 20 Nov '09  
GeneralRe: The magicians big secret PinPopularmvpLuc Pattyn9:12 20 Nov '09  
GeneralRe: The magicians big secret PinmemberAndrew Rissing9:21 20 Nov '09  
GeneralRe: The magicians big secret PinmemberYusuf9:59 20 Nov '09  
GeneralRe: The magicians big secret Pinmemberbquick7:53 27 Feb '10  
GeneralRe: The magicians big secret Pinmemberradioman.lt8:17 26 Feb '10  
GeneralDifferences between strncpy and strncpy_s PinmemberIndivara18:43 14 Nov '09  
I got burned by this once. As you know, the strxxx_s functions are the secure versions of strxxx, which check the buffer size to prevent overruns. When porting old code to VS2005 or later, I usually replace all these functions more or less mechanically. However, there strncpy has changed in a way which sometimes breaks code in a puzzling manner.
 
From MSDN documentation
strncpy
char *strncpy(
   char *strDest,
   const char *strSource,
   size_t count 
);
The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string.

strncpy_s
errno_t strncpy_s(
   char *strDest,
   size_t numberOfElements,
   const char *strSource,
   size_t count
);
These functions try to copy the first D characters of strSource to strDest, where D is the lesser of count and the length of strSource. If those D characters will fit within strDest (whose size is given as numberOfElements) and still leave room for a null terminator, then those characters are copied and a terminating null is appended; otherwise, strDest[0] is set to the null character and ...

 
Now under ordinary circumstances, this is fine, but in a few cases, the source string was being copied to the middle of the destination (overwriting part of the existing string). The old specification meant that the remaining string is left intact, but with the new secure version, the string gets truncated.
 
Using memcpy instead has fixed the issue (for now, at least, until I can rewrite the spaghetti that I've inherited... Dead | X| )
GeneralRe: Differences between strncpy and strncpy_s Pinmembermegaadam4:57 25 Nov '09  
GeneralEnumerators PinPopularmvpJohn Simmons / outlaw programmer2:48 14 Nov '09  
GeneralRe: Enumerators PinmemberRichard MacCutchan3:21 14 Nov '09  
GeneralRe: Enumerators PinmvpJohn Simmons / outlaw programmer2:39 15 Nov '09  
GeneralRe: Enumerators PinmemberRichard MacCutchan2:47 15 Nov '09  
JokeRe: Enumerators PinmemberChris Meech4:40 16 Nov '09  
GeneralRe: Enumerators PinmemberRichard MacCutchan5:12 16 Nov '09  
GeneralRe: Enumerators PinmemberAndrew Rissing6:12 16 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult7:36 16 Nov '09  
GeneralRe: Enumerators PinmemberAndrew Rissing9:36 16 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult10:03 16 Nov '09  
GeneralRe: Enumerators Pinmemberlongnights10:12 20 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult13:30 20 Nov '09  
GeneralRe: Enumerators PinmemberGraham Parkings1:45 20 Nov '09  
GeneralRe: Enumerators PinmvpJohn Simmons / outlaw programmer2:37 20 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult15:22 20 Nov '09  
GeneralRe: Enumerators PinmvpJohn Simmons / outlaw programmer1:52 21 Nov '09  
GeneralRe: Enumerators PinmemberPIEBALDconsult3:13 21 Nov '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 14 Feb 2012
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid