fix svace issues 21/113621/4
authorJongkyu Koo <jk.koo@samsung.com>
Wed, 8 Feb 2017 07:37:08 +0000 (16:37 +0900)
committerJongkyu Koo <jk.koo@samsung.com>
Wed, 8 Feb 2017 11:56:00 +0000 (03:56 -0800)
Change-Id: Ic55c9561db05797631ee3a00bd9a73a2ab005a0f
Signed-off-by: Jongkyu Koo <jk.koo@samsung.com>
common/ctsvc_vcard.c

index 993bad4..41bf611 100644 (file)
@@ -4436,12 +4436,26 @@ EXPORT_API int contacts_vcard_parse_to_contacts(const char *vcard_stream, contac
        return CONTACTS_ERROR_NONE;
 }
 
+
+static int  _ctsvc_safe_add(unsigned int* total, unsigned int value)
+{
+       const unsigned int unsigned_int_max = 4294967295;
+       if (unsigned_int_max -*total < value) {
+               ERR("overflow occurs when %d + %d", *total, value);
+               return CONTACTS_ERROR_SYSTEM;
+       }
+
+       *total += value;
+       return CONTACTS_ERROR_NONE;
+}
+
 EXPORT_API int contacts_vcard_parse_to_contact_foreach(const char *vcard_file_name,
                contacts_vcard_parse_cb cb, void *data)
 {
        contacts_record_h record;
        FILE *file;
-       int buf_size, len;
+       unsigned int buf_size, len;
+       int written_len;
        int ret;
        int vcard_depth = 0;
        char *stream;
@@ -4467,9 +4481,7 @@ EXPORT_API int contacts_vcard_parse_to_contact_foreach(const char *vcard_file_na
                        if (STRING_EQUAL != strncmp(line, "BEGIN:VCARD", strlen("BEGIN:VCARD")))
                                continue;
 
-               if (len + sizeof(line) < buf_size) {
-                       len += snprintf(stream + len, buf_size - len, "%s", line);
-               } else {
+               if (buf_size - len <= strlen(line)) {
                        char *new_stream;
                        buf_size += sizeof(line) * 2;
                        new_stream = realloc(stream, buf_size);
@@ -4480,8 +4492,21 @@ EXPORT_API int contacts_vcard_parse_to_contact_foreach(const char *vcard_file_na
                                fclose(file);
                                return CONTACTS_ERROR_OUT_OF_MEMORY;
                        }
+               }
+               written_len = snprintf(stream + len, buf_size - len, "%s", line);
+               if (written_len < 0) {
+                       free(stream);
+                       fclose(file);
+                       ERR("snprintf() Fail(%d)", written_len);
+                       return CONTACTS_ERROR_SYSTEM;
+               }
 
-                       len += snprintf(stream + len, buf_size - len, "%s", line);
+               ret = _ctsvc_safe_add(&len, (unsigned int)written_len);
+               if (CONTACTS_ERROR_NONE != ret) {
+                       free(stream);
+                       fclose(file);
+                       ERR("_ctsvc_safe_add() Fail", len, written_len);
+                       return ret;
                }
 
                if (STRING_EQUAL == strncmp(line, "END:VCARD", 9)) {