Skip to contentSkip to conversation

spi: replace strcpy(), sprintf() and simple_strtoul() with strscpy(), snprintf() and kstrtoul()

PATCH In review Aayush Kumar 4 participants 9 messages Oct 9 – Oct 15

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

cover spi: replace strcpy(), sprintf() and simple_strtoul() with strscpy(), snprintf() and kstrtoul()

Replace strcpy(), sprintf() and simple_strtoul() with their safer modern equivalents (strscpy, snprintf, kstrtoul).

Aayush Kumar (5):
spi: butterfly: Use strscpy() instead of strcpy()
spi: lm70llp: Use strscpy() instead of strcpy()
spi: tle62x0: Use kstrtoul() instead of simple_strtoul()
spi: omap2-mcspi: Use snprintf() instead of sprintf()
spi: s3c64xx: Use snprintf() instead of sprintf()

 drivers/spi/spi-butterfly.c   | 3 ++-
 drivers/spi/spi-lm70llp.c     | 2 +-
 drivers/spi/spi-omap2-mcspi.c | 6 ++++--
 drivers/spi/spi-s3c64xx.c     | 3 ++-
 drivers/spi/spi-tle62x0.c     | 6 +++---
 5 files changed, 12 insertions(+), 8 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/5 spi: butterfly: Use strscpy() instead of strcpy()

Use a safer function strscpy() instead of strcpy() for copying to arrays. Only idiomatic code replacement, and no functional changes.

---
 drivers/spi/spi-butterfly.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/spi/spi-butterfly.c +2 -1
index 84eb454ed..5e1e205bb 100644
@@ -259,7 +259,8 @@ static void butterfly_attach(struct parport *p)
 	 */
 
 	pp->info[0].max_speed_hz = 15 * 1000 * 1000;
-	strcpy(pp->info[0].modalias, "mtd_dataflash");
+	strscpy(pp->info[0].modalias, "mtd_dataflash",
+		sizeof(pp->info[0].modalias));
 	pp->info[0].platform_data = &flash;
 	pp->info[0].chip_select = 1;
 	pp->info[0].controller_data = pp;
Signature · 1 line
2.56.0
#2 lore raw

PATCH 2/5 spi: lm70llp: Use strscpy() instead of strcpy()

In reply to Aayush Kumar

Use a safer function strscpy() instead of strcpy() for copying to arrays. Only idiomatic code replacement, and no functional changes.

---
 drivers/spi/spi-lm70llp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-lm70llp.c +1 -1
index e61e89b41..bd8028892 100644
@@ -252,7 +252,7 @@ static void spi_lm70llp_attach(struct parport *p)
 	 * We are binding to the generic drivers/hwmon/lm70.c device
 	 * driver.
 	 */
-	strcpy(pp->info.modalias, "lm70");
+	strscpy(pp->info.modalias, "lm70", sizeof(pp->info.modalias));
 	pp->info.max_speed_hz = 6 * 1000 * 1000;
 	pp->info.chip_select = 0;
 	pp->info.mode = SPI_3WIRE | SPI_MODE_0;
Signature · 1 line
2.56.0
#5 lore raw

PATCH 3/5 spi: tle62x0: Use kstrtoul() instead of simple_strtoul()

In reply to Aayush Kumar

Use the recommended kstrtoul() instead of the deprecated simple_strtoul(). Check the return value and reject invalid values.

---
 drivers/spi/spi-tle62x0.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/spi/spi-tle62x0.c +3 -3
index dd8bfb3f3..986d78ab4 100644
@@ -151,10 +151,10 @@ static ssize_t tle62x0_gpio_store(struct device *dev,
 	struct tle62x0_state *st = dev_get_drvdata(dev);
 	int gpio_num = to_gpio_num(attr);
 	unsigned long val;
-	char *endp;
+	int ret;
 
-	val = simple_strtoul(buf, &endp, 0);
-	if (buf == endp)
+	ret = kstrtoul(buf, 0, &val);
+	if (ret)
 		return -EINVAL;
 
 	dev_dbg(dev, "setting gpio %d to %ld\n", gpio_num, val);
Signature · 1 line
2.56.0
#6 lore raw

PATCH 4/5 spi: omap2-mcspi: Use snprintf() instead of sprintf()

In reply to Aayush Kumar

Use snprintf() instead of sprintf() to avoid potential buffer overflows when formatting DMA channel names.

---
 drivers/spi/spi-omap2-mcspi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/spi/spi-omap2-mcspi.c +4 -2
index a3355147f..311ee0ca9 100644
@@ -1544,8 +1544,10 @@ static int omap2_mcspi_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	for (i = 0; i < ctlr->num_chipselect; i++) {
-		sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
-		sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
+		snprintf(mcspi->dma_channels[i].dma_rx_ch_name,
+			 sizeof(mcspi->dma_channels[i].dma_rx_ch_name), "rx%d", i);
+		snprintf(mcspi->dma_channels[i].dma_tx_ch_name,
+			 sizeof(mcspi->dma_channels[i].dma_tx_ch_name), "tx%d", i);
 
 		status = omap2_mcspi_request_dma(mcspi,
 						 &mcspi->dma_channels[i]);
Signature · 1 line
2.56.0
#7 lore raw

PATCH 5/5 spi: s3c64xx: Use snprintf() instead of sprintf()

In reply to Aayush Kumar

Use snprintf() instead of sprintf() to avoid potential buffer overflows when formatting the clock name.

---
 drivers/spi/spi-s3c64xx.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/spi/spi-s3c64xx.c +2 -1
index 8d4120cf9..8195bf376 100644
@@ -1331,7 +1331,8 @@ static int s3c64xx_spi_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, PTR_ERR(sdd->clk),
 				     "Unable to acquire clock 'spi'\n");
 
-	sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
+	snprintf(clk_name, sizeof(clk_name), "spi_busclk%d",
+		 sci->src_clk_nr);
 	sdd->src_clk = devm_clk_get_enabled(&pdev->dev, clk_name);
 	if (IS_ERR(sdd->src_clk))
 		return dev_err_probe(&pdev->dev, PTR_ERR(sdd->src_clk),
Signature · 1 line
2.56.0
#3 lore raw

Re: [PATCH 2/5] spi: lm70llp: Use strscpy() instead of strcpy()

In reply to Aayush Kumar
Quoted 13 lines · On Fri, 9 Oct 2026 19:06:06 +0530 Aayush Kumar <[email protected]> wrote:

Use a safer function strscpy() instead of strcpy() for copying to arrays. Only idiomatic code replacement, and no functional changes.

---
 drivers/spi/spi-lm70llp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-lm70llp.c +0 -0
index e61e89b41..bd8028892 100644

drivers/spi/spi-lm70llp.c static void spi_lm70llp_attach(struct parport *p) · line 255

Quoted 5 lines
drivers/spi/spi-lm70llp.c +1 -1
@@ -252,7 +252,7 @@ static void spi_lm70llp_attach(struct parport *p)
 	 * We are binding to the generic drivers/hwmon/lm70.c device
 	 * driver.
 	 */
-	strcpy(pp->info.modalias, "lm70");
+	strscpy(pp->info.modalias, "lm70", sizeof(pp->info.modalias));

Come on, if you are going to do that use the two argument form. Or just don't bother at all.
Do the test compiles - if the literal string is too long you'll get a compile error for strcpy(), but strscpy() truncates and returns an error.
So strcpy() is arguably better.

David

Quoted 3 lines
drivers/spi/spi-lm70llp.c +0 -0
 	pp->info.max_speed_hz = 6 * 1000 * 1000;
 	pp->info.chip_select = 0;
 	pp->info.mode = SPI_3WIRE | SPI_MODE_0;
#4 lore raw

Re: [PATCH 2/5] spi: lm70llp: Use strscpy() instead of strcpy()

Quoted 1 line · David Laight

So strcpy() is arguably better.

Fair point, I'll drop both the strscpy patches in v2. Thanks

maintainer
#8 lore raw
In reply to Aayush Kumar
Quoted 5 lines · Aayush Kumar

spi: replace strcpy(), sprintf() and simple_strtoul() with strscpy(), snprintf() and kstrtoul()

Replace strcpy(), sprintf() and simple_strtoul() with their safer modern equivalents (strscpy, snprintf, kstrtoul).

Quoted 8 lines

Aayush Kumar (5):
spi: butterfly: Use strscpy() instead of strcpy()
spi: lm70llp: Use strscpy() instead of strcpy()
spi: tle62x0: Use kstrtoul() instead of simple_strtoul()
spi: omap2-mcspi: Use snprintf() instead of sprintf()
spi: s3c64xx: Use snprintf() instead of sprintf()

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-7.4

Thanks!

[1/5] spi: butterfly: Use strscpy() instead of strcpy()

      https://git.kernel.org/broonie/spi/c/e9b641d92963
[2/5] spi: lm70llp: Use strscpy() instead of strcpy()
      https://git.kernel.org/broonie/spi/c/ae6056b248eb
[3/5] spi: tle62x0: Use kstrtoul() instead of simple_strtoul()
      https://git.kernel.org/broonie/spi/c/9a6b86cee9d7
[4/5] spi: omap2-mcspi: Use snprintf() instead of sprintf()
      https://git.kernel.org/broonie/spi/c/50f3d77e805f
[5/5] spi: s3c64xx: Use snprintf() instead of sprintf()
      https://git.kernel.org/broonie/spi/c/65f9474caced

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark