Skip to contentSkip to conversation

mmc: mmc_spi: fix snprintf() output buffer size

PATCH In review Bartosz Golaszewski 3 participants 4 messages Oct 7, 2024 – Oct 8, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc: mmc_spi: fix snprintf() output buffer size

From: Bartosz Golaszewski <[email protected]>

GCC 13 complains about the truncated output of snprintf():

drivers/mmc/host/mmc_spi.c: In function ‘mmc_spi_response_get’:
drivers/mmc/host/mmc_spi.c:227:64: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
227 | snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
| ^
drivers/mmc/host/mmc_spi.c:227:9: note: ‘snprintf’ output between 26 and 43 bytes into a destination of size 32

  227 |         snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  228 |                 cmd->opcode, maptype(cmd));

Increase the size of the target buffer.

Fixes 15a0580ced08 ("mmc_spi host driver")
---
 drivers/mmc/host/mmc_spi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/host/mmc_spi.c +1 -1
index 8fee7052f2ef..fa1d1a1b3142 100644
@@ -222,7 +222,7 @@ static int mmc_spi_response_get(struct mmc_spi_host *host,
 	u8 	leftover = 0;
 	unsigned short rotator;
 	int 	i;
-	char	tag[32];
+	char	tag[43];
 
 	snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
 		cmd->opcode, maptype(cmd));
Signature · 1 line
2.43.0
#1 lore raw

Le 07/10/2024 à 13:45, Bartosz Golaszewski a écrit :

Quoted 26 lines

From: Bartosz Golaszewski <[email protected]>

GCC 13 complains about the truncated output of snprintf():

drivers/mmc/host/mmc_spi.c: In function ‘mmc_spi_response_get’:
drivers/mmc/host/mmc_spi.c:227:64: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
227 | snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
| ^
drivers/mmc/host/mmc_spi.c:227:9: note: ‘snprintf’ output between 26 and 43 bytes into a destination of size 32

   227 |         snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   228 |                 cmd->opcode, maptype(cmd));

Increase the size of the target buffer.

Fixes 15a0580ced08 ("mmc_spi host driver")
---
  drivers/mmc/host/mmc_spi.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/host/mmc_spi.c +0 -0
index 8fee7052f2ef..fa1d1a1b3142 100644

drivers/mmc/host/mmc_spi.c static int mmc_spi_response_get(struct mmc_spi_host *host, · line 228

Quoted 8 lines
drivers/mmc/host/mmc_spi.c +1 -1
@@ -222,7 +222,7 @@ static int mmc_spi_response_get(struct mmc_spi_host *host,
  	u8 	leftover = 0;
  	unsigned short rotator;
  	int 	i;
-	char	tag[32];
+	char	tag[43];
 
  	snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
  		cmd->opcode, maptype(cmd));

'tag' is only used in a dev_dbg() at the end of the function.

Maybe " ... CMD%d response SPI_%s" could me moved directly within the dev_dbg(). This would save a few bytes on the stack, save a snprintf() in the normal path and fix the warning without the need of magic number.

just my 2c.

CJ

#2 lore raw
Quoted 39 lines · Christophe JAILLET
Quoted 26 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GCC 13 complains about the truncated output of snprintf():

drivers/mmc/host/mmc_spi.c: In function ‘mmc_spi_response_get’:
drivers/mmc/host/mmc_spi.c:227:64: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
227 | snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
| ^
drivers/mmc/host/mmc_spi.c:227:9: note: ‘snprintf’ output between 26 and 43 bytes into a destination of size 32

   227 |         snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   228 |                 cmd->opcode, maptype(cmd));

Increase the size of the target buffer.

Fixes 15a0580ced08 ("mmc_spi host driver")
---
  drivers/mmc/host/mmc_spi.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/host/mmc_spi.c +0 -0
index 8fee7052f2ef..fa1d1a1b3142 100644

drivers/mmc/host/mmc_spi.c static int mmc_spi_response_get(struct mmc_spi_host *host, · line 228

Quoted 8 lines
drivers/mmc/host/mmc_spi.c +1 -1
@@ -222,7 +222,7 @@ static int mmc_spi_response_get(struct mmc_spi_host *host,
      u8      leftover = 0;
      unsigned short rotator;
      int     i;
-     char    tag[32];
+     char    tag[43];
 
      snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
              cmd->opcode, maptype(cmd));

'tag' is only used in a dev_dbg() at the end of the function.

Quoted 7 lines

Maybe " ... CMD%d response SPI_%s" could me moved directly within the dev_dbg(). This would save a few bytes on the stack, save a snprintf() in the normal path and fix the warning without the need of magic number.

just my 2c.

CJ

I would be hesitant to change this logic here. The cmd struct is manipulated pretty extensively later in the function which leads me to believe that this snprintf() here was done on purpose.

Bart

#3 lore raw
Quoted 46 lines · Bartosz Golaszewski
Quoted 43 lines · Christophe JAILLET
Quoted 26 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GCC 13 complains about the truncated output of snprintf():

drivers/mmc/host/mmc_spi.c: In function ‘mmc_spi_response_get’:
drivers/mmc/host/mmc_spi.c:227:64: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
227 | snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
| ^
drivers/mmc/host/mmc_spi.c:227:9: note: ‘snprintf’ output between 26 and 43 bytes into a destination of size 32

   227 |         snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   228 |                 cmd->opcode, maptype(cmd));

Increase the size of the target buffer.

Fixes 15a0580ced08 ("mmc_spi host driver")
---
  drivers/mmc/host/mmc_spi.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/host/mmc_spi.c +0 -0
index 8fee7052f2ef..fa1d1a1b3142 100644

drivers/mmc/host/mmc_spi.c static int mmc_spi_response_get(struct mmc_spi_host *host, · line 228

Quoted 8 lines
drivers/mmc/host/mmc_spi.c +1 -1
@@ -222,7 +222,7 @@ static int mmc_spi_response_get(struct mmc_spi_host *host,
      u8      leftover = 0;
      unsigned short rotator;
      int     i;
-     char    tag[32];
+     char    tag[43];
 
      snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
              cmd->opcode, maptype(cmd));

'tag' is only used in a dev_dbg() at the end of the function.

Maybe " ... CMD%d response SPI_%s" could me moved directly within the dev_dbg(). This would save a few bytes on the stack, save a snprintf() in the normal path and fix the warning without the need of magic number.

Quoted 7 lines
Quoted 3 lines

just my 2c.

CJ

I would be hesitant to change this logic here. The cmd struct is manipulated pretty extensively later in the function which leads me to believe that this snprintf() here was done on purpose.

The cmd->opcode and cmd->flags aren't really something a host driver should need to change. It's the core that sets them to inform the host driver about the command.

I looked closer and it seems to be correct in this case too.

Kind regards
Uffe