Skip to content

Commit

Permalink
[slam] Avoid potential division by zero
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Jan 27, 2016
1 parent fef8e34 commit 4ddd3d9
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/net/udp/slam.c
Expand Up @@ -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 */
Expand All @@ -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 );
Expand Down

0 comments on commit 4ddd3d9

Please sign in to comment.