From: Ben Pfaff Date: Tue, 16 Nov 2010 20:52:10 +0000 (-0800) Subject: csum: Avoid misaligned data access. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=80642190644ae6fbcf485924b1c59e67d0be8a87;p=openvswitch csum: Avoid misaligned data access. This should fix a checksum test failure observed on Alpha in Debian's buildds. --- diff --git a/lib/csum.c b/lib/csum.c index 922f6862..33ec28b6 100644 --- a/lib/csum.c +++ b/lib/csum.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009 Nicira Networks. + * Copyright (c) 2008, 2009, 2010 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ #include #include "csum.h" +#include "unaligned.h" /* Returns the IP checksum of the 'n' bytes in 'data'. * @@ -57,8 +58,8 @@ csum_continue(uint32_t partial, const void *data_, size_t n) { const uint16_t *data = data_; - for (; n > 1; n -= 2) { - partial = csum_add16(partial, *data++); + for (; n > 1; n -= 2, data++) { + partial = csum_add16(partial, get_unaligned_u16(data)); } if (n) { partial += *(uint8_t *) data; diff --git a/tests/test-csum.c b/tests/test-csum.c index fd16dd3f..af6655e7 100644 --- a/tests/test-csum.c +++ b/tests/test-csum.c @@ -22,6 +22,7 @@ #include #include #include "random.h" +#include "unaligned.h" #include "util.h" #undef NDEBUG @@ -149,7 +150,7 @@ main(void) /* Test csum_add16(). */ partial = 0; for (i = 0; i < tc->size / 2; i++) { - partial = csum_add16(partial, data16[i]); + partial = csum_add16(partial, get_unaligned_u16(&data16[i])); } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); @@ -157,7 +158,7 @@ main(void) /* Test csum_add32(). */ partial = 0; for (i = 0; i < tc->size / 4; i++) { - partial = csum_add32(partial, data32[i]); + partial = csum_add32(partial, get_unaligned_u32(&data32[i])); } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); @@ -166,10 +167,12 @@ main(void) partial = 0; for (i = 0; i < tc->size / 4; i++) { if (i % 2) { - partial = csum_add32(partial, data32[i]); + partial = csum_add32(partial, get_unaligned_u32(&data32[i])); } else { - partial = csum_add16(partial, data16[i * 2]); - partial = csum_add16(partial, data16[i * 2 + 1]); + uint16_t u0 = get_unaligned_u16(&data16[i * 2]); + uint16_t u1 = get_unaligned_u16(&data16[i * 2 + 1]); + partial = csum_add16(partial, u0); + partial = csum_add16(partial, u1); } } assert(ntohs(csum_finish(partial)) == tc->csum);