Jelajahi Sumber

improved robustness

Vaclav Kubart 19 tahun lalu
induk
melakukan
c0ce5c29fe
8 mengubah file dengan 111 tambahan dan 21 penghapusan
  1. 28 2
      lib/cds/dstring.c
  2. 7 0
      lib/cds/dstring.h
  3. 4 1
      lib/cds/serialize.c
  4. 4 1
      lib/cds/sstr.c
  5. 13 4
      lib/presence/lpidf.c
  6. 11 4
      lib/presence/pidf.c
  7. 33 6
      lib/presence/pres_doc.c
  8. 11 3
      lib/presence/xpidf.c

+ 28 - 2
lib/cds/dstring.c

@@ -48,6 +48,7 @@ static dstr_buff_t *add_new_buffer(dstring_t *dstr)
 		buff->used = 0;
 		dlink_add(&dstr->buffers, e);
 	}
+	else dstr->error = 1;
 	return buff;
 }
 
@@ -55,7 +56,10 @@ int dstr_append(dstring_t *dstr, const char *s, int len)
 {
 	int size;
 	dstr_buff_t *buff;
-	
+
+	if (!dstr) return -1;
+	if (dstr->error) return -2;
+
 	if (len == 0) return 0; /*append empty string*/
 	
 	buff = get_current_buffer(dstr);
@@ -70,7 +74,10 @@ int dstr_append(dstring_t *dstr, const char *s, int len)
 		dstr->len += size;
 		if (len > 0) buff = add_new_buffer(dstr);
 	}
-	if (!buff) return -1;
+	if (!buff) {
+		dstr->error = 1;
+		return -1;
+	}
 	return 0;
 }
 
@@ -100,6 +107,8 @@ int dstr_get_data(dstring_t *dstr, char *dst)
 	dstr_buff_t* buff;
 	
 	if (!dstr) return -1;
+	if (dstr->error) return -2; /* a previous operation returned error */
+	
 	e = dlink_start_walk(&dstr->buffers);
 	while (e) {
 		buff = (dstr_buff_t*)dlink_element_data(e);
@@ -115,6 +124,11 @@ int dstr_get_str(dstring_t *dstr, str_t *dst)
 	int res = 0;
 	
 	if (!dst) return -1;
+	if (dstr->error) {
+		str_clear(dst);
+		return -2; /* a previous operation returned error */
+	}
+
 	dst->len = dstr_get_data_length(dstr);
 	if (dst->len > 0) {
 		dst->s = (char*)cds_malloc(dst->len);
@@ -139,6 +153,7 @@ int dstr_init(dstring_t *dstr, int buff_size)
 	if (!dstr) return -1;
 	dstr->buff_size = buff_size;
 	dstr->len = 0;
+	dstr->error = 0;
 	dlink_init(&dstr->buffers);
 	return 0;
 }
@@ -159,3 +174,14 @@ int dstr_destroy(dstring_t *dstr)
 	return 0;
 }
 
+int dstr_error(dstring_t *dstr)
+{
+	if (dstr) return dstr->error;
+	else return -1;
+}
+
+void dstr_clear_error(dstring_t *dstr)
+{
+	if (dstr) dstr->error = 0;
+}
+

+ 7 - 0
lib/cds/dstring.h

@@ -46,6 +46,9 @@ typedef struct _dstring_t {
 	/** the length of whole string */
 	int len;
 	int buff_size;
+
+	/** a operation on this string was unsuccesfull -> all other will produce error */
+	int error;
 } dstring_t;
 
 int dstr_append_zt(dstring_t *dstr, const char *s);
@@ -57,6 +60,10 @@ int dstr_get_str(dstring_t *dstr, str_t *dst);
 int dstr_init(dstring_t *dstr, int buff_size);
 int dstr_destroy(dstring_t *dstr);
 
+/* returns nozero if error !!! */
+int dstr_error(dstring_t *dstr);
+void dstr_clear_error(dstring_t *dstr);
+
 #ifdef __cplusplus
 }
 #endif

+ 4 - 1
lib/cds/serialize.c

@@ -103,7 +103,10 @@ int sstream_get_str(sstream_t *ss, int len, str_t *dst)
 {
 	str_t tmp;
 	int res = sstream_get_str_ex(ss, len, &tmp);
-	if (res >= 0) str_dup(dst, &tmp);
+	if (res >= 0) {
+		res = str_dup(dst, &tmp);
+		if (res != 0) str_clear(dst);
+	}
 	return res;
 }
 

+ 4 - 1
lib/cds/sstr.c

@@ -87,7 +87,10 @@ int str_dup(str_t* dst, const str_t* src)
 	if ( (!src->s) || (src->len < 1)) return 0;
 
 	dst->s = cds_malloc(src->len);
-	if (!dst->s) return -1;
+	if (!dst->s) {
+		/* ERROR_LOG("can't allocate memory (%d bytes)\n", src->len); */
+		return -1;
+	}
 	memcpy(dst->s, src->s, src->len);
 	dst->len = src->len;
 	return 0;

+ 13 - 4
lib/presence/lpidf.c

@@ -66,6 +66,7 @@ static void doc_add_presentity(dstring_t *buf, presentity_info_t *p)
 int create_lpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_type)
 {
 	dstring_t buf;
+	int err;
 	
 	if (!dst) return -1;
 	
@@ -74,8 +75,11 @@ int create_lpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_t
 
 	if (!p) return -1;
 	
-	if (dst_content_type) 
-		str_dup_zt(dst_content_type, "text/lpidf");
+	if (dst_content_type) {
+		if (str_dup_zt(dst_content_type, "text/lpidf") < 0) {
+			return -1;
+		}
+	}
 
 /*	if (!p->first_tuple) return 0;*/	/* no tuples => nothing to say */ 
 	
@@ -83,9 +87,14 @@ int create_lpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_t
 	
 	doc_add_presentity(&buf, p);
 	
-	dstr_get_str(&buf, dst);
+	err = dstr_get_str(&buf, dst);
 	dstr_destroy(&buf);
 	
-	return 0;
+	if (err != 0) {
+		str_free_content(dst);
+		if (dst_content_type) str_free_content(dst_content_type);
+	}
+	
+	return err;
 }
 

+ 11 - 4
lib/presence/pidf.c

@@ -177,6 +177,7 @@ static void doc_add_presentity(dstring_t *buf, presentity_info_t *p, int use_cpi
 int create_pidf_document_ex(presentity_info_t *p, str_t *dst, str_t *dst_content_type, int use_cpim_pidf_ns)
 {
 	dstring_t buf;
+	int err;
 	
 	if (!dst) return -1;
 	
@@ -187,9 +188,10 @@ int create_pidf_document_ex(presentity_info_t *p, str_t *dst, str_t *dst_content
 	
 	if (dst_content_type) {
 		if (use_cpim_pidf_ns)
-			str_dup_zt(dst_content_type, "application/cpim-pidf+xml");
+			err = str_dup_zt(dst_content_type, "application/cpim-pidf+xml");
 		else
-			str_dup_zt(dst_content_type, "application/pidf+xml;charset=\"UTF-8\"");
+			err = str_dup_zt(dst_content_type, "application/pidf+xml;charset=\"UTF-8\"");
+		if (err < 0) return -1;
 	}
 	
 /*	if (!p->first_tuple) return 0;*/	/* no tuples => nothing to say */ 
@@ -199,10 +201,15 @@ int create_pidf_document_ex(presentity_info_t *p, str_t *dst, str_t *dst_content
 	dstr_append_zt(&buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
 	doc_add_presentity(&buf, p, use_cpim_pidf_ns);
 	
-	dstr_get_str(&buf, dst);
+	err = dstr_get_str(&buf, dst);
 	dstr_destroy(&buf);
 	
-	return 0;
+	if (err != 0) {
+		str_free_content(dst);
+		if (dst_content_type) str_free_content(dst_content_type);
+	}
+	
+	return err;
 }
 
 int create_pidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_type)

+ 33 - 6
lib/presence/pres_doc.c

@@ -68,8 +68,17 @@ presence_tuple_info_t *create_tuple_info(const str_t *contact, const str_t *id,
 		return t;
 	}
 	/* str_clear(&t->contact.s); */
-	str_dup(&t->contact, contact);
-	str_dup(&t->id, id);
+	if (str_dup(&t->contact, contact) != 0) {
+		ERROR_LOG("can't allocate memory for contact\n");
+		cds_free(t);
+		return NULL;
+	}
+	if (str_dup(&t->id, id) != 0) {
+		ERROR_LOG("can't allocate memory for id\n");
+		str_free_content(&t->contact);
+		cds_free(t);
+		return NULL;
+	}
 	str_clear(&t->extra_status);
 	t->prev = NULL;
 	t->next = NULL;
@@ -202,8 +211,17 @@ presence_note_t *create_presence_note(const str_t *note, const str_t *lang)
 		return t;
 	}
 	/* str_clear(&t->contact.s); */
-	str_dup(&t->value, note);
-	str_dup(&t->lang, lang);
+	if (str_dup(&t->value, note) < 0) {
+		ERROR_LOG("can't duplicate note value\n");
+		cds_free(t);
+		return NULL;
+	}
+	if (str_dup(&t->lang, lang) < 0) {
+		ERROR_LOG("can't duplicate note lang\n");
+		str_free_content(&t->value);
+		cds_free(t);
+		return NULL;
+	}
 	t->prev = NULL;
 	t->next = NULL;
 	return t;
@@ -229,8 +247,17 @@ person_t *create_person(const str_t *element, const str_t *id)
 		return t;
 	}
 	/* str_clear(&t->contact.s); */
-	str_dup(&t->person_element, element);
-	str_dup(&t->id, id);
+	if (str_dup(&t->person_element, element) < 0) {
+		ERROR_LOG("can't duplicate person element\n");
+		cds_free(t);
+		return NULL;
+	}
+	if (str_dup(&t->id, id) < 0) {
+		ERROR_LOG("can't duplicate person element id\n");
+		str_free_content(&t->person_element);
+		cds_free(t);
+		return NULL;
+	}
 	t->next = NULL;
 	return t;
 }

+ 11 - 3
lib/presence/xpidf.c

@@ -132,6 +132,7 @@ static void doc_add_presentity(dstring_t *buf, presentity_info_t *p)
 int create_xpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_type)
 {
 	dstring_t buf;
+	int err = 0;
 	
 	if (!dst) return -1;
 	
@@ -141,7 +142,9 @@ int create_xpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_t
 	if (!p) return -1;
 	
 	if (dst_content_type) 
-		str_dup_zt(dst_content_type, "application/xpidf+xml;charset=\"UTF-8\"");
+		if (str_dup_zt(dst_content_type, "application/xpidf+xml;charset=\"UTF-8\"") < 0) {
+			return -1;
+		}
 
 /*	if (!p->first_tuple) return 0;*/	/* no tuples => nothing to say */ 
 	
@@ -151,9 +154,14 @@ int create_xpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_t
 	dstr_append_zt(&buf, "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\r\n");
 	doc_add_presentity(&buf, p);
 	
-	dstr_get_str(&buf, dst);
+	err = dstr_get_str(&buf, dst);
 	dstr_destroy(&buf);
+
+	if (err != 0) {
+		str_free_content(dst);
+		if (dst_content_type) str_free_content(dst_content_type);
+	}
 	
-	return 0;
+	return err;
 }