From 3d2cf6c5bd35b0d72716b47bdd7e3892388aafc4 Mon Sep 17 00:00:00 2001 From: PiRK Date: Fri, 29 Jan 2021 11:49:51 +0100 Subject: [PATCH 1/2] initialize variable in tests This was detected while running the tests with the `-Wconditional-uninitialized` flag ``` ./autogen.sh CC=clang CFLAGS="-Wconditional-uninitialized" ./configure make check ``` The resulting warning is a false positive, but setting the value to -1 ensures that the CHECK below will fail if recid is never written to. --- src/tests.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/tests.c b/src/tests.c index c2d5e28924..4110bb9a70 100644 --- a/src/tests.c +++ b/src/tests.c @@ -4324,8 +4324,10 @@ void test_ecdsa_sign_verify(void) { secp256k1_scalar one; secp256k1_scalar msg, key; secp256k1_scalar sigr, sigs; - int recid; int getrec; + /* Initialize recid to suppress a false positive -Wconditional-uninitialized in clang. + VG_UNDEF ensures that valgrind will still treat the variable as uninitialized. */ + int recid = -1; VG_UNDEF(&recid, sizeof(recid)); random_scalar_order_test(&msg); random_scalar_order_test(&key); secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pubj, &key); From 99a1cfec1740a914aa416a87fd0acbde5426b969 Mon Sep 17 00:00:00 2001 From: PiRK Date: Sun, 31 Jan 2021 18:41:35 +0100 Subject: [PATCH 2/2] print warnings for conditional-uninitialized This compiler flag is available for clang but not gcc. Test plan: ``` autogen.sh ./configure make check CC=clang ./configure make check ``` If a variable is used uninitialized, the warning should look something like: ``` CC src/tests-tests.o src/tests.c:4336:15: warning: variable 'recid' may be uninitialized when used here [-Wconditional-uninitialized] CHECK(recid >= 0 && recid < 4); ^~~~~ ./src/util.h:54:18: note: expanded from macro 'CHECK' if (EXPECT(!(cond), 0)) { \ ^~~~ ./src/util.h:41:39: note: expanded from macro 'EXPECT' ^ src/tests.c:4327:14: note: initialize the variable 'recid' to silence this warning int recid; ^ = 0 1 warning generated. ``` --- configure.ac | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/configure.ac b/configure.ac index 451915ccfd..d9a7ddb6a3 100644 --- a/configure.ac +++ b/configure.ac @@ -79,6 +79,15 @@ AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])], CFLAGS="$saved_CFLAGS" ]) +saved_CFLAGS="$CFLAGS" +CFLAGS="-Wconditional-uninitialized $CFLAGS" +AC_MSG_CHECKING([if ${CC} supports -Wconditional-uninitialized]) +AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])], + [ AC_MSG_RESULT([yes]) ], + [ AC_MSG_RESULT([no]) + CFLAGS="$saved_CFLAGS" + ]) + saved_CFLAGS="$CFLAGS" CFLAGS="-fvisibility=hidden $CFLAGS" AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden])