Skip to contentSkip to conversation

Documentation: Fix size parameter for snprintf (reported by cppcheck)

PATCH New Stefan Weil 1 participant 1 message Dec 30, 2013 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Documentation: Fix size parameter for snprintf (reported by cppcheck)

cppcheck reports this error:

Documentation/laptops/hpfall.c:33]: (error)
Dangerous usage of 'devname' (strncpy doesn't always 0-terminate it)

The terminating '\0' is needed for the global char array unload_heads_path, so never write the last array entry (which is initially '\0').

---

This is only code in the documentation, nevertheless even there a correction might help other users.

Cheers and happy new year

Stefan Weil

 Documentation/laptops/hpfall.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/laptops/hpfall.c +1 -1
index a4a8fc5..b85dbba 100644
@@ -29,7 +29,7 @@ int set_unload_heads_path(char *device)
 		return -EINVAL;
 	strncpy(devname, device + 5, sizeof(devname));
 
-	snprintf(unload_heads_path, sizeof(unload_heads_path),
+	snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
 				"/sys/block/%s/device/unload_heads", devname);
 	return 0;
 }
Signature · 1 line
1.7.10.4