Skip to content

Commit 06360e3

Browse files
committed
lib: os: cbprintf: Add alignment check to cbprintf_package
Added validation of alignment to cbprintf_package. Error is returned if input buffer is not aligned to the largest argument. Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent 3b5d405 commit 06360e3

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

include/sys/cbprintf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ typedef int (*cbprintf_cb)(/* int c, void *ctx */);
139139
* @retval nonegative the number of bytes successfully stored at @p packaged.
140140
* This will not exceed @p len.
141141
* @retval -EINVAL if @p format is not acceptable
142+
* @retval -EFAULT if @p packaged alignment is not acceptable
142143
* @retval -ENOSPC if @p packaged was not null and the space required to store
143144
* exceed @p len.
144145
*/

lib/os/cbprintf_packaged.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ int cbvprintf_package(void *packaged, size_t len,
374374
/* align destination buffer location */
375375
buf = (void *) ROUND_UP(buf, align);
376376

377+
/* Check if buffer is properly aligned. */
378+
if ((uintptr_t)buf0 & (align - 1)) {
379+
return -EFAULT;
380+
}
381+
377382
/* make sure the data fits */
378383
if (buf0 && buf - buf0 + size > len) {
379384
return -ENOSPC;

0 commit comments

Comments
 (0)