[v3] elf: fixes compile error when both enable -Werror and -DNDEBUG

Message ID 20220402095152.3063-1-yangyanchao6@huawei.com
State Superseded
Headers
Series [v3] elf: fixes compile error when both enable -Werror and -DNDEBUG |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Yang Yanchao April 2, 2022, 9:51 a.m. UTC
  Use -Werror and -DNDEBUG at the same time will
causes the following compilation errors:

cache.c: In function 'save_cache':
cache.c:758:15: error: unused variable 'old_offset' [-Werror=unused-variable]
  758 |       off64_t old_offset = lseek64 (fd, extension_offset, SEEK_SET);
      |               ^~~~~~~~~~

-DNDEBUG will disables the assertion.
Therefore, only the variables used by assertions do not take effect.
Fooling the Compiler with Type Conversions.
---
 elf/cache.c | 1 +
 1 file changed, 1 insertion(+)
  

Comments

Carlos O'Donell April 11, 2022, 2:29 p.m. UTC | #1
On 4/2/22 05:51, Yang Yanchao via Libc-alpha wrote:
> Use -Werror and -DNDEBUG at the same time will
> causes the following compilation errors:

Thank you for following up with a v3. A little more work and I think
this will be ready. Thank you for testing with -Werror and -DNDEBUG.
This is a configuration we used to test in Fedora, but have since
stopped using -DNDEBUG to keep the asserts in place. I need to consider
adding such an option to build-many-glibcs testing.

> cache.c: In function 'save_cache':
> cache.c:758:15: error: unused variable 'old_offset' [-Werror=unused-variable]
>   758 |       off64_t old_offset = lseek64 (fd, extension_offset, SEEK_SET);
>       |               ^~~~~~~~~~
> 
> -DNDEBUG will disables the assertion.
> Therefore, only the variables used by assertions do not take effect.
> Fooling the Compiler with Type Conversions.
> ---
>  elf/cache.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/elf/cache.c b/elf/cache.c
> index dbf4c83a7a..68cd4d0828 100644
> --- a/elf/cache.c
> +++ b/elf/cache.c
> @@ -754,6 +754,7 @@ save_cache (const char *cache_name)
>      {
>        /* Align file position to 4.  */
>        off64_t old_offset = lseek64 (fd, extension_offset, SEEK_SET);
> +      (void) old_offset;

The compiler unused-variable warnings are very aggressive, and this may not fix all issues.

In cases like these we try to improve the situation by using specific pragmas.

e.g.

/* With GCC X.Y when compiling with -Werror, -DNDEBUG, and optimizations, the
   compiler may be able to eliminate the use of extension_offset and so we
   get an unused variable warning for extension_offset.  We want to continue to
   support the assert, so we disable this warning for this code.  */
DIAG_PUSH_NEEDS_COMMENT;
DIAG_IGNORE_NEEDS_COMMENT (X, "-Werror=unused-variable")
assert ((unsigned long long int) (extension_offset - old_offset) < 4);
DIAG_POP_NEEDS_COMMENT;

This is just an example, with X being the major version of gcc, and y being the minor.

You'll need to test this out, and double check CI/CD passes:
https://patchwork.sourceware.org/project/glibc/list/
You can view your posts there with success/warn/fail results.

>        assert ((unsigned long long int) (extension_offset - old_offset) < 4);
>        write_extensions (fd, str_offset, extension_offset);
>      }

Looking forward to a v4.
  

Patch

diff --git a/elf/cache.c b/elf/cache.c
index dbf4c83a7a..68cd4d0828 100644
--- a/elf/cache.c
+++ b/elf/cache.c
@@ -754,6 +754,7 @@  save_cache (const char *cache_name)
     {
       /* Align file position to 4.  */
       off64_t old_offset = lseek64 (fd, extension_offset, SEEK_SET);
+      (void) old_offset;
       assert ((unsigned long long int) (extension_offset - old_offset) < 4);
       write_extensions (fd, str_offset, extension_offset);
     }