Add SAFE_SNPRINTF macro. 33/41533/1 accepted/tizen/mobile/20150617.044510 accepted/tizen/tv/20150617.044525 accepted/tizen/wearable/20150617.044539 submit/tizen/20150617.015440
authorMinje Ahn <minje.ahn@samsung.com>
Tue, 16 Jun 2015 09:59:34 +0000 (18:59 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Tue, 16 Jun 2015 09:59:34 +0000 (18:59 +0900)
Change-Id: I792e65185b3ebb40eba614cc2b43dc11376188b4
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-svc-localize-utils.c

index d2468c75c3968f67efb31b05187b879bf01dcbfb..e09470de28f4925880cbab8fa06fa8b6c52b1c44 100755 (executable)
  * limitations under the License.
  *
  */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
 
 #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;
+       }
+}