From 50d20994f8657928753cbe7bb1b3346f0bcd5209 Mon Sep 17 00:00:00 2001 From: Mark Spencer Date: Tue, 18 Oct 2005 16:48:19 +0000 Subject: [PATCH] Fix off-by-one issue with sort (bug #5459) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6816 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- apps/app_cut.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/app_cut.c b/apps/app_cut.c index dd40bb4b06..9c0629adb4 100755 --- a/apps/app_cut.c +++ b/apps/app_cut.c @@ -93,7 +93,7 @@ static int sort_subroutine(const void *arg1, const void *arg2) static int sort_internal(struct ast_channel *chan, char *data, char *buffer, size_t buflen) { char *strings, *ptrkey, *ptrvalue; - int count=1, count2; + int count=1, count2, element_count=0; struct sortable_keys *sortable_keys; memset(buffer, 0, buflen); @@ -139,13 +139,13 @@ static int sort_internal(struct ast_channel *chan, char *data, char *buffer, siz qsort(sortable_keys, count, sizeof(struct sortable_keys), sort_subroutine); for (count2 = 0; count2 < count; count2++) { - strncat(buffer + strlen(buffer), sortable_keys[count2].key, buflen - strlen(buffer)); - strncat(buffer + strlen(buffer), ",", buflen - strlen(buffer)); + int blen = strlen(buffer); + if (element_count++) { + strncat(buffer + blen, ",", buflen - blen - 1); + } + strncat(buffer + blen + 1, sortable_keys[count2].key, buflen - blen - 2); } - /* Remove trailing comma */ - buffer[strlen(buffer) - 1] = '\0'; - return 0; }