Skip to contentSkip to conversation

fix clang warning about missing braces

PATCH In review Lev Stipakov 4 participants 12 messages Nov 28, 2019 – Nov 29, 2019 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix clang warning about missing braces

From: Lev Stipakov <[email protected]...>

A struct with subobjects should be initialized
with double braces.

---
 src/openvpn/crypto.c         | 2 +-
 src/openvpn/crypto_mbedtls.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
src/openvpn/crypto.c +1 -1
index 65e789ed..731f5bad 100644
@@ -1857,7 +1857,7 @@ void
 write_pem_key_file(const char *filename, const char *pem_name)
 {
     struct gc_arena gc = gc_new();
-    struct key server_key = { 0 };
+    struct key server_key = {{ 0 }};
     struct buffer server_key_buf = clear_buf();
     struct buffer server_key_pem = clear_buf();
 
src/openvpn/crypto_mbedtls.c +1 -1
index 3e77fa9e..57ad85ad 100644
@@ -304,7 +304,7 @@ mbedtls_ctr_drbg_context *
 rand_ctx_get(void)
 {
     static mbedtls_entropy_context ec = {0};
-    static mbedtls_ctr_drbg_context cd_ctx = {0};
+    static mbedtls_ctr_drbg_context cd_ctx = {{0}};
     static bool rand_initialised = false;
 
     if (!rand_initialised)
Signature · 1 line
2.17.1

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#2 lore raw

Hi

Quoted 9 lines · Steffan Karger
Quoted 2 lines · Lev Stipakov

A struct with subobjects should be initialized
with double braces.

This is not true. {0} is a valid initializer for structs in C. Both clang and gcc used to have a bug where they incorrectly warned about this. GCC fixed this a while ago[0]. I thought clang had fixed it too recently, but apparently not?

Pretty much same as my thoughts.

I think the correct fix here is to remove -Werror from travis build. I have tried and failed to lobby for this earlier, but one more try can't hurt, I suppose :)

That said, it seems clang has fixed this some time after clang-7. I don't get this warning anymore after upgrading to clang-9.

Selva

#3 lore raw

hi,

Quoted 3 lines · Selva Nair

I think the correct fix here is to remove -Werror from travis build. I have tried and failed to lobby for this earlier, but one more try can't hurt, I suppose :)

Can we restrict -Werror to gcc builds, for the time being?

If not, I agree with you, and it needs to go...

Valid (and unambiguous) C statements shouldn't cause warnings, and we shouldn't add patches just because an old version of a certain compiler is silly.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#4 lore raw

PATCH 1/1 travis: bump clang version

From: Lev Stipakov <[email protected]...>

This should fix "suggest braces around initialization of subobject" warning.

---
 .travis.yml | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
.travis.yml +11 -6
index 61be17cd..40296d87 100644
@@ -46,16 +46,16 @@ matrix:
     - env: SSLLIB="openssl" EXTRA_CONFIG="--enable-iproute2"
       os: linux
       compiler: gcc
-    - env: SSLLIB="openssl" CFLAGS="-fsanitize=address"
+    - env: SSLLIB="openssl" CFLAGS="-fsanitize=address" CC=clang-9
       os: linux
       compiler: clang
-    - env: SSLLIB="openssl" OPENSSL_VERSION="1.1.1c"
+    - env: SSLLIB="openssl" OPENSSL_VERSION="1.1.1c" CC=clang-9
       os: linux
       compiler: clang
     - env: SSLLIB="mbedtls"
       os: linux
       compiler: gcc
-    - env: SSLLIB="mbedtls" CFLAGS="-fsanitize=address"
+    - env: SSLLIB="mbedtls" CFLAGS="-fsanitize=address" CC=clang-9
       os: linux
       compiler: clang
     - env: SSLLIB="openssl"
@@ -73,17 +73,21 @@ matrix:
     - env: SSLLIB="openssl" CHOST=i686-w64-mingw32
       os: linux
       compiler: ": Win32 build only"
-    - env: SSLLIB="openssl" EXTRA_CONFIG="--disable-lzo" EXTRA_SCRIPT="make distcheck"
+    - env: SSLLIB="openssl" EXTRA_CONFIG="--disable-lzo" EXTRA_SCRIPT="make distcheck" CC=clang-9
       os: linux
       compiler: clang
-    - env: SSLLIB="openssl" EXTRA_CONFIG="--enable-small"
+    - env: SSLLIB="openssl" EXTRA_CONFIG="--enable-small" CC=clang-9
       os: linux
       compiler: clang
 
 addons:
   apt:
+    sources:
+      - ubuntu-toolchain-r-test
+      - sourceline: 'deb https://apt.llvm.org/xenial/ llvm-toolchain-xenial-9 main'
+        key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
     update: true
-    packages: [ liblzo2-dev, libpam0g-dev, liblz4-dev, linux-libc-dev, man2html, mingw-w64]
+    packages: [ liblzo2-dev, libpam0g-dev, liblz4-dev, linux-libc-dev, man2html, mingw-w64 ]
   homebrew:
     update: true
     packages: [ lzo ]
@@ -95,6 +99,7 @@ cache:
   - ${HOME}/Library/Caches/Homebrew
 
 install:
+  - if [ "$TRAVIS_OS_NAME" = "linux" ] && [ "$TRAVIS_CPU_ARCH" != "ppc64le" ]; then sudo apt-get install clang-9; fi
   - if [ ! -z "${CHOST}" ]; then unset CC; fi
   - .travis/run-build-deps.sh
 
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#5 lore raw

[Openvpn-devel] [PATCH applied] Re: travis: bump clang version

This looks reasonable - we want to see warnings-as-errors with recent compilers. Older compilers may warn all they want, but we can not consider it an *error* or spend time on fixing them (in ugly ways).

Your patch has been applied to the master branch.

commit 4a2c8dab5dffff7a09d794d9b2afc4bf710db2b9

Author: Lev Stipakov
Date:   Fri Nov 29 12:43:35 2019 +0200
     travis: bump clang version

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg19203.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#6 lore raw
In reply to Gert Doering
Quoted 7 lines · Gert Doering

hi,

Quoted 3 lines · Selva Nair

I think the correct fix here is to remove -Werror from travis build. I have tried and failed to lobby for this earlier, but one more try can't hurt, I suppose :)

Quoted 7 lines

Can we restrict -Werror to gcc builds, for the time being?

If not, I agree with you, and it needs to go...

Valid (and unambiguous) C statements shouldn't cause warnings, and we shouldn't add patches just because an old version of a certain compiler is silly.

With GCC-4.3.8, I see this warning:

crypto.c: In function ‘write_pem_key_file’:
crypto.c:1860:12: warning: missing braces around initializer [-Wmissing-braces]

     struct key server_key = { 0 };

Perhaps we should just add -Wno-missing-braces? A quick patch is attached, this silences this warning with GCC-4.3.8 at least. That said, I'm not sure this is the best approach; it may hide other missing braces warnings we should see.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc

From 1166081219e8623e32aeea93ddabd2937df36ef8 Mon Sep 17 00:00:00 2001
From: David Sommerseth <[email protected]...>
Date: Fri, 29 Nov 2019 11:43:34 +0100
Subject: [PATCH] build: Add -Wno-missing-braces to CFLAGS
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Some older GCC and clang compilers complains about missing braces when initaling structs.

crypto.c: In function ‘write_pem_key_file’:
crypto.c:1860:12: warning: missing braces around initializer [-Wmissing-braces]

     struct key server_key = { 0 };

Use of the ACL_CHECK_ADD_COMPILE_FLAGS() macro to check if the compiler support -Wno-missing-braces flag, which ends up in CFLAGS if the compiler accepts it.

---
 configure.ac | 1 +
 1 file changed, 1 insertion(+)
configure.ac +1 -0
index a47e0a06..0e98dfaa 100644
@@ -1289,6 +1289,7 @@ AC_DEFUN([ACL_CHECK_ADD_COMPILE_FLAGS], [
 ACL_CHECK_ADD_COMPILE_FLAGS([-Wno-stringop-truncation])
 ACL_CHECK_ADD_COMPILE_FLAGS([-Wno-unused-function])
 ACL_CHECK_ADD_COMPILE_FLAGS([-Wno-unused-parameter])
+ACL_CHECK_ADD_COMPILE_FLAGS([-Wno-missing-braces])
 ACL_CHECK_ADD_COMPILE_FLAGS([-Wall])
 
 if test "${enable_pedantic}" = "yes"; then
Signature · 1 line
2.17.1
#7 lore raw

Hi,

Quoted 1 line · David Sommerseth

With GCC-4.3.8, I see this warning:

This is about as old as you :-) - do we care about suppressing warnings in old gcc versions that might suppress a *relevant* warning when compiling with a current gcc version?

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#8 lore raw
Quoted 8 lines · Gert Doering

Hi,

Quoted 1 line · David Sommerseth

With GCC-4.3.8, I see this warning:

This is about as old as you :-) - do we care about suppressing warnings in old gcc versions that might suppress a *relevant* warning when compiling with a current gcc version?

This is the default compiler on stock RHEL-7 ... which goes EOL August 2024.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#9 lore raw

Hi,

Quoted 9 lines · David Sommerseth
Quoted 6 lines · Gert Doering
Quoted 1 line · David Sommerseth

With GCC-4.3.8, I see this warning:

This is about as old as you :-) - do we care about suppressing warnings in old gcc versions that might suppress a *relevant* warning when compiling with a current gcc version?

This is the default compiler on stock RHEL-7 ... which goes EOL August 2024.

We're not going to break it.

I'm just not going to care very much about the warnings it might throw (and I might object to any patches that are "just for the benefit of suppressing warnings from gcc-4").

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#10 lore raw
Quoted 11 lines · Gert Doering

Hi,

Quoted 8 lines · David Sommerseth
Quoted 6 lines · Gert Doering
Quoted 1 line · David Sommerseth

With GCC-4.3.8, I see this warning:

This is about as old as you :-) - do we care about suppressing warnings in old gcc versions that might suppress a *relevant* warning when compiling with a current gcc version?

Quoted 7 lines
Quoted 1 line

This is the default compiler on stock RHEL-7 ... which goes EOL August 2024.

We're not going to break it.

I'm just not going to care very much about the warnings it might throw (and I might object to any patches that are "just for the benefit of suppressing warnings from gcc-4").

Fair enough. But that will actually restrain us from adding -Werror by default for some time forward, unless we want to make package maintenance a bit more tricky - needing to revert a -Werror chage or add -Wno-missing-braces. If we have status quo for the moment, I can live with that until RHEL-7 is EOL.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#11 lore raw

Hi,

Quoted 9 lines · David Sommerseth
Quoted 3 lines

I'm just not going to care very much about the warnings it might throw (and I might object to any patches that are "just for the benefit of suppressing warnings from gcc-4").

Fair enough. But that will actually restrain us from adding -Werror by default for some time forward, unless we want to make package maintenance a bit more tricky - needing to revert a -Werror chage or add -Wno-missing-braces. If we have status quo for the moment, I can live with that until RHEL-7 is EOL.

I don't think we can add -Werror as "on by default" for any platform in the foreseeable future - we'll always hit "old" or "too new" compilers that cause build breakage and lots of extra effort to work around it.

Now, well-defined environments ("travis, clang-9, Linux") I'm all for it :)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#12 lore raw
In reply to Gert Doering

I upgraded clang to 9.0 and warning disappeared.

https://travis-ci.org/lstipakov/openvpn/builds/618527918

Patch is on the list.

to 28. marrask. 2019 klo 21.44 Gert Doering ([email protected]...) kirjoitti:

Quoted 32 lines

hi,

Quoted 3 lines · Selva Nair

I think the correct fix here is to remove -Werror from travis build. I have tried and failed to lobby for this earlier, but one more try can't hurt, I suppose :)

Can we restrict -Werror to gcc builds, for the time being?

If not, I agree with you, and it needs to go...

Valid (and unambiguous) C statements shouldn't cause warnings, and we shouldn't add patches just because an old version of a certain compiler is silly.

gert

--
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never
doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh
Mistress

Gert Doering - Munich, Germany
[email protected]...
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Signature · 1 line
-Lev