[PATCH] powerpc: fix i2c device string format

Guennadi Liakhovetski g.liakhovetski at gmx.de
Fri Aug 10 06:46:55 EST 2007


On Thu, 9 Aug 2007, Segher Boessenkool wrote:

> > > strncpy() won't put a terminating zero on there, is everything
> > > that uses the resulting string okay with that?  Also, if the
> > > name gets cut short, it might match some _other_ expected name.
> > 
> > On Wed, 1 Aug 2007, Scott Wood wrote:
> > 
> > > You could use strlcpy() instead, which always leaves a zero terminator.
> > 
> > The patch below does exactly this - uses strlcpy() to guarantee strings in
> > i2c device type and driver_name fields are 0-terminated.
> 
> You're not checking the return values of these calls.  This would
> be a good function to put attribute warn_unused_result on :-)

hm... Well, the worst that could happen, if an "evil" programmer defines 
too long a name, it gets truncated, and then binds to a wrong driver, 
well, the worst that can happen is that your hardware gets damaged, not a 
big thing. However, some might disagree, so, below is a new version... 
Wrap some long lines while at that.

Signed-off-by: G. Liakhovetski <g.liakhovetski at gmx.de>

diff --git a/arch/powerpc/sysdev/fsl_soc.c b/arch/powerpc/sysdev/fsl_soc.c
index 727453d..c0d66df 100644
--- a/arch/powerpc/sysdev/fsl_soc.c
+++ b/arch/powerpc/sysdev/fsl_soc.c
@@ -320,21 +320,26 @@ static struct i2c_driver_device i2c_devices[] __initdata = {
 	{"ricoh,rv5c387a", "rtc-rs5c372", "rv5c387a",},
 };
 
-static int __init of_find_i2c_driver(struct device_node *node, struct i2c_board_info *info)
+static int __init of_find_i2c_driver(struct device_node *node,
+				     struct i2c_board_info *info)
 {
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) {
 		if (!of_device_is_compatible(node, i2c_devices[i].of_device))
 			continue;
-		strncpy(info->driver_name, i2c_devices[i].i2c_driver, KOBJ_NAME_LEN);
-		strncpy(info->type, i2c_devices[i].i2c_type, I2C_NAME_SIZE);
+		if (strlcpy(info->driver_name, i2c_devices[i].i2c_driver,
+			    KOBJ_NAME_LEN) >= KOBJ_NAME_LEN ||
+		    strlcpy(info->type, i2c_devices[i].i2c_type,
+			    I2C_NAME_SIZE) >= I2C_NAME_SIZE)
+			return -ENOMEM;
 		return 0;
 	}
 	return -ENODEV;
 }
 
-static void __init of_register_i2c_devices(struct device_node *adap_node, int bus_num)
+static void __init of_register_i2c_devices(struct device_node *adap_node,
+					   int bus_num)
 {
 	struct device_node *node = NULL;
 



More information about the Linuxppc-dev mailing list