Skip to content

Commit

Permalink
lib: c_lib: add a check logic to verify overflow in fwrite
Browse files Browse the repository at this point in the history
Add a simple logic to validate parameters of fwrite(). If the result
of size times nitems greater than the string length, return zero.

Fixes zephyrproject-rtos#33491.

Signed-off-by: Enjia Mai <enjiax.mai@intel.com>
  • Loading branch information
Enjia Mai committed Apr 23, 2021
1 parent be226ca commit 5111a77
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/libc/minimal/source/stdout/stdout_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ size_t z_impl_zephyr_fwrite(const void *_MLIBC_RESTRICT ptr, size_t size,
return 0;
}

if ((strlen(ptr) + 1) < (size * nitems)) {
return 0;
}

p = ptr;
i = nitems;
do {
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/sprintf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ void test_fwrite(void)
zassert_equal(ret, 0, "fwrite failed!");

ret = fwrite("This 3", 4, 4, stdout);
zassert_not_equal(ret, 0, "fwrite failed!");
zassert_equal(ret, 0, "fwrite failed!");

ret = fwrite("This 3", 4, 4, stdin);
zassert_equal(ret, 0, "fwrite failed!");
Expand Down

0 comments on commit 5111a77

Please sign in to comment.