Skip to content

Commit 71a329f

Browse files
de-nordickartben
authored andcommitted
tests: flash_map: Basic offset/length overflow tests
Test integer overflow on Flash Area operation parameters. All functions call the same is_in_flash_area_bounds function for parameter verification, so it was enough to test parameter checks of flash_read. Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent 3d4b427 commit 71a329f

File tree

1 file changed

+20
-0
lines changed
  • tests/subsys/storage/flash_map/src

1 file changed

+20
-0
lines changed

tests/subsys/storage/flash_map/src/main.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,24 @@ ZTEST(flash_map, test_flash_area_copy)
256256
zassert_mem_equal(src_buf, dst_buf, sizeof(src_buf), "Data mismatch after copy");
257257
}
258258

259+
ZTEST(flash_map, test_parameter_overflows)
260+
{
261+
const struct flash_area *fa;
262+
uint8_t dst_buf[FLASH_AREA_COPY_SIZE];
263+
int rc;
264+
265+
fa = FIXED_PARTITION(SLOT1_PARTITION);
266+
/* -1 cast to size_t gives us max size_t value, added to offset of 1,
267+
* it will overflow to 0.
268+
*/
269+
rc = flash_area_read(fa, 1, dst_buf, (size_t)(-1));
270+
zassert_equal(rc, -EINVAL, "1: Overflow should have been detected");
271+
/* Here we have offset 1 below size of area, with added max size_t
272+
* it upper bound of read range should overflow to:
273+
* (max(size_t) + fa->fa_size - 1) mod (max(size_t)) == fa->fa_size - 2
274+
*/
275+
rc = flash_area_read(fa, fa->fa_size - 1, dst_buf, (size_t)(-1));
276+
zassert_equal(rc, -EINVAL, "2: Overflow should have been detected");
277+
}
278+
259279
ZTEST_SUITE(flash_map, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)