From: Eugene Syromiatnikov Date: Mon, 1 Sep 2025 11:26:44 +0000 (+0200) Subject: test/bioprinttest.c: fix UB in %n result handling X-Git-Tag: 4.0-PRE-CLANG-FORMAT-WEBKIT~581 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1cdea4907954b61e301f534c59420eeb3e378dc;p=thirdparty%2Fopenssl.git test/bioprinttest.c: fix UB in %n result handling Per paragraph 3 of section 6.5.16.1 "Simple assignment" of ISO 9899:1999 TC3: If the value being stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact and the two objects shall have qualified or unqualified versions of a compatible type; otherwise, the behavior is undefined. And that is what exactly violated when one field of the union is assigned to another; avoid that by introducing separate local variable where the cast value is stored and then compared. Fixes: 9deaf8383338 "test/bioprinttest.c: add some checks for integer and string printing" Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665144 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665145 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665146 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665147 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665148 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665150 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665152 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665153 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665156 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665157 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665158 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665159 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665160 Resolves: https://scan5.scan.coverity.com/#/project-view/65248/10222?selectedIssue=1665162 References: https://github.com/openssl/project/issues/1362 Signed-off-by: Eugene Syromiatnikov Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale Reviewed-by: Neil Horman (Merged from https://github.com/openssl/openssl/pull/28405) --- diff --git a/test/bioprinttest.c b/test/bioprinttest.c index f9bd916db56..a2e107b62df 100644 --- a/test/bioprinttest.c +++ b/test/bioprinttest.c @@ -541,6 +541,7 @@ static int test_n(int i) ossl_ssize_t z; ptrdiff_t t; } n = { 0 }, std_n = { 0 }; + uint64_t n_val, std_n_val; #if defined(OPENSSL_SYS_WINDOWS) /* @@ -594,8 +595,8 @@ static int test_n(int i) std_ret = snprintf(std_buf, sizeof(std_buf), data->format, \ data->arg1.i, data->arg2.i, &std_n.field_); \ } \ - n.val = n.field_; \ - std_n.val = std_n.field_; \ + n_val = n.field_; \ + std_n_val = std_n.field_; \ } while (0) case AT_CHAR: DO_PRINT(hh); @@ -625,7 +626,7 @@ static int test_n(int i) } if (!TEST_str_eq(bio_buf, data->expected) - + !TEST_uint64_t_eq(n.val, data->exp_n) + + !TEST_uint64_t_eq(n_val, data->exp_n) + !TEST_int_eq(bio_ret, exp_ret)) { TEST_note("Format: \"%s\"", data->format); return 0; @@ -637,7 +638,7 @@ static int test_n(int i) */ if (!data->skip_libc_check) { if (!TEST_str_eq(bio_buf, std_buf) - + !TEST_uint64_t_eq(n.val, std_n.val) + + !TEST_uint64_t_eq(n_val, std_n_val) + !(data->skip_libc_ret_check || TEST_int_eq(bio_ret, std_ret))) { TEST_note("Format: \"%s\"", data->format); #if defined(OPENSSL_STRICT_LIBC_PRINTF_CHECK)