summaryrefslogtreecommitdiff
path: root/main/strings.c
diff options
context:
space:
mode:
authorMark Michelson <mmichelson@digium.com>2010-02-17 21:22:40 +0000
committerMark Michelson <mmichelson@digium.com>2010-02-17 21:22:40 +0000
commit2ce7eabb24054500de22f05cba4f33038c1d46ea (patch)
treeedacbe3e720f595e5609bf8d6fae84a9b76ee975 /main/strings.c
parent27a4cda821b3093bb41c29e23d1fb9d62890597a (diff)
Fix two problems in ast_str functions found while writing a unit test.
1. The documentation for ast_str_set and ast_str_append state that the max_len parameter may be -1 in order to limit the size of the ast_str to its current allocated size. The problem was that the max_len parameter in all cases was a size_t, which is unsigned. Thus a -1 was interpreted as UINT_MAX instead of -1. Changing the max_len parameter to be ssize_t fixed this issue. 2. Once issue 1 was fixed, there was an off-by-one error in the case where we attempted to write a string larger than the current allotted size to a string when -1 was passed as the max_len parameter. When trying to write more than the allotted size, the ast_str's __AST_STR_USED was set to 1 higher than it should have been. Thanks to Tilghman for quickly spotting the offending line of code. Oh, and the unit test that I referenced in the top line of this commit will be added to reviewboard shortly. Sit tight... git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@247335 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/strings.c')
-rw-r--r--main/strings.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/main/strings.c b/main/strings.c
index 040372449..6167e9b08 100644
--- a/main/strings.c
+++ b/main/strings.c
@@ -49,10 +49,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
*/
#if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
-int __ast_debug_str_helper(struct ast_str **buf, size_t max_len,
+int __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len,
int append, const char *fmt, va_list ap, const char *file, int lineno, const char *function)
#else
-int __ast_str_helper(struct ast_str **buf, size_t max_len,
+int __ast_str_helper(struct ast_str **buf, ssize_t max_len,
int append, const char *fmt, va_list ap)
#endif
{
@@ -106,12 +106,12 @@ int __ast_str_helper(struct ast_str **buf, size_t max_len,
break;
} while (1);
/* update space used, keep in mind the truncation */
- (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN : res + offset;
+ (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN - 1: res + offset;
return res;
}
-char *__ast_str_helper2(struct ast_str **buf, size_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
+char *__ast_str_helper2(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
{
int dynamic = 0;
char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR;