Skip to contentSkip to conversation

ext4: avoid unused variable warning

PATCH In review Arnd Bergmann 3 participants 4 messages Oct 10, 2018 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: avoid unused variable warning

The two new variables are only used in an #ifdef, so they cause a warning without CONFIG_QUOTA:

fs/ext4/super.c: In function 'parse_options':
fs/ext4/super.c:1977:26: error: unused variable 'grp_qf_name' [-Werror=unused-variable]

  char *p, *usr_qf_name, *grp_qf_name;
                          ^~~~~~~~~~~

fs/ext4/super.c:1977:12: error: unused variable 'usr_qf_name' [-Werror=unused-variable]
char *p, *usr_qf_name, *grp_qf_name;

Fixes 20cefcdc2040 ("ext4: fix use-after-free race in ext4_remount()'s error path")
---
 fs/ext4/super.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/ext4/super.c +4 -1
index e9ca8312457b..d60199510c94 100644
@@ -1974,7 +1974,10 @@ static int parse_options(char *options, struct super_block *sb,
 			 int is_remount)
 {
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
-	char *p, *usr_qf_name, *grp_qf_name;
+	char *p;
+#ifdef CONFIG_QUOTA
+	char *usr_qf_name, *grp_qf_name;
+#endif
 	substring_t args[MAX_OPT_ARGS];
 	int token;
 
Signature · 1 line
2.18.0
maintainer
#1 lore raw
Quoted 5 lines · Arnd Bergmann

The two new variables are only used in an #ifdef, so they cause a warning without CONFIG_QUOTA:

fs/ext4/super.c: In function 'parse_options':
fs/ext4/super.c:1977:26: error: unused variable 'grp_qf_name' [-Werror=unused-variable]

Quoted 7 lines
  char *p, *usr_qf_name, *grp_qf_name;
                          ^~~~~~~~~~~

fs/ext4/super.c:1977:12: error: unused variable 'usr_qf_name' [-Werror=unused-variable]
char *p, *usr_qf_name, *grp_qf_name;

Fixes 20cefcdc2040 ("ext4: fix use-after-free race in ext4_remount()'s error path")

Hmm, I wonder if we should do something like:

#define EXT4_UNUSED_VAR __attribute__ ((unused))

and then we could do:

	char *p, *usr_qf_name EXT4_UNUSED_VAR, *grp_qf_name EXT4_UNUSED_VAR;

More generally, I wonder if this is something we should have defined for the whole kernel, as opposed to a one-off hack that ACPI and ext4 subsystems use. It's a little ugly, but I think it's much nicer than having extra #ifdefs such as:

	char *p;
#ifdef CONFIG_QUOTA
	char *usr_qf_name, *grp_qf_name;

#endif

After all, the compiler is perfectly capable of ignoring variables which are unused. And if it's only because of an #ifdef later in the function, it would be nice to not have an extra #ifdef in the variable declarations.

- Ted

#2 lore raw
Quoted 21 lines · Theodore Y. Ts'o
Quoted 8 lines · Arnd Bergmann

The two new variables are only used in an #ifdef, so they cause a warning without CONFIG_QUOTA:

fs/ext4/super.c: In function 'parse_options':
fs/ext4/super.c:1977:26: error: unused variable 'grp_qf_name' [-Werror=unused-variable]

  char *p, *usr_qf_name, *grp_qf_name;
                          ^~~~~~~~~~~
Quoted 7 lines

fs/ext4/super.c:1977:12: error: unused variable 'usr_qf_name' [-Werror=unused-variable]
char *p, *usr_qf_name, *grp_qf_name;

Fixes 20cefcdc2040 ("ext4: fix use-after-free race in ext4_remount()'s

error path")

Hmm, I wonder if we should do something like:

#define EXT4_UNUSED_VAR __attribute__ ((unused))

Quoted 7 lines

and then we could do:

	char *p, *usr_qf_name EXT4_UNUSED_VAR, *grp_qf_name EXT4_UNUSED_VAR;

More generally, I wonder if this is something we should have defined for the whole kernel, as opposed to a one-off hack that ACPI and ext4 subsystems use.

I think the global __maybe_unused macro should work fine here. I though about using that instead, but picked the #ifdef for consistency with the other ifdef in the same function.

Quoted 6 lines

It's a little ugly, but I think it's much nicer than
having extra #ifdefs such as:

	char *p;
#ifdef CONFIG_QUOTA
	char *usr_qf_name, *grp_qf_name;
Quoted 6 lines

#endif

After all, the compiler is perfectly capable of ignoring variables which are unused. And if it's only because of an #ifdef later in the function, it would be nice to not have an extra #ifdef in the variable declarations.

Another alternative that often results in more readable code is to use a check like

        if (IS_ENABLED(CONFIG_QUOTA)) {
             ....
        }

around the conditional code instead of the #ifdef. This should usually work unless the code accesses some struct members that are also hidden in that #ifdef. I have not checked if that is the case here.

Arnd

#3 lore raw
In reply to Theodore Y. Ts'o

Hi Ted,

Quoted 12 lines · Theodore Y. Ts'o
Quoted 10 lines · Arnd Bergmann

The two new variables are only used in an #ifdef, so they cause a warning without CONFIG_QUOTA:

fs/ext4/super.c: In function 'parse_options':
fs/ext4/super.c:1977:26: error: unused variable 'grp_qf_name' [-Werror=unused-variable]

  char *p, *usr_qf_name, *grp_qf_name;
                          ^~~~~~~~~~~

fs/ext4/super.c:1977:12: error: unused variable 'usr_qf_name' [-Werror=unused-variable]
char *p, *usr_qf_name, *grp_qf_name;

Quoted 6 lines
Quoted 2 lines
Fixes 20cefcdc2040 ("ext4: fix use-after-free race in ext4_remount()'s error path")

Hmm, I wonder if we should do something like:

#define EXT4_UNUSED_VAR __attribute__ ((unused))

We have __maybe_unused already, so you can go ahead! :-)

(Also __always_unused, same definition as well, but here it does not may sense).

Quoted 13 lines

and then we could do:

        char *p, *usr_qf_name EXT4_UNUSED_VAR, *grp_qf_name EXT4_UNUSED_VAR;

More generally, I wonder if this is something we should have defined for the whole kernel, as opposed to a one-off hack that ACPI and ext4 subsystems use. It's a little ugly, but I think it's much nicer than having extra #ifdefs such as:

        char *p;
#ifdef CONFIG_QUOTA
        char *usr_qf_name, *grp_qf_name;
Quoted 6 lines

#endif

After all, the compiler is perfectly capable of ignoring variables which are unused. And if it's only because of an #ifdef later in the function, it would be nice to not have an extra #ifdef in the variable declarations.

Indeed, it looks clean --- I like it.

Although I am not sure how people will feel about that :-) Someone may argue that, for consistency, we shouldn't, because inside structs we have to use #ifdefs still.

Cheers,
Miguel