[PATCH 2/4] drivers/thermal: decrement sizeof size in strncmp

From: Julia Lawall
Date: Thu Nov 12 2009 - 02:49:12 EST


From: Julia Lawall <julia@xxxxxxx>

As observed by Joe Perches, sizeof of a constant string includes the
trailing 0. If what is wanted is to check the initial characters of
another string, this trailing 0 should not be taken into account. If an
exact match is wanted, strcmp should be used instead.

The semantic patch that makes this change is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@@
expression foo;
constant char *abc;
@@

strncmp(foo, abc,
- sizeof(abc)
+ sizeof(abc)-1
)
// </smpl>

Signed-off-by: Julia Lawall <julia@xxxxxxx>

---
drivers/thermal/thermal_sys.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff -u -p a/drivers/thermal/thermal_sys.c b/drivers/thermal/thermal_sys.c
--- a/drivers/thermal/thermal_sys.c
+++ b/drivers/thermal/thermal_sys.c
@@ -147,9 +147,9 @@ mode_store(struct device *dev, struct de
if (!tz->ops->set_mode)
return -EPERM;

- if (!strncmp(buf, "enabled", sizeof("enabled")))
+ if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
- else if (!strncmp(buf, "disabled", sizeof("disabled")))
+ else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
else
result = -EINVAL;
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/