Skip to contentSkip to conversation

spi: Fix potential uninitialized variable in probe()

PATCH Superseded Dan Carpenter 3 participants 4 messages Nov 21, 2025 – Nov 24, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: Fix potential uninitialized variable in probe()

If the device tree is messed up, then potentially the "protocol" string could potentially be uninitialized. Add a check to prevent that.

Fixes 059f545832be ("spi: add support for microchip "soft" spi controller")
---
 drivers/spi/spi-microchip-core-spi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-microchip-core-spi.c +2 -0
index b8738190cdcb..e65036cc62f3 100644
@@ -320,6 +320,8 @@ static int mchp_corespi_probe(struct platform_device *pdev)
 	 */
 	ret = of_property_read_string(pdev->dev.of_node, "microchip,protocol-configuration",
 				      &protocol);
+	if (ret)
+		return ret;
 	if (strcmp(protocol, "motorola") != 0)
 		return dev_err_probe(&pdev->dev, -EINVAL,
 				     "CoreSPI: protocol '%s' not supported by this driver\n",
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 14 lines · Dan Carpenter

If the device tree is messed up, then potentially the "protocol" string could potentially be uninitialized. Add a check to prevent that.

Fixes 059f545832be ("spi: add support for microchip "soft" spi controller")
---
 drivers/spi/spi-microchip-core-spi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-microchip-core-spi.c +0 -0
index b8738190cdcb..e65036cc62f3 100644

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_probe(struct platform_device *pdev) · line 327

Quoted 8 lines
drivers/spi/spi-microchip-core-spi.c +2 -0
@@ -320,6 +320,8 @@ static int mchp_corespi_probe(struct platform_device *pdev)
 	 */
 	ret = of_property_read_string(pdev->dev.of_node, "microchip,protocol-configuration",
 				      &protocol);
+	if (ret)
+		return ret;
 	if (strcmp(protocol, "motorola") != 0)
 		return dev_err_probe(&pdev->dev, -EINVAL,
 				     "CoreSPI: protocol '%s' not supported by this driver\n",

This should probably also complain about not being able to get the property, otherwise nobody is going to be able to figure out what's wrong if we actually hit the error case.

#2 lore raw
Quoted 19 lines · Mark Brown
Quoted 18 lines · Dan Carpenter

If the device tree is messed up, then potentially the "protocol" string could potentially be uninitialized. Add a check to prevent that.

Fixes 059f545832be ("spi: add support for microchip "soft" spi controller")
---
 drivers/spi/spi-microchip-core-spi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-microchip-core-spi.c +1 -0
index b8738190cdcb..e65036cc62f3 100644
@@ -320,6 +320,8 @@ static int mchp_corespi_probe(struct platform_device *pdev)
 	 */
 	ret = of_property_read_string(pdev->dev.of_node, "microchip,protocol-configuration",
 				      &protocol);
+	if (ret)
Quoted 8 lines

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_probe(struct platform_device *pdev) · line 327

Quoted 4 lines
drivers/spi/spi-microchip-core-spi.c +1 -0
+		return ret;
 	if (strcmp(protocol, "motorola") != 0)
 		return dev_err_probe(&pdev->dev, -EINVAL,
 				     "CoreSPI: protocol '%s' not supported by this driver\n",

This should probably also complain about not being able to get the property, otherwise nobody is going to be able to figure out what's wrong if we actually hit the error case.

The one thing to be careful of is that the property has a default, so EINVAL needs to be treated differently, so the decision tree is something like:
if (ret == _EINVAL)
<do nothing>
else if (ret)
abort complaining about malformed
else if (!motorola)
abort complaining about unsupported mode
else
<do nothing>

obviously that can just become two clauses, but you get the idea.

#3 lore raw
Quoted 41 lines · Conor Dooley
Quoted 19 lines · Mark Brown
Quoted 18 lines · Dan Carpenter

If the device tree is messed up, then potentially the "protocol" string could potentially be uninitialized. Add a check to prevent that.

Fixes 059f545832be ("spi: add support for microchip "soft" spi controller")
---
 drivers/spi/spi-microchip-core-spi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/spi/spi-microchip-core-spi.c +1 -0
index b8738190cdcb..e65036cc62f3 100644
@@ -320,6 +320,8 @@ static int mchp_corespi_probe(struct platform_device *pdev)
 	 */
 	ret = of_property_read_string(pdev->dev.of_node, "microchip,protocol-configuration",
 				      &protocol);
+	if (ret)
Quoted 8 lines

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_probe(struct platform_device *pdev) · line 327

Quoted 4 lines
drivers/spi/spi-microchip-core-spi.c +1 -0
+		return ret;
 	if (strcmp(protocol, "motorola") != 0)
 		return dev_err_probe(&pdev->dev, -EINVAL,
 				     "CoreSPI: protocol '%s' not supported by this driver\n",

This should probably also complain about not being able to get the property, otherwise nobody is going to be able to figure out what's wrong if we actually hit the error case.

The one thing to be careful of is that the property has a default, so EINVAL needs to be treated differently, so the decision tree is something like:
if (ret == _EINVAL)
<do nothing>
else if (ret)
abort complaining about malformed
else if (!motorola)
abort complaining about unsupported mode
else
<do nothing>

Quoted 1 line

obviously that can just become two clauses, but you get the idea.

Sure. I've sent a v2 which defaults to motorola.

regards,
dan carpenter