[v4] 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
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.
use DIAG_IGNORE_NEEDS_COMMENT to disable this warning.
---
elf/cache.c | 6 ++++++
1 file changed, 6 insertions(+)
Comments
* Yang Yanchao:
> 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.
> use DIAG_IGNORE_NEEDS_COMMENT to disable this warning.
> ---
> elf/cache.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/elf/cache.c b/elf/cache.c
> index dbf4c83a7a..1e5427555b 100644
> --- a/elf/cache.c
> +++ b/elf/cache.c
> @@ -36,6 +36,7 @@
> #include <dl-cache.h>
> #include <version.h>
> #include <stringtable.h>
> +#include <libc-diag.h>
>
> /* Used to store library names, paths, and other strings. */
> static struct stringtable strings;
> @@ -751,6 +752,10 @@ save_cache (const char *cache_name)
> != (ssize_t) strings_finalized.size)
> error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
>
> + /* Building with -DNDEBUG, assert will be ignored,
> + so that old_offset is not used */
> + DIAG_PUSH_NEEDS_COMMENT
> + DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wunused-variable");
> if (opt_format != opt_format_old)
> {
> /* Align file position to 4. */
> @@ -758,6 +763,7 @@ save_cache (const char *cache_name)
> assert ((unsigned long long int) (extension_offset - old_offset) < 4);
> write_extensions (fd, str_offset, extension_offset);
> }
> + DIAG_POP_NEEDS_COMMENT
>
> /* Make sure user can always read cache file */
> if (chmod (temp_name, S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR))
Personally I would prefer __attribute__ ((unused)) directly on the
old_offset declaration. I think it's clearer.
Not sure what others think.
Thanks,
Florian
On Apr 13 2022, Florian Weimer via Libc-alpha wrote:
> Personally I would prefer __attribute__ ((unused)) directly on the
> old_offset declaration. I think it's clearer.
Perhaps even wrapping it inside #ifndef NDEBUG, like this:
#ifndef NDEBUG
off64_t old_offset =
#endif
lseek64 (fd, extension_offset, SEEK_SET);
@@ -36,6 +36,7 @@
#include <dl-cache.h>
#include <version.h>
#include <stringtable.h>
+#include <libc-diag.h>
/* Used to store library names, paths, and other strings. */
static struct stringtable strings;
@@ -751,6 +752,10 @@ save_cache (const char *cache_name)
!= (ssize_t) strings_finalized.size)
error (EXIT_FAILURE, errno, _("Writing of cache data failed"));
+ /* Building with -DNDEBUG, assert will be ignored,
+ so that old_offset is not used */
+ DIAG_PUSH_NEEDS_COMMENT
+ DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wunused-variable");
if (opt_format != opt_format_old)
{
/* Align file position to 4. */
@@ -758,6 +763,7 @@ save_cache (const char *cache_name)
assert ((unsigned long long int) (extension_offset - old_offset) < 4);
write_extensions (fd, str_offset, extension_offset);
}
+ DIAG_POP_NEEDS_COMMENT
/* Make sure user can always read cache file */
if (chmod (temp_name, S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR))