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

Message ID 20220402032304.2959-1-yangyanchao6@huawei.com
State Superseded
Headers
Series 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, 3:23 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.
Integrate variable assignment and assertion to make compilation pass.

Signed-off-by: Yang Yanchao <yangyanchao6@huawei.com>
---
 elf/cache.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Andreas Schwab April 2, 2022, 7:08 a.m. UTC | #1
On Apr 02 2022, Yang Yanchao via Libc-alpha wrote:

> diff --git a/elf/cache.c b/elf/cache.c
> index dbf4c83a7a..68cd4d0828 100644
> --- a/elf/cache.c
> +++ b/elf/cache.c
> @@ -754,8 +754,8 @@ save_cache (const char *cache_name)
>    if (opt_format != opt_format_old)
>      {
>        /* Align file position to 4.  */
> -      off64_t old_offset = lseek64 (fd, extension_offset, SEEK_SET);
> -      assert ((unsigned long long int) (extension_offset - old_offset) < 4);
> +      assert ((unsigned long long int) (extension_offset - 
> +					lseek64 (fd, extension_offset, SEEK_SET)) < 4);

This will fail to perform the side effect with -DNDEBUG.
  

Patch

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