Skip to contentSkip to conversation

mtd: sm_ftl: fix NULL pointer warning

PATCH Applied Arnd Bergmann 2 participants 4 messages Jan 7, 2020 – Jan 9, 2020 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: sm_ftl: fix NULL pointer warning

With gcc -O3, we get a new warning:

In file included from arch/arm64/include/asm/processor.h:28,
from drivers/mtd/sm_ftl.c:8:
In function 'memset',
inlined from 'sm_read_sector.constprop' at drivers/mtd/sm_ftl.c:250:3:
include/linux/string.h:411:9: error: argument 1 null where non-null expected [-Werror=nonnull]
return __builtin_memset(p, c, size);

From all I can tell, this cannot happen (the function is called either with a NULL buffer or with a -1 block number but not both), but adding a check makes it more robust and avoids the warning.

Fixes mmtom ("init/Kconfig: enable -O3 for all arches")
---
 drivers/mtd/sm_ftl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mtd/sm_ftl.c +2 -1
index 4744bf94ad9a..b9f272408c4d 100644
@@ -247,7 +247,8 @@ static int sm_read_sector(struct sm_ftl *ftl,
 
 	/* FTL can contain -1 entries that are by default filled with bits */
 	if (block == -1) {
-		memset(buffer, 0xFF, SM_SECTOR_SIZE);
+		if (buffer)
+			memset(buffer, 0xFF, SM_SECTOR_SIZE);
 		return 0;
 	}
 
Signature · 1 line
2.20.0
maintainer
#1 lore raw

Hi Arnd,

Arnd Bergmann <[email protected]> wrote on Tue, 7 Jan 2020 22:24:52 +0100:

Quoted 24 lines

With gcc -O3, we get a new warning:

In file included from arch/arm64/include/asm/processor.h:28,
from drivers/mtd/sm_ftl.c:8:
In function 'memset',
inlined from 'sm_read_sector.constprop' at drivers/mtd/sm_ftl.c:250:3:
include/linux/string.h:411:9: error: argument 1 null where non-null expected [-Werror=nonnull]
return __builtin_memset(p, c, size);

From all I can tell, this cannot happen (the function is called either with a NULL buffer or with a -1 block number but not both), but adding a check makes it more robust and avoids the warning.

Fixes mmtom ("init/Kconfig: enable -O3 for all arches")
---
 drivers/mtd/sm_ftl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mtd/sm_ftl.c +0 -0
index 4744bf94ad9a..b9f272408c4d 100644

drivers/mtd/sm_ftl.c static int sm_read_sector(struct sm_ftl *ftl, · line 254

Quoted 9 lines
drivers/mtd/sm_ftl.c +2 -1
@@ -247,7 +247,8 @@ static int sm_read_sector(struct sm_ftl *ftl,
 
 	/* FTL can contain -1 entries that are by default filled with bits */
 	if (block == -1) {
-		memset(buffer, 0xFF, SM_SECTOR_SIZE);
+		if (buffer)
+			memset(buffer, 0xFF, SM_SECTOR_SIZE);
 		return 0;
 	}
 

What about a simpler form:

        if (buffer && block == -1) { ...

Instead of an additional indentation level?

Thanks,
Miquèl

#2 lore raw
Quoted 41 lines · Miquel Raynal

Hi Arnd,

Arnd Bergmann <[email protected]> wrote on Tue, 7 Jan 2020 22:24:52 +0100:

Quoted 24 lines

With gcc -O3, we get a new warning:

In file included from arch/arm64/include/asm/processor.h:28,
from drivers/mtd/sm_ftl.c:8:
In function 'memset',
inlined from 'sm_read_sector.constprop' at drivers/mtd/sm_ftl.c:250:3:
include/linux/string.h:411:9: error: argument 1 null where non-null expected [-Werror=nonnull]
return __builtin_memset(p, c, size);

From all I can tell, this cannot happen (the function is called either with a NULL buffer or with a -1 block number but not both), but adding a check makes it more robust and avoids the warning.

Fixes mmtom ("init/Kconfig: enable -O3 for all arches")
---
 drivers/mtd/sm_ftl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mtd/sm_ftl.c +0 -0
index 4744bf94ad9a..b9f272408c4d 100644

drivers/mtd/sm_ftl.c static int sm_read_sector(struct sm_ftl *ftl, · line 254

Quoted 9 lines
drivers/mtd/sm_ftl.c +2 -1
@@ -247,7 +247,8 @@ static int sm_read_sector(struct sm_ftl *ftl,
 
      /* FTL can contain -1 entries that are by default filled with bits */
      if (block == -1) {
-             memset(buffer, 0xFF, SM_SECTOR_SIZE);
+             if (buffer)
+                     memset(buffer, 0xFF, SM_SECTOR_SIZE);
              return 0;
      }
 

What about a simpler form:

Quoted 3 lines
        if (buffer && block == -1) { ...

Instead of an additional indentation level?

That would fail to return early from the function if we ever get block==-1 and buffer==NULL, probably resulting is worse problems later.

Arnd

maintainer Applied
#3 lore raw
In reply to Arnd Bergmann
Quoted 9 lines · Arnd Bergmann

With gcc -O3, we get a new warning:

In file included from arch/arm64/include/asm/processor.h:28,
from drivers/mtd/sm_ftl.c:8:
In function 'memset',
inlined from 'sm_read_sector.constprop' at drivers/mtd/sm_ftl.c:250:3:
include/linux/string.h:411:9: error: argument 1 null where non-null expected [-Werror=nonnull]
return __builtin_memset(p, c, size);

Quoted 6 lines
Quoted 1 line

From all I can tell, this cannot happen (the function is called

either with a NULL buffer or with a -1 block number but not both), but adding a check makes it more robust and avoids the warning.

Fixes mmtom ("init/Kconfig: enable -O3 for all arches")

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes, thanks.

Miquel

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/