Browse Source

[slam] Avoid potential division by zero

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 8 years ago
parent
commit
4ddd3d99c3
1 changed files with 13 additions and 7 deletions
  1. 13
    7
      src/net/udp/slam.c

+ 13
- 7
src/net/udp/slam.c View File

@@ -415,6 +415,8 @@ static int slam_pull_value ( struct slam_request *slam,
415 415
 static int slam_pull_header ( struct slam_request *slam,
416 416
 			      struct io_buffer *iobuf ) {
417 417
 	void *header = iobuf->data;
418
+	unsigned long total_bytes;
419
+	unsigned long block_size;
418 420
 	int rc;
419 421
 
420 422
 	/* If header matches cached header, just pull it and return */
@@ -431,22 +433,26 @@ static int slam_pull_header ( struct slam_request *slam,
431 433
 	 */
432 434
 	if ( ( rc = slam_pull_value ( slam, iobuf, NULL ) ) != 0 )
433 435
 		return rc;
434
-	if ( ( rc = slam_pull_value ( slam, iobuf,
435
-				      &slam->total_bytes ) ) != 0 )
436
+	if ( ( rc = slam_pull_value ( slam, iobuf, &total_bytes ) ) != 0 )
436 437
 		return rc;
437
-	if ( ( rc = slam_pull_value ( slam, iobuf,
438
-				      &slam->block_size ) ) != 0 )
438
+	if ( ( rc = slam_pull_value ( slam, iobuf, &block_size ) ) != 0 )
439 439
 		return rc;
440 440
 
441
+	/* Sanity check */
442
+	if ( block_size == 0 ) {
443
+		DBGC ( slam, "SLAM %p ignoring zero block size\n", slam );
444
+		return -EINVAL;
445
+	}
446
+
441 447
 	/* Update the cached header */
442 448
 	slam->header_len = ( iobuf->data - header );
443 449
 	assert ( slam->header_len <= sizeof ( slam->header ) );
444 450
 	memcpy ( slam->header, header, slam->header_len );
445 451
 
446 452
 	/* Calculate number of blocks */
447
-	slam->num_blocks = ( ( slam->total_bytes + slam->block_size - 1 ) /
448
-			     slam->block_size );
449
-
453
+	slam->total_bytes = total_bytes;
454
+	slam->block_size = block_size;
455
+	slam->num_blocks = ( ( total_bytes + block_size - 1 ) / block_size );
450 456
 	DBGC ( slam, "SLAM %p has total bytes %ld, block size %ld, num "
451 457
 	       "blocks %ld\n", slam, slam->total_bytes, slam->block_size,
452 458
 	       slam->num_blocks );

Loading…
Cancel
Save