[Home]

Summary:ASTERISK-04048: ast_copy_string cuts off the last character when passing in a size
Reporter:jshugart (jshugart)Labels:
Date Opened:2005-05-03 15:02:53Date Closed:2008-01-15 15:33:04.000-0600
Priority:MajorRegression?No
Status:Closed/CompleteComponents:Core/General
Versions:Frequency of
Occurrence
Related
Issues:
Environment:Attachments:( 0) set_destination.patch
Description:ast_copy_string(temp, "abc", 3);
will fill temp with "ab" and drop the "c".

I am compiling with gcc 3.2.3.
Comments:By: Diego Ercolani (dercol) 2005-05-03 17:24:51

seem a problem of set_destination routine in chan_sip.c. My patch should resolve.

By: Kevin P. Fleming (kpfleming) 2005-05-03 19:47:36

First: that behavior is exactly what is expected from ast_copy_string(). It always null-terminates the output buffer, unlike strncpy(). If you pass it a string containing three characters (which is actually four bytes long) and an output buffer size of '3', the last character of the string will be replaced with a null byte, so that the output will be null-terminated.

Second: there _was_ a bug in ast_copy_string() when it was used to copy substrings (when the source string had more characters than needed to be copied). That is now corrected.

Third: the semantic differences between strncpy() and ast_copy_string() when used to copy substrings are significant enough that each usage needs to be individually reviewed and corrected (or left alone to use strncpy()). I did not do that adequately when I did the ast_copy_string() conversion in chan_sip.c yesterday, which led to multiple places where strings were not copied properly. I believe those are now corrected.

I'm sorry for the trouble... however, that is the reason why CVS HEAD is for testing, not production :-) Occasionally we will all make mistakes.

By: Digium Subversion (svnbot) 2008-01-15 15:33:04.000-0600

Repository: asterisk
Revision: 5561

U   trunk/channels/chan_sip.c
U   trunk/utils.c

------------------------------------------------------------------------
r5561 | kpfleming | 2008-01-15 15:33:03 -0600 (Tue, 15 Jan 2008) | 2 lines

fix breakage when ast_copy_string is used to copy substrings (bug ASTERISK-4048, but a different fix)

------------------------------------------------------------------------

http://svn.digium.com/view/asterisk?view=rev&revision=5561