Skip to contentSkip to conversation

Driver core: Documentation: use snprintf and strnlen

PATCH In review [email protected] 3 participants 3 messages Jul 31, 2005 – Aug 1, 2005 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 3/5 Driver core: Documentation: use snprintf and strnlen

From: Jan Veldeman <[email protected]>

Documentation should give the good example of using snprintf and strnlen in stead of sprintf and strlen.

PAGE_SIZE is used as the maximal length to reflect the behaviour of show/store.

---
 sysfs.txt |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
Documentation/filesystems/sysfs.txt +2 -2
Index: quilt/Documentation/filesystems/sysfs.txt
===================================================================
@@ -216,13 +216,13 @@ A very simple (and naive) implementation
 
 static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
 {
-        return sprintf(buf,"%s\n",dev->name);
+        return snprintf(buf,PAGE_SIZE,"%s\n",dev->name);
 }
 
 static ssize_t store_name(struct device * dev, const char * buf)
 {
 	sscanf(buf,"%20s",dev->name);
-	return strlen(buf);
+	return strnlen(buf,PAGE_SIZE);
 }
 
 static DEVICE_ATTR(name,S_IRUGO,show_name,store_name);

--

#1 lore raw

Hi Domen,

Quoted 6 lines · On Sunday 31 July 2005 13:12, [email protected] wrote:

From: Jan Veldeman <[email protected]>
Documentation should give the good example of using snprintf and strnlen in stead of sprintf and strlen.

PAGE_SIZE is used as the maximal length to reflect the behaviour of show/store.

The whole part of the Documentation is obsoleted by the fact, that struct device has no structure member called "name".

People hacking sysfs should also try to hack the docu to match or at least remove the obsolete parts of it.

So you can drop this patch altogether, I think.

Regards

Ingo Oeser

#2 lore raw

Hi Ingo,

Quoted 8 lines · Ingo Oeser
Quoted 6 lines · On Sunday 31 July 2005 13:12, [email protected] wrote:

From: Jan Veldeman <[email protected]>
Documentation should give the good example of using snprintf and strnlen in stead of sprintf and strlen.

PAGE_SIZE is used as the maximal length to reflect the behaviour of show/store.

Quoted 9 lines

The whole part of the Documentation is obsoleted by the fact, that struct device has no structure member called "name".

People hacking sysfs should also try to hack the docu to match or at least remove the obsolete parts of it.

So you can drop this patch altogether, I think.

Even though the example doesn't work (the comment below the example even indicates it shouldn't be done), I would suggest to still apply this patch, as it shows the good usage of snprintf in stead of sprintf:

Somebody who looks at the documentation will certainly change the contents of the attribute (this is by all means the reason for such an example), but will not likely change the way sprintf is used.

Best regards,
Jan