Browse code

improved robustness

Vaclav Kubart authored on 06/03/2006 14:42:28
Showing 8 changed files
... ...
@@ -48,6 +48,7 @@ static dstr_buff_t *add_new_buffer(dstring_t *dstr)
48 48
 		buff->used = 0;
49 49
 		dlink_add(&dstr->buffers, e);
50 50
 	}
51
+	else dstr->error = 1;
51 52
 	return buff;
52 53
 }
53 54
 
... ...
@@ -55,7 +56,10 @@ int dstr_append(dstring_t *dstr, const char *s, int len)
55 56
 {
56 57
 	int size;
57 58
 	dstr_buff_t *buff;
58
-	
59
+
60
+	if (!dstr) return -1;
61
+	if (dstr->error) return -2;
62
+
59 63
 	if (len == 0) return 0; /*append empty string*/
60 64
 	
61 65
 	buff = get_current_buffer(dstr);
... ...
@@ -70,7 +74,10 @@ int dstr_append(dstring_t *dstr, const char *s, int len)
70 74
 		dstr->len += size;
71 75
 		if (len > 0) buff = add_new_buffer(dstr);
72 76
 	}
73
-	if (!buff) return -1;
77
+	if (!buff) {
78
+		dstr->error = 1;
79
+		return -1;
80
+	}
74 81
 	return 0;
75 82
 }
76 83
 
... ...
@@ -100,6 +107,8 @@ int dstr_get_data(dstring_t *dstr, char *dst)
100 107
 	dstr_buff_t* buff;
101 108
 	
102 109
 	if (!dstr) return -1;
110
+	if (dstr->error) return -2; /* a previous operation returned error */
111
+	
103 112
 	e = dlink_start_walk(&dstr->buffers);
104 113
 	while (e) {
105 114
 		buff = (dstr_buff_t*)dlink_element_data(e);
... ...
@@ -115,6 +124,11 @@ int dstr_get_str(dstring_t *dstr, str_t *dst)
115 124
 	int res = 0;
116 125
 	
117 126
 	if (!dst) return -1;
127
+	if (dstr->error) {
128
+		str_clear(dst);
129
+		return -2; /* a previous operation returned error */
130
+	}
131
+
118 132
 	dst->len = dstr_get_data_length(dstr);
119 133
 	if (dst->len > 0) {
120 134
 		dst->s = (char*)cds_malloc(dst->len);
... ...
@@ -139,6 +153,7 @@ int dstr_init(dstring_t *dstr, int buff_size)
139 153
 	if (!dstr) return -1;
140 154
 	dstr->buff_size = buff_size;
141 155
 	dstr->len = 0;
156
+	dstr->error = 0;
142 157
 	dlink_init(&dstr->buffers);
143 158
 	return 0;
144 159
 }
... ...
@@ -159,3 +174,14 @@ int dstr_destroy(dstring_t *dstr)
159 174
 	return 0;
160 175
 }
161 176
 
177
+int dstr_error(dstring_t *dstr)
178
+{
179
+	if (dstr) return dstr->error;
180
+	else return -1;
181
+}
182
+
183
+void dstr_clear_error(dstring_t *dstr)
184
+{
185
+	if (dstr) dstr->error = 0;
186
+}
187
+
... ...
@@ -46,6 +46,9 @@ typedef struct _dstring_t {
46 46
 	/** the length of whole string */
47 47
 	int len;
48 48
 	int buff_size;
49
+
50
+	/** a operation on this string was unsuccesfull -> all other will produce error */
51
+	int error;
49 52
 } dstring_t;
50 53
 
51 54
 int dstr_append_zt(dstring_t *dstr, const char *s);
... ...
@@ -57,6 +60,10 @@ int dstr_get_str(dstring_t *dstr, str_t *dst);
57 60
 int dstr_init(dstring_t *dstr, int buff_size);
58 61
 int dstr_destroy(dstring_t *dstr);
59 62
 
63
+/* returns nozero if error !!! */
64
+int dstr_error(dstring_t *dstr);
65
+void dstr_clear_error(dstring_t *dstr);
66
+
60 67
 #ifdef __cplusplus
61 68
 }
62 69
 #endif
... ...
@@ -103,7 +103,10 @@ int sstream_get_str(sstream_t *ss, int len, str_t *dst)
103 103
 {
104 104
 	str_t tmp;
105 105
 	int res = sstream_get_str_ex(ss, len, &tmp);
106
-	if (res >= 0) str_dup(dst, &tmp);
106
+	if (res >= 0) {
107
+		res = str_dup(dst, &tmp);
108
+		if (res != 0) str_clear(dst);
109
+	}
107 110
 	return res;
108 111
 }
109 112
 
... ...
@@ -87,7 +87,10 @@ int str_dup(str_t* dst, const str_t* src)
87 87
 	if ( (!src->s) || (src->len < 1)) return 0;
88 88
 
89 89
 	dst->s = cds_malloc(src->len);
90
-	if (!dst->s) return -1;
90
+	if (!dst->s) {
91
+		/* ERROR_LOG("can't allocate memory (%d bytes)\n", src->len); */
92
+		return -1;
93
+	}
91 94
 	memcpy(dst->s, src->s, src->len);
92 95
 	dst->len = src->len;
93 96
 	return 0;
... ...
@@ -66,6 +66,7 @@ static void doc_add_presentity(dstring_t *buf, presentity_info_t *p)
66 66
 int create_lpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_type)
67 67
 {
68 68
 	dstring_t buf;
69
+	int err;
69 70
 	
70 71
 	if (!dst) return -1;
71 72
 	
... ...
@@ -74,8 +75,11 @@ int create_lpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_t
74 75
 
75 76
 	if (!p) return -1;
76 77
 	
77
-	if (dst_content_type) 
78
-		str_dup_zt(dst_content_type, "text/lpidf");
78
+	if (dst_content_type) {
79
+		if (str_dup_zt(dst_content_type, "text/lpidf") < 0) {
80
+			return -1;
81
+		}
82
+	}
79 83
 
80 84
 /*	if (!p->first_tuple) return 0;*/	/* no tuples => nothing to say */ 
81 85
 	
... ...
@@ -83,9 +87,14 @@ int create_lpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_t
83 87
 	
84 88
 	doc_add_presentity(&buf, p);
85 89
 	
86
-	dstr_get_str(&buf, dst);
90
+	err = dstr_get_str(&buf, dst);
87 91
 	dstr_destroy(&buf);
88 92
 	
89
-	return 0;
93
+	if (err != 0) {
94
+		str_free_content(dst);
95
+		if (dst_content_type) str_free_content(dst_content_type);
96
+	}
97
+	
98
+	return err;
90 99
 }
91 100
 
... ...
@@ -177,6 +177,7 @@ static void doc_add_presentity(dstring_t *buf, presentity_info_t *p, int use_cpi
177 177
 int create_pidf_document_ex(presentity_info_t *p, str_t *dst, str_t *dst_content_type, int use_cpim_pidf_ns)
178 178
 {
179 179
 	dstring_t buf;
180
+	int err;
180 181
 	
181 182
 	if (!dst) return -1;
182 183
 	
... ...
@@ -187,9 +188,10 @@ int create_pidf_document_ex(presentity_info_t *p, str_t *dst, str_t *dst_content
187 188
 	
188 189
 	if (dst_content_type) {
189 190
 		if (use_cpim_pidf_ns)
190
-			str_dup_zt(dst_content_type, "application/cpim-pidf+xml");
191
+			err = str_dup_zt(dst_content_type, "application/cpim-pidf+xml");
191 192
 		else
192
-			str_dup_zt(dst_content_type, "application/pidf+xml;charset=\"UTF-8\"");
193
+			err = str_dup_zt(dst_content_type, "application/pidf+xml;charset=\"UTF-8\"");
194
+		if (err < 0) return -1;
193 195
 	}
194 196
 	
195 197
 /*	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
199 201
 	dstr_append_zt(&buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
200 202
 	doc_add_presentity(&buf, p, use_cpim_pidf_ns);
201 203
 	
202
-	dstr_get_str(&buf, dst);
204
+	err = dstr_get_str(&buf, dst);
203 205
 	dstr_destroy(&buf);
204 206
 	
205
-	return 0;
207
+	if (err != 0) {
208
+		str_free_content(dst);
209
+		if (dst_content_type) str_free_content(dst_content_type);
210
+	}
211
+	
212
+	return err;
206 213
 }
207 214
 
208 215
 int create_pidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_type)
... ...
@@ -68,8 +68,17 @@ presence_tuple_info_t *create_tuple_info(const str_t *contact, const str_t *id,
68 68
 		return t;
69 69
 	}
70 70
 	/* str_clear(&t->contact.s); */
71
-	str_dup(&t->contact, contact);
72
-	str_dup(&t->id, id);
71
+	if (str_dup(&t->contact, contact) != 0) {
72
+		ERROR_LOG("can't allocate memory for contact\n");
73
+		cds_free(t);
74
+		return NULL;
75
+	}
76
+	if (str_dup(&t->id, id) != 0) {
77
+		ERROR_LOG("can't allocate memory for id\n");
78
+		str_free_content(&t->contact);
79
+		cds_free(t);
80
+		return NULL;
81
+	}
73 82
 	str_clear(&t->extra_status);
74 83
 	t->prev = NULL;
75 84
 	t->next = NULL;
... ...
@@ -202,8 +211,17 @@ presence_note_t *create_presence_note(const str_t *note, const str_t *lang)
202 211
 		return t;
203 212
 	}
204 213
 	/* str_clear(&t->contact.s); */
205
-	str_dup(&t->value, note);
206
-	str_dup(&t->lang, lang);
214
+	if (str_dup(&t->value, note) < 0) {
215
+		ERROR_LOG("can't duplicate note value\n");
216
+		cds_free(t);
217
+		return NULL;
218
+	}
219
+	if (str_dup(&t->lang, lang) < 0) {
220
+		ERROR_LOG("can't duplicate note lang\n");
221
+		str_free_content(&t->value);
222
+		cds_free(t);
223
+		return NULL;
224
+	}
207 225
 	t->prev = NULL;
208 226
 	t->next = NULL;
209 227
 	return t;
... ...
@@ -229,8 +247,17 @@ person_t *create_person(const str_t *element, const str_t *id)
229 247
 		return t;
230 248
 	}
231 249
 	/* str_clear(&t->contact.s); */
232
-	str_dup(&t->person_element, element);
233
-	str_dup(&t->id, id);
250
+	if (str_dup(&t->person_element, element) < 0) {
251
+		ERROR_LOG("can't duplicate person element\n");
252
+		cds_free(t);
253
+		return NULL;
254
+	}
255
+	if (str_dup(&t->id, id) < 0) {
256
+		ERROR_LOG("can't duplicate person element id\n");
257
+		str_free_content(&t->person_element);
258
+		cds_free(t);
259
+		return NULL;
260
+	}
234 261
 	t->next = NULL;
235 262
 	return t;
236 263
 }
... ...
@@ -132,6 +132,7 @@ static void doc_add_presentity(dstring_t *buf, presentity_info_t *p)
132 132
 int create_xpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_type)
133 133
 {
134 134
 	dstring_t buf;
135
+	int err = 0;
135 136
 	
136 137
 	if (!dst) return -1;
137 138
 	
... ...
@@ -141,7 +142,9 @@ int create_xpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_t
141 142
 	if (!p) return -1;
142 143
 	
143 144
 	if (dst_content_type) 
144
-		str_dup_zt(dst_content_type, "application/xpidf+xml;charset=\"UTF-8\"");
145
+		if (str_dup_zt(dst_content_type, "application/xpidf+xml;charset=\"UTF-8\"") < 0) {
146
+			return -1;
147
+		}
145 148
 
146 149
 /*	if (!p->first_tuple) return 0;*/	/* no tuples => nothing to say */ 
147 150
 	
... ...
@@ -151,9 +154,14 @@ int create_xpidf_document(presentity_info_t *p, str_t *dst, str_t *dst_content_t
151 154
 	dstr_append_zt(&buf, "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\r\n");
152 155
 	doc_add_presentity(&buf, p);
153 156
 	
154
-	dstr_get_str(&buf, dst);
157
+	err = dstr_get_str(&buf, dst);
155 158
 	dstr_destroy(&buf);
159
+
160
+	if (err != 0) {
161
+		str_free_content(dst);
162
+		if (dst_content_type) str_free_content(dst_content_type);
163
+	}
156 164
 	
157
-	return 0;
165
+	return err;
158 166
 }
159 167