Skip to content

Commit 10180af

Browse files
pabigotnashif
authored andcommitted
lib: cbprintf: avoid referencing distinct union fields in a statement
An assignment from one multi-word union field to another was not safe from corruption. Copy the value out to a local value before storing it to the preferred union field. Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
1 parent 0129dd6 commit 10180af

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib/os/cbprintf_complete.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,7 @@ int cbvprintf(cbprintf_cb out, void *ctx, const char *fp, va_list ap)
13231323
{
13241324
char buf[CONVERTED_BUFLEN];
13251325
size_t count = 0;
1326+
sint_value_type sint;
13261327

13271328
/* Output character, returning EOF if output failed, otherwise
13281329
* updating count.
@@ -1579,11 +1580,16 @@ int cbvprintf(cbprintf_cb out, void *ctx, const char *fp, va_list ap)
15791580
sign = ' ';
15801581
}
15811582

1582-
if (value->sint < 0) {
1583+
/* sint/uint overlay in the union, and so
1584+
* can't appear in read and write operations
1585+
* in the same statement.
1586+
*/
1587+
sint = value->sint;
1588+
if (sint < 0) {
15831589
sign = '-';
1584-
value->uint = (uint_value_type)-value->sint;
1590+
value->uint = (uint_value_type)-sint;
15851591
} else {
1586-
value->uint = (uint_value_type)value->sint;
1592+
value->uint = (uint_value_type)sint;
15871593
}
15881594

15891595
__fallthrough;

0 commit comments

Comments
 (0)