david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[slam] Avoid potential division by zero

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2016-01-27 23:27:47 +00:00
parent fef8e34b6f
commit 4ddd3d99c3
1 changed files with 13 additions and 7 deletions

View File

@ -415,6 +415,8 @@ static int slam_pull_value ( struct slam_request *slam,
static int slam_pull_header ( struct slam_request *slam,
struct io_buffer *iobuf ) {
void *header = iobuf->data;
unsigned long total_bytes;
unsigned long block_size;
int rc;
/* If header matches cached header, just pull it and return */
@ -431,22 +433,26 @@ static int slam_pull_header ( struct slam_request *slam,
*/
if ( ( rc = slam_pull_value ( slam, iobuf, NULL ) ) != 0 )
return rc;
if ( ( rc = slam_pull_value ( slam, iobuf,
&slam->total_bytes ) ) != 0 )
if ( ( rc = slam_pull_value ( slam, iobuf, &total_bytes ) ) != 0 )
return rc;
if ( ( rc = slam_pull_value ( slam, iobuf,
&slam->block_size ) ) != 0 )
if ( ( rc = slam_pull_value ( slam, iobuf, &block_size ) ) != 0 )
return rc;
/* Sanity check */
if ( block_size == 0 ) {
DBGC ( slam, "SLAM %p ignoring zero block size\n", slam );
return -EINVAL;
}
/* Update the cached header */
slam->header_len = ( iobuf->data - header );
assert ( slam->header_len <= sizeof ( slam->header ) );
memcpy ( slam->header, header, slam->header_len );
/* Calculate number of blocks */
slam->num_blocks = ( ( slam->total_bytes + slam->block_size - 1 ) /
slam->block_size );
slam->total_bytes = total_bytes;
slam->block_size = block_size;
slam->num_blocks = ( ( total_bytes + block_size - 1 ) / block_size );
DBGC ( slam, "SLAM %p has total bytes %ld, block size %ld, num "
"blocks %ld\n", slam, slam->total_bytes, slam->block_size,
slam->num_blocks );