From 3bf54a9e85b2a895ac91fcb46dbbb35259ad314a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Wed, 30 Oct 2024 12:16:20 +0100 Subject: [PATCH] lib: os: cbprintf_packaged: Fix cbprintf_package_convert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When conversion is done in two steps (output length calculation followed by the actual conversion), it was failing when helper array for holding calculated string length from the first conversion run was shorter than number of strings that were supposed to be appended to the package. Fix by taking into account strl_len to determine if length can be taken from the array or need to be calculated again. Signed-off-by: Krzysztof Chruściński --- lib/os/cbprintf_packaged.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/os/cbprintf_packaged.c b/lib/os/cbprintf_packaged.c index 9696016d31d..d5add5ecf37 100644 --- a/lib/os/cbprintf_packaged.c +++ b/lib/os/cbprintf_packaged.c @@ -1148,7 +1148,7 @@ calculate_string_length: for (unsigned int i = 0; i < scpy_cnt; i++) { uint8_t loc = cpy_str_pos[i]; const char *str = *(const char **)&buf32[loc]; - uint16_t str_len = strl ? strl[i] : 0; + uint16_t str_len = (strl && (i < strl_len)) ? strl[i] : 0; rv = cb(&loc, 1, ctx); if (rv < 0) {