Handle odd buffer lengths in checksum
Buffers of odd length can be passed to the checksum, for example signatures. csum_bytes uses a uint16_t so change the function to prevent overflowing the buffer, while taking the extra byte into account if the length is odd. Signed-off-by: Linn Crosetto <linn@hpe.com> Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
This commit is contained in:
parent
2c2f71313e
commit
1c9dfe7364
1 changed files with 7 additions and 4 deletions
11
src/image.c
11
src/image.c
|
@ -145,13 +145,16 @@ static uint16_t csum_update_fold(uint16_t csum, uint16_t x)
|
|||
static uint16_t csum_bytes(uint16_t checksum, void *buf, size_t len)
|
||||
{
|
||||
unsigned int i;
|
||||
uint16_t *p;
|
||||
uint16_t *p = buf;
|
||||
|
||||
for (i = 0; i < len; i += sizeof(*p)) {
|
||||
p = buf + i;
|
||||
checksum = csum_update_fold(checksum, *p);
|
||||
for (i = 0; i + sizeof(*p) <= len; i += sizeof(*p)) {
|
||||
checksum = csum_update_fold(checksum, *p++);
|
||||
}
|
||||
|
||||
/* if length is odd, add the remaining byte */
|
||||
if (i < len)
|
||||
checksum = csum_update_fold(checksum, *((uint8_t *)p));
|
||||
|
||||
return checksum;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue