summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVadim Bendebury <vbendeb@chromium.org>2021-08-25 17:06:54 -0700
committerCommit Bot <commit-bot@chromium.org>2021-08-26 18:06:07 +0000
commit51ca8532a387ce70564beb5445d43422a637f04a (patch)
tree138519ff2c5e033520ae765c78429763f80957ef
parent7cdd6a24aad8fcf33c1527ebdbe5a10547258b41 (diff)
downloadvboot-51ca8532a387ce70564beb5445d43422a637f04a.tar.gz
tests: fix compilation failures
Attempts to access array of fixed size beyond the boundary now trigger compilation warnings, even though there is room beyond the array boundary in due to data layout. Let's modify the code to declare the array size explicitly to calm the compiler. Also needed to add en explicit return value in a function where all returns happened inside a case statement. BRANCH=none BUG=none TEST='make run2tests' does not fail to compile any more and succeeds running tests. Change-Id: Ib2158145233a0e8641c5b9b95499928f18390a8d Signed-off-by: Vadim Bendebury <vbendeb@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/3120000 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
-rw-r--r--tests/vb2_common2_tests.c1
-rw-r--r--tests/vb2_sha256_x86_tests.c29
-rw-r--r--tests/vb2_sha_tests.c24
3 files changed, 34 insertions, 20 deletions
diff --git a/tests/vb2_common2_tests.c b/tests/vb2_common2_tests.c
index 3f062892..5f065f8f 100644
--- a/tests/vb2_common2_tests.c
+++ b/tests/vb2_common2_tests.c
@@ -41,6 +41,7 @@ static vb2_error_t hwcrypto_mock(enum hwcrypto_state *state)
/* shouldn't reach here but added for compiler */
return VB2_ERROR_MOCK;
}
+ return VB2_ERROR_MOCK;
}
vb2_error_t vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg,
diff --git a/tests/vb2_sha256_x86_tests.c b/tests/vb2_sha256_x86_tests.c
index 98be77ef..ca934e47 100644
--- a/tests/vb2_sha256_x86_tests.c
+++ b/tests/vb2_sha256_x86_tests.c
@@ -71,22 +71,29 @@ static void sha256_tests(void)
static void known_value_tests(void)
{
const char sentinel[] = "keepme";
- struct {
+ union {
struct vb2_hash hash;
- uint8_t overflow[8];
+ char overflow[sizeof(struct vb2_hash) + 8];
} test;
#define TEST_KNOWN_VALUE(algo, str, value) \
TEST_EQ(vb2_digest_size(algo), sizeof(value) - 1, \
- "Known hash size " #algo ": " #str); \
- strcpy((char *)&test.hash.raw[sizeof(value) - 1], sentinel); \
- TEST_SUCC(vb2_digest_buffer((const uint8_t *)str, sizeof(str) - 1, \
- algo, test.hash.raw, vb2_digest_size(algo)), \
- "Calculate known hash " #algo ": " #str); \
- TEST_EQ(memcmp(test.hash.raw, value, sizeof(value) - 1), 0, \
- "Known hash " #algo ": " #str); \
- TEST_EQ(strcmp((char *)&test.hash.raw[sizeof(value) - 1], sentinel), 0,\
- "Overflow known hash " #algo ": " #str);
+ "Known hash size " #algo ": " #str); \
+ { \
+ char *sent_base = test.overflow + \
+ offsetof(struct vb2_hash, raw) + sizeof(value) - 1; \
+ strcpy(sent_base, sentinel); \
+ strcpy(sent_base, sentinel); \
+ TEST_SUCC(vb2_digest_buffer((const uint8_t *)str, \
+ sizeof(str) - 1, \
+ algo, test.hash.raw, \
+ vb2_digest_size(algo)), \
+ "Calculate known hash " #algo ": " #str); \
+ TEST_EQ(memcmp(test.hash.raw, value, sizeof(value) - 1), 0, \
+ "Known hash " #algo ": " #str); \
+ TEST_EQ(strcmp(sent_base, sentinel), 0, \
+ "Overflow known hash " #algo ": " #str); \
+ }
TEST_KNOWN_VALUE(VB2_HASH_SHA256, "",
"\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
diff --git a/tests/vb2_sha_tests.c b/tests/vb2_sha_tests.c
index b4a4bd06..a14af65c 100644
--- a/tests/vb2_sha_tests.c
+++ b/tests/vb2_sha_tests.c
@@ -172,21 +172,27 @@ static void misc_tests(void)
static void known_value_tests(void)
{
const char sentinel[] = "keepme";
- struct {
+ union {
struct vb2_hash hash;
- uint8_t overflow[8];
+ char overflow[sizeof(struct vb2_hash) + 8];
} test;
+
#define TEST_KNOWN_VALUE(algo, str, value) \
TEST_EQ(vb2_digest_size(algo), sizeof(value) - 1, \
"Known hash size " #algo ": " #str); \
- strcpy((char *)&test.hash.raw[sizeof(value) - 1], sentinel); \
- TEST_SUCC(vb2_hash_calculate(str, sizeof(str) - 1, algo, &test.hash), \
- "Calculate known hash " #algo ": " #str); \
- TEST_EQ(memcmp(test.hash.raw, value, sizeof(value) - 1), 0, \
- "Known hash " #algo ": " #str); \
- TEST_EQ(strcmp((char *)&test.hash.raw[sizeof(value) - 1], sentinel), 0,\
- "Overflow known hash " #algo ": " #str);
+ { \
+ char *sent_base = test.overflow + \
+ offsetof(struct vb2_hash, raw) + sizeof(value) - 1; \
+ strcpy(sent_base, sentinel); \
+ TEST_SUCC(vb2_hash_calculate(str, sizeof(str) - 1, \
+ algo, &test.hash), \
+ "Calculate known hash " #algo ": " #str); \
+ TEST_EQ(memcmp(test.hash.raw, value, sizeof(value) - 1), 0, \
+ "Known hash " #algo ": " #str); \
+ TEST_EQ(strcmp(sent_base, sentinel), 0, \
+ "Overflow known hash " #algo ": " #str); \
+ }
TEST_KNOWN_VALUE(VB2_HASH_SHA1, "",
"\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"