Browse code

improve error handling

git-svn: trunk@2226

Tomasz Kojm authored on 2006/09/01 04:43:51
Showing 3 changed files
... ...
@@ -1,3 +1,7 @@
1
+Thu Aug 31 21:43:09 CEST 2006 (tk)
2
+----------------------------------
3
+  * libclamav/unrar: improve error handling
4
+
1 5
 Thu Aug 31 14:49:42 BST 2006 (njh)
2 6
 ----------------------------------
3 7
   * libclamav/pe.c:	Added Windows support (with permission from ACAB)
... ...
@@ -648,7 +648,10 @@ static int read_tables(int fd, unpack_data_t *unpack_data)
648 648
 	if (bit_field & 0x8000) {
649 649
 		unpack_data->unp_block_type = BLOCK_PPM;
650 650
 		rar_dbgmsg("Calling ppm_decode_init\n");
651
-		ppm_decode_init(&unpack_data->ppm_data, fd, unpack_data, &unpack_data->ppm_esc_char);
651
+		if(!ppm_decode_init(&unpack_data->ppm_data, fd, unpack_data, &unpack_data->ppm_esc_char)) {
652
+		    cli_dbgmsg("unrar: read_tables: ppm_decode_init failed\n");
653
+		    return FALSE;
654
+		}
652 655
 		return(TRUE);
653 656
 	}
654 657
 	unpack_data->unp_block_type = BLOCK_LZ;
... ...
@@ -1439,7 +1442,7 @@ rar_metadata_t *cli_unrar(int fd, const char *dirname, const struct cl_limits *l
1439 1439
 				cli_dbgmsg("Computed File CRC: 0x%x\n", unpack_data->unp_crc^0xffffffff);
1440 1440
 				if (unpack_data->unp_crc != 0xffffffff) {
1441 1441
 					if (file_header->file_crc != (unpack_data->unp_crc^0xffffffff)) {
1442
-						cli_warnmsg("RAR CRC error. Please send file to trog@clamav.net");
1442
+						cli_warnmsg("RAR CRC error. Please report the bug at https://bugs.clamav.net/\n");
1443 1443
 					}
1444 1444
 				}
1445 1445
 			}
... ...
@@ -374,7 +374,7 @@ static void update(struct see2_context_tag *see2_cont)
374 374
 	}
375 375
 }
376 376
 
377
-static void restart_model_rare(ppm_data_t *ppm_data)
377
+static int restart_model_rare(ppm_data_t *ppm_data)
378 378
 {
379 379
 	int i, k, m;
380 380
 	static const uint16_t init_bin_esc[] = {
... ...
@@ -388,11 +388,19 @@ static void restart_model_rare(ppm_data_t *ppm_data)
388 388
 	ppm_data->init_rl=-(ppm_data->max_order < 12 ? ppm_data->max_order:12)-1;
389 389
 	ppm_data->min_context = ppm_data->max_context =
390 390
 		(struct ppm_context *) sub_allocator_alloc_context(&ppm_data->sub_alloc);
391
+	if(!ppm_data->min_context) {
392
+	    cli_errmsg("unrar: restart_model_rare: sub_allocator_alloc_context failed\n");
393
+	    return -1;
394
+	}
391 395
 	ppm_data->min_context->suffix = NULL;
392 396
 	ppm_data->order_fall = ppm_data->max_order;
393 397
 	ppm_data->min_context->con_ut.u.summ_freq = (ppm_data->min_context->num_stats=256)+1;
394 398
 	ppm_data->found_state = ppm_data->min_context->con_ut.u.stats=
395 399
 		(struct state_tag *)sub_allocator_alloc_units(&ppm_data->sub_alloc, 256/2);
400
+	if(!ppm_data->found_state) {
401
+	    cli_errmsg("unrar: restart_model_rare: sub_allocator_alloc_units failed\n");
402
+	    return -1;
403
+	}
396 404
 	for (ppm_data->run_length = ppm_data->init_rl, ppm_data->prev_success=i=0; i < 256 ; i++) {
397 405
 		ppm_data->min_context->con_ut.u.stats[i].symbol = i;
398 406
 		ppm_data->min_context->con_ut.u.stats[i].freq = 1;
... ...
@@ -411,16 +419,21 @@ static void restart_model_rare(ppm_data_t *ppm_data)
411 411
 			see2_init(&ppm_data->see2cont[i][k], 5*i+10);
412 412
 		}
413 413
 	}
414
+
415
+	return 0;
414 416
 }
415 417
 	
416
-static void start_model_rare(ppm_data_t *ppm_data, int max_order)
418
+static int start_model_rare(ppm_data_t *ppm_data, int max_order)
417 419
 {
418
-	int i, k, m, step;
420
+	int i, k, m, step, ret;
419 421
 	
420 422
 	ppm_data->esc_count = 1;
421 423
 	ppm_data->max_order = max_order;
422 424
 	
423
-	restart_model_rare(ppm_data);
425
+	if((ret = restart_model_rare(ppm_data))) {
426
+	    cli_dbgmsg("unrar: start_model_rare: restart_model_rare failed\n");
427
+	    return ret;
428
+	}
424 429
 	
425 430
 	ppm_data->ns2bsindx[0] = 2*0;
426 431
 	ppm_data->ns2bsindx[1] = 2*1;
... ...
@@ -441,6 +454,7 @@ static void start_model_rare(ppm_data_t *ppm_data, int max_order)
441 441
 	memset(ppm_data->hb2flag, 0, 0x40);
442 442
 	memset(ppm_data->hb2flag+0x40, 0x08, 0x100-0x40);
443 443
 	ppm_data->dummy_sse2cont.shift = PERIOD_BITS;
444
+	return 0;
444 445
 }
445 446
 
446 447
 	
... ...
@@ -599,11 +613,12 @@ NO_LOOP:
599 599
 	return pc;
600 600
 }
601 601
 
602
-static void update_model(ppm_data_t *ppm_data)
602
+static int update_model(ppm_data_t *ppm_data)
603 603
 {
604 604
 	struct state_tag fs, *p;
605 605
 	struct ppm_context *pc, *successor;
606 606
 	unsigned int ns1, ns, cf, sf, s0;
607
+	int ret;
607 608
 	
608 609
 	rar_dbgmsg("in update_model\n");
609 610
 	fs = *ppm_data->found_state;
... ...
@@ -635,7 +650,7 @@ static void update_model(ppm_data_t *ppm_data)
635 635
 		if (!ppm_data->min_context) {
636 636
 			goto RESTART_MODEL;
637 637
 		}
638
-		return;
638
+		return 0;
639 639
 	}
640 640
 	*ppm_data->sub_alloc.ptext++ = fs.symbol;
641 641
 	successor = (struct ppm_context *) ppm_data->sub_alloc.ptext;
... ...
@@ -697,11 +712,15 @@ static void update_model(ppm_data_t *ppm_data)
697 697
 		pc->num_stats = ++ns1;
698 698
 	}
699 699
 	ppm_data->max_context = ppm_data->min_context = fs.successor;
700
-	return;
700
+	return 0;
701 701
 	
702 702
 RESTART_MODEL:
703
-	restart_model_rare(ppm_data);
703
+	if((ret = restart_model_rare(ppm_data))) {
704
+	    cli_dbgmsg("unrar: update_model: restart_model_rare: failed\n");
705
+	    return ret;
706
+	}
704 707
 	ppm_data->esc_count = 0;
708
+	return 0;
705 709
 }
706 710
 
707 711
 static void update1(ppm_data_t *ppm_data, struct state_tag *p, struct ppm_context *context)
... ...
@@ -928,8 +947,14 @@ int ppm_decode_init(ppm_data_t *ppm_data, int fd, unpack_data_t *unpack_data, in
928 928
 			sub_allocator_stop_sub_allocator(&ppm_data->sub_alloc);
929 929
 			return FALSE;
930 930
 		}
931
-		sub_allocator_start_sub_allocator(&ppm_data->sub_alloc, MaxMB+1);
932
-		start_model_rare(ppm_data, max_order);
931
+		if(!sub_allocator_start_sub_allocator(&ppm_data->sub_alloc, MaxMB+1)) {
932
+		    sub_allocator_stop_sub_allocator(&ppm_data->sub_alloc);
933
+		    return FALSE;
934
+		}
935
+		if(start_model_rare(ppm_data, max_order) < 0) {
936
+		    sub_allocator_stop_sub_allocator(&ppm_data->sub_alloc);
937
+		    return FALSE;
938
+		}
933 939
 	}
934 940
 	rar_dbgmsg("ppm_decode_init done: %d\n", ppm_data->min_context != NULL);
935 941
 	return (ppm_data->min_context != NULL);
... ...
@@ -974,7 +999,11 @@ int ppm_decode_char(ppm_data_t *ppm_data, int fd, unpack_data_t *unpack_data)
974 974
 	if (!ppm_data->order_fall && (uint8_t *) ppm_data->found_state->successor > ppm_data->sub_alloc.ptext) {
975 975
 		ppm_data->min_context = ppm_data->max_context = ppm_data->found_state->successor;
976 976
 	} else {
977
-		update_model(ppm_data);
977
+		if(update_model(ppm_data)) {
978
+		    cli_dbgmsg("unrar: ppm_decode_char: update_model failed\n");
979
+		    return -1;
980
+		}
981
+
978 982
 		if (ppm_data->esc_count == 0) {
979 983
 			clear_mask(ppm_data);
980 984
 		}