From b4e678090fbf12bb59c5d267785a25c815db1002 Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Tue, 16 Jun 2015 18:59:34 +0900 Subject: [PATCH] Add SAFE_SNPRINTF macro. Change-Id: I792e65185b3ebb40eba614cc2b43dc11376188b4 Signed-off-by: Minje Ahn --- src/common/media-svc-localize-utils.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/common/media-svc-localize-utils.c b/src/common/media-svc-localize-utils.c index d2468c7..e09470d 100755 --- a/src/common/media-svc-localize-utils.c +++ b/src/common/media-svc-localize-utils.c @@ -16,11 +16,16 @@ * limitations under the License. * */ +#include +#include +#include #include "media-util-err.h" #include "media-svc-debug.h" #include "media-svc-localize-utils.h" +#define SAFE_STRLEN(src) ((src)?strlen(src):0) + int _media_svc_check_utf8(char c) { if ((c & 0xff) < (128 & 0xff)) @@ -39,3 +44,32 @@ int _media_svc_check_utf8(char c) return MS_MEDIA_ERR_INVALID_PARAMETER; } +int SAFE_SNPRINTF(char **buf, int *buf_size, int len, const char *src) +{ + int remain; + int temp_len; + + if (len < 0) + return -1; + + remain = *buf_size - len; + if (remain > strlen(src) + 1) { + temp_len = snprintf((*buf)+len, remain, "%s", src); + return temp_len; + } + else { + char *temp; + while(1) { + temp = realloc(*buf, *buf_size*2); + if (NULL == temp) + return -1; + *buf = temp; + *buf_size = *buf_size * 2; + remain = *buf_size - len; + if (remain > strlen(src) + 1) + break; + } + temp_len = snprintf((*buf)+len, remain, "%s", src); + return temp_len; + } +} -- 2.7.4