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;
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);
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)) {