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 <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2021-03-10 15:55:27 +01:00 committed by Carles Cufí
commit 9966d85c0a
2 changed files with 6 additions and 0 deletions

View file

@ -139,6 +139,7 @@ typedef int (*cbprintf_cb)(/* int c, void *ctx */);
* @retval nonegative the number of bytes successfully stored at @p packaged. * @retval nonegative the number of bytes successfully stored at @p packaged.
* This will not exceed @p len. * This will not exceed @p len.
* @retval -EINVAL if @p format is not acceptable * @retval -EINVAL if @p format is not acceptable
* @retval -EFAULT if @p packaged alignment is not acceptable
* @retval -ENOSPC if @p packaged was not null and the space required to store * @retval -ENOSPC if @p packaged was not null and the space required to store
* exceed @p len. * exceed @p len.
*/ */

View file

@ -374,6 +374,11 @@ int cbvprintf_package(void *packaged, size_t len,
/* align destination buffer location */ /* align destination buffer location */
buf = (void *) ROUND_UP(buf, align); buf = (void *) ROUND_UP(buf, align);
/* Check if buffer is properly aligned. */
if ((uintptr_t)buf0 & (align - 1)) {
return -EFAULT;
}
/* make sure the data fits */ /* make sure the data fits */
if (buf0 && buf - buf0 + size > len) { if (buf0 && buf - buf0 + size > len) {
return -ENOSPC; return -ENOSPC;