Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Differences between strncpy and strncpy_s

Differences between strncpy and strncpy_s

Scheduled Pinned Locked Moved Clever Code
csshelpannouncement
2 Posts 2 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indivara
    wrote on last edited by
    #1

    I got burned by this once. As you know, the str_xxx__s functions are the secure versions of str_xxx_, 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... X| )

    M 1 Reply Last reply
    0
    • I Indivara

      I got burned by this once. As you know, the str_xxx__s functions are the secure versions of str_xxx_, 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... X| )

      M Offline
      M Offline
      megaadam
      wrote on last edited by
      #2

      Tricky, but: std::wstring is your friend. (And worth the extra lines, even if surrounded by spaghetti.)

      ........................ Life is too shor

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups