Browse code

Error path cleanup. Fix bug spotted by Nigel.

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@869 77e5149b-7576-45b1-b177-96237e5ba77b

Trog authored on 2004/09/15 21:28:43
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Wed Sep 15 13:27:24 BST 2004 (trog)
2
+-----------------------------------
3
+  * libclamav/htmlnorm.c: Error path cleanup. Fix bug spotted by Nigel.
4
+
1 5
 Wed Sep 15 10:04:52 BST 2004 (trog)
2 6
 -----------------------------------
3 7
   * libclamav/htmlnorm.c: Preserve the case of href tags. Minor cleanup.
... ...
@@ -150,7 +150,7 @@ static unsigned char *cli_readline(FILE *stream, m_area_t *m_area, unsigned int
150 150
 	unsigned char *line, *ptr, *start, *end;
151 151
 	unsigned int line_len, count;
152 152
 
153
-	line = (unsigned char *) malloc(max_len);
153
+	line = (unsigned char *) cli_malloc(max_len);
154 154
 	if (!line) {
155 155
 		return NULL;
156 156
 	}
... ...
@@ -284,15 +284,17 @@ static void html_tag_arg_set(tag_arguments_t *tags, char *tag, char *value)
284 284
 static void html_tag_arg_add(tag_arguments_t *tags,
285 285
 		unsigned char *tag, unsigned char *value)
286 286
 {
287
-	int len;
287
+	int len, i;
288 288
 	tags->count++;
289 289
 	tags->tag = (unsigned char **) cli_realloc(tags->tag,
290 290
 				tags->count * sizeof(char *));
291
+	if (!tags->tag) {
292
+		goto abort;
293
+	}
291 294
 	tags->value = (unsigned char **) cli_realloc(tags->value,
292 295
 				tags->count * sizeof(char *));
293
-	if (!tags->tag || !tags->value) {
294
-		tags->count--;
295
-		return;
296
+	if (!tags->value) {
297
+		goto abort;
296 298
 	}
297 299
 	tags->tag[tags->count-1] = strdup(tag);
298 300
 	if (value) {
... ...
@@ -308,6 +310,28 @@ static void html_tag_arg_add(tag_arguments_t *tags,
308 308
 	} else {
309 309
 		tags->value[tags->count-1] = NULL;
310 310
 	}
311
+	return;
312
+	
313
+abort:
314
+	/* Bad error - can't do 100% recovery */
315
+	tags->count--;
316
+	for (i=0; i < tags->count; i++) {
317
+		if (tags->tag) {
318
+			free(tags->tag[i]);
319
+		}
320
+		if (tags->value) {
321
+			free(tags->value[i]);
322
+		}
323
+	}
324
+	if (tags->tag) {
325
+		free(tags->tag);
326
+	}
327
+	if (tags->value) {
328
+		free(tags->value);
329
+	}
330
+	tags->tag = tags->value = NULL;
331
+	tags->count = 0;	
332
+	return;
311 333
 }
312 334
 
313 335
 static void html_output_tag(file_buff_t *fbuff, char *tag, tag_arguments_t *tags)
... ...
@@ -381,21 +405,42 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
381 381
 			return FALSE;
382 382
 		}
383 383
 	}
384
+
385
+	tag_args.count = 0;
386
+	tag_args.tag = NULL;
387
+	tag_args.value = NULL;
384 388
 	
385 389
 	if (dirname) {
386 390
 		file_buff_o1 = (file_buff_t *) cli_malloc(sizeof(file_buff_t));
391
+		if (!file_buff_o1) {
392
+			file_buff_o1 = file_buff_o2 = file_buff_script = NULL;
393
+			goto abort;
394
+		}
395
+		
387 396
 		file_buff_o2 = (file_buff_t *) cli_malloc(sizeof(file_buff_t));
397
+		if (!file_buff_o2) {
398
+			free(file_buff_o1);
399
+			file_buff_o1 = file_buff_o2 = file_buff_script = NULL;
400
+			goto abort;
401
+		}
402
+		
388 403
 		file_buff_script = (file_buff_t *) cli_malloc(sizeof(file_buff_t));
404
+		if (!file_buff_script) {
405
+			free(file_buff_o1);
406
+			free(file_buff_o2);
407
+			file_buff_o1 = file_buff_o2 = file_buff_script = NULL;
408
+			goto abort;
409
+		}
389 410
 		
390 411
 		snprintf(filename, 1024, "%s/comment.html", dirname);
391 412
 		file_buff_o1->fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
392 413
 		if (!file_buff_o1->fd) {
393 414
 			cli_dbgmsg("open failed: %s\n", filename);
394
-			fclose(stream_in);
395 415
 			free(file_buff_o1);
396 416
 			free(file_buff_o2);
397 417
 			free(file_buff_script);
398
-			return FALSE;
418
+			file_buff_o1 = file_buff_o2 = file_buff_script = NULL;
419
+			goto abort;
399 420
 		}
400 421
 
401 422
 		snprintf(filename, 1024, "%s/nocomment.html", dirname);
... ...
@@ -403,11 +448,11 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
403 403
 		if (!file_buff_o2->fd) {
404 404
 			cli_dbgmsg("open failed: %s\n", filename);
405 405
 			close(file_buff_o1->fd);
406
-			fclose(stream_in);
407 406
 			free(file_buff_o1);
408 407
 			free(file_buff_o2);
409 408
 			free(file_buff_script);
410
-			return FALSE;
409
+			file_buff_o1 = file_buff_o2 = file_buff_script = NULL;
410
+			goto abort;
411 411
 		}
412 412
 
413 413
 		snprintf(filename, 1024, "%s/script.html", dirname);
... ...
@@ -416,11 +461,11 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
416 416
 			cli_dbgmsg("open failed: %s\n", filename);
417 417
 			close(file_buff_o1->fd);
418 418
 			close(file_buff_o2->fd);
419
-			fclose(stream_in);
420 419
 			free(file_buff_o1);
421 420
 			free(file_buff_o2);
422 421
 			free(file_buff_script);
423
-			return FALSE;
422
+			file_buff_o1 = file_buff_o2 = file_buff_script = NULL;
423
+			goto abort;
424 424
 		}
425 425
 
426 426
 		file_buff_o1->length = 0;
... ...
@@ -431,11 +476,7 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
431 431
 		file_buff_o2 = NULL;
432 432
 		file_buff_script = NULL;
433 433
 	}
434
-	
435
-	tag_args.count = 0;
436
-	tag_args.tag = NULL;
437
-	tag_args.value = NULL;
438
-		
434
+			
439 435
 	ptr = line = cli_readline(stream_in, m_area, 8192);
440 436
 	while (line) {
441 437
 		while (*ptr && isspace(*ptr)) {
... ...
@@ -519,7 +560,7 @@ static int cli_html_normalise(int fd, m_area_t *m_area, const char *dirname, tag
519 519
 						html_output_c(file_buff_script, NULL, '!');
520 520
 					}
521 521
 					/* Need to rewind in the no-comment output stream */
522
-					if (file_buff_o2->length > 0) {
522
+					if (file_buff_o2 && (file_buff_o2->length > 0)) {
523 523
 						file_buff_o2->length--;
524 524
 					}
525 525
 					state = HTML_COMMENT;