#49 strncpy doesn't terminate output string

closed
None
5
2006-06-16
2006-06-01
jmunch
No

strncpy copies the first n charactors of the string,
but does not terminate the output string. I think this
is a general problem, but I am using the "51 large"
version.

Discussion

  • jmunch

    jmunch - 2006-06-01

    Proposed revision

     
  • Maarten Brock

    Maarten Brock - 2006-06-01

    Logged In: YES
    user_id=888171

    Allthough that may be your expectation that is not what
    the C99 standard prescribes in "7.21.2.4 The strncpy
    function". Current behaviour is correct.

     
  • Maarten Brock

    Maarten Brock - 2006-06-01
    • assigned_to: nobody --> maartenbrock
    • status: open --> pending
     
  • Jesus Calvino-Fraga

    Logged In: YES
    user_id=603650

    That has been the standard behaviour for a long time. From
    MSDN (July 2001):

    "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..."

    Just after copying, you can null terminate the string
    yourself:

    strncpy( strDest, strSource, count );
    strDest[count]=0;

     
  • SourceForge Robot

    Logged In: YES
    user_id=1312539

    This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).

     
  • SourceForge Robot

    • status: pending --> closed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks