Browse code

Wed Feb 4 16:33:08 GMT 2004 (trog) ----------------------------------- * libclamav: ole2_extract.c: Improve error handling

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

Trog authored on 2004/02/05 01:34:18
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Wed Feb  4 16:33:08 GMT 2004 (trog)
2
+-----------------------------------
3
+  * libclamav: ole2_extract.c: Improve error handling
4
+
1 5
 Wed Feb  4 13:34:28 GMT 2004 (njh)
2 6
   * libclamav:	Binhex now removes repetitive characters before handling the
3 7
 			header (used to be the other way around)
... ...
@@ -226,7 +226,6 @@ char *get_property_name(char *name, int size)
226 226
 	}
227 227
 	newname[j] = '\0';
228 228
 	if (strlen(newname) == 0) {
229
-		printf ("zero sized newname\n");
230 229
 		free(newname);
231 230
 		return NULL;
232 231
 	}
... ...
@@ -301,6 +300,10 @@ int ole2_read_block(int fd, ole2_header_t *hdr, void *buff, int32_t blockno)
301 301
 {
302 302
 	off_t offset;
303 303
 
304
+	if (blockno < 0) {
305
+		return FALSE;
306
+	}
307
+	
304 308
 	// other methods: (blockno+1) * 512 or (blockno * block_size) + 512;
305 309
 	offset = (blockno << hdr->log2_big_block_size) + 512;	/* 512 is header size */
306 310
 	if (lseek(fd, offset, SEEK_SET) != offset) {
... ...
@@ -317,12 +320,19 @@ int32_t ole2_get_next_bat_block(int fd, ole2_header_t *hdr, int32_t current_bloc
317 317
 	int32_t bat_array_index;
318 318
 	uint32_t bat[128];
319 319
 
320
+	if (current_block < 0) {
321
+		return -1;
322
+	}
323
+	
320 324
 	bat_array_index = current_block / 128;
321 325
 	if (bat_array_index > hdr->bat_count) {
322 326
 		cli_dbgmsg("bat_array index error\n");
323 327
 		return -10;
324 328
 	}
325
-	ole2_read_block(fd, hdr, &bat, ole2_endian_convert_32(hdr->bat_array[bat_array_index]));
329
+	if (!ole2_read_block(fd, hdr, &bat,
330
+			ole2_endian_convert_32(hdr->bat_array[bat_array_index]))) {
331
+		return -1;
332
+	}
326 333
 	return ole2_endian_convert_32(bat[current_block-(bat_array_index * 128)]);
327 334
 }
328 335
 
... ...
@@ -331,6 +341,10 @@ int32_t ole2_get_next_xbat_block(int fd, ole2_header_t *hdr, int32_t current_blo
331 331
 	int32_t xbat_index, xbat_block_index, bat_index, bat_blockno;
332 332
 	uint32_t xbat[128], bat[128];
333 333
 
334
+	if (current_block < 0) {
335
+		return -1;
336
+	}
337
+	
334 338
 	xbat_index = current_block / 128;
335 339
 
336 340
 	/* NB:	The last entry in each XBAT points to the next XBAT block.
... ...
@@ -341,21 +355,32 @@ int32_t ole2_get_next_xbat_block(int fd, ole2_header_t *hdr, int32_t current_blo
341 341
 
342 342
 	bat_index = current_block % 128;
343 343
 
344
-	ole2_read_block(fd, hdr, &xbat, hdr->xbat_start);
344
+	if (!ole2_read_block(fd, hdr, &xbat, hdr->xbat_start)) {
345
+		return -1;
346
+	}
345 347
 
346 348
 	/* Follow the chain of XBAT blocks */
347 349
 	while (xbat_block_index > 0) {
348
-		ole2_read_block(fd, hdr, &xbat, ole2_endian_convert_32(xbat[127]));
350
+		if (!ole2_read_block(fd, hdr, &xbat,
351
+				ole2_endian_convert_32(xbat[127]))) {
352
+			return -1;
353
+		}
349 354
 		xbat_block_index--;
350 355
 	}
351 356
 
352
-	ole2_read_block(fd, hdr, &bat, xbat[bat_blockno]);
357
+	if (!ole2_read_block(fd, hdr, &bat, xbat[bat_blockno])) {
358
+		return -1;
359
+	}
353 360
 
354 361
 	return ole2_endian_convert_32(bat[bat_index]);
355 362
 }
356 363
 
357 364
 int32_t ole2_get_next_block_number(int fd, ole2_header_t *hdr, int32_t current_block)
358 365
 {
366
+	if (current_block < 0) {
367
+		return -1;
368
+	}
369
+
359 370
 	if ((current_block / 128) > 108) {
360 371
 		return ole2_get_next_xbat_block(fd, hdr, current_block);
361 372
 	} else {
... ...
@@ -368,13 +393,19 @@ int32_t ole2_get_next_sbat_block(int fd, ole2_header_t *hdr, int32_t current_blo
368 368
 	int32_t iter, current_bat_block;
369 369
 	uint32_t sbat[128];
370 370
 
371
+	if (current_block < 0) {
372
+		return -1;
373
+	}
374
+	
371 375
 	current_bat_block = hdr->sbat_start;
372 376
 	iter = current_block / 128;
373 377
 	while (iter > 0) {
374 378
 		current_bat_block = ole2_get_next_block_number(fd, hdr, current_bat_block);
375 379
 		iter--;
376 380
 	}
377
-	ole2_read_block(fd, hdr, &sbat, current_bat_block);
381
+	if (!ole2_read_block(fd, hdr, &sbat, current_bat_block)) {
382
+		return -1;
383
+	}
378 384
 	return ole2_endian_convert_32(sbat[current_block % 128]);
379 385
 }
380 386
 
... ...
@@ -383,6 +414,10 @@ int32_t ole2_get_sbat_data_block(int fd, ole2_header_t *hdr, void *buff, int32_t
383 383
 {
384 384
 	int32_t block_count, current_block;
385 385
 
386
+	if (sbat_index < 0) {
387
+		return FALSE;
388
+	}
389
+	
386 390
 	if (hdr->sbat_root_start < 0) {
387 391
 		cli_errmsg("No root start block\n");
388 392
 		return FALSE;
... ...
@@ -412,7 +447,10 @@ void ole2_read_property_tree(int fd, ole2_header_t *hdr, const char *dir,
412 412
 	current_block = hdr->prop_start;
413 413
 
414 414
 	while(current_block >= 0) {
415
-		ole2_read_block(fd, hdr, prop_block, current_block);
415
+		if (!ole2_read_block(fd, hdr, prop_block,
416
+					current_block)) {
417
+			return;
418
+		}
416 419
 		for (index=0 ; index < 4 ; index++) {
417 420
 			if (prop_block[index].type > 0) {
418 421
 				prop_block[index].name_size = ole2_endian_convert_16(prop_block[index].name_size);
... ...
@@ -524,7 +562,6 @@ int ole2_read_header(int fd, ole2_header_t *hdr)
524 524
 {
525 525
 	int i;
526 526
 	
527
-	fprintf(stderr, "Slow header read\n");
528 527
 	if (readn(fd, &hdr->magic, 8) != 8) {
529 528
 		return FALSE;
530 529
 	}