]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
test/bioprinttest.c: fix UB in %n result handling
authorEugene Syromiatnikov <esyr@openssl.org>
Mon, 1 Sep 2025 11:26:44 +0000 (13:26 +0200)
committerNeil Horman <nhorman@openssl.org>
Wed, 3 Sep 2025 11:47:33 +0000 (07:47 -0400)
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 <esyr@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/28405)

test/bioprinttest.c

index f9bd916db56efba55fd88a6d44fcfb15acb7c5cc..a2e107b62df3195690bb5587ec52aff0ddd4857c 100644 (file)
@@ -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)