Skip to contentSkip to conversation

samples/kobject: Use kstrtoint instead of sscanf

PATCH In review Rastislav Barlik 3 participants 3 messages Dec 16, 2014 – Dec 19, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 samples/kobject: Use kstrtoint instead of sscanf

Use kstrtoint function instead of sscanf and check for return values.

---
 samples/kobject/kobject-example.c | 14 +++++++++++---
 samples/kobject/kset-example.c    | 14 +++++++++++---
 2 files changed, 22 insertions(+), 6 deletions(-)
samples/kobject/kobject-example.c +11 -3
index 01562e0..063aaec 100644
@@ -36,7 +36,12 @@ static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
 static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
 			 const char *buf, size_t count)
 {
-	sscanf(buf, "%du", &foo);
+	int ret;
+
+	ret = kstrtoint(buf, 10, &foo);
+	if (ret < 0)
+		return ret;
+
 	return count;
 }
 
@@ -63,9 +68,12 @@ static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
 static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
 		       const char *buf, size_t count)
 {
-	int var;
+	int var, ret;
+
+	ret = kstrtoint(buf, 10, &var);
+	if (ret < 0)
+		return ret;
 
-	sscanf(buf, "%du", &var);
 	if (strcmp(attr->attr.name, "baz") == 0)
 		baz = var;
 	else
samples/kobject/kset-example.c +11 -3
index ab5e447..e80ced3 100644
@@ -120,7 +120,12 @@ static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
 static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
 			 const char *buf, size_t count)
 {
-	sscanf(buf, "%du", &foo_obj->foo);
+	int ret;
+
+	ret = kstrtoint(buf, 10, &foo_obj->foo);
+	if (ret < 0)
+		return ret;
+
 	return count;
 }
 
@@ -147,9 +152,12 @@ static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
 static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
 		       const char *buf, size_t count)
 {
-	int var;
+	int var, ret;
+
+	ret = kstrtoint(buf, 10, &var);
+	if (ret < 0)
+		return ret;
 
-	sscanf(buf, "%du", &var);
 	if (strcmp(attr->attr.name, "baz") == 0)
 		foo_obj->baz = var;
 	else
Signature · 1 line
2.1.3
#1 lore raw
Quoted 3 lines · Rastislav Barlik

Use kstrtoint function instead of sscanf and check for return values.

Hmm, this seems like Greg, not me.

Greg, I noticed this in samples/kobject/kobject-example.c:

* Released under the GPL version 2 only.
...
MODULE_LICENSE("GPL");

Quoted 1 line

From module.h:

* The following license idents are currently accepted as indicating free
* software modules
*
* "GPL" [GNU Public License v2 or later]
* "GPL v2" [GNU Public License v2]

Cheers,
Rusty.

Quoted 73 lines
---
 samples/kobject/kobject-example.c | 14 +++++++++++---
 samples/kobject/kset-example.c    | 14 +++++++++++---
 2 files changed, 22 insertions(+), 6 deletions(-)
samples/kobject/kobject-example.c +11 -3
index 01562e0..063aaec 100644
@@ -36,7 +36,12 @@ static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
 static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
 			 const char *buf, size_t count)
 {
-	sscanf(buf, "%du", &foo);
+	int ret;
+
+	ret = kstrtoint(buf, 10, &foo);
+	if (ret < 0)
+		return ret;
+
 	return count;
 }
 
@@ -63,9 +68,12 @@ static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr,
 static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
 		       const char *buf, size_t count)
 {
-	int var;
+	int var, ret;
+
+	ret = kstrtoint(buf, 10, &var);
+	if (ret < 0)
+		return ret;
 
-	sscanf(buf, "%du", &var);
 	if (strcmp(attr->attr.name, "baz") == 0)
 		baz = var;
 	else
samples/kobject/kset-example.c +11 -3
index ab5e447..e80ced3 100644
@@ -120,7 +120,12 @@ static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
 static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
 			 const char *buf, size_t count)
 {
-	sscanf(buf, "%du", &foo_obj->foo);
+	int ret;
+
+	ret = kstrtoint(buf, 10, &foo_obj->foo);
+	if (ret < 0)
+		return ret;
+
 	return count;
 }
 
@@ -147,9 +152,12 @@ static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr,
 static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr,
 		       const char *buf, size_t count)
 {
-	int var;
+	int var, ret;
+
+	ret = kstrtoint(buf, 10, &var);
+	if (ret < 0)
+		return ret;
 
-	sscanf(buf, "%du", &var);
 	if (strcmp(attr->attr.name, "baz") == 0)
 		foo_obj->baz = var;
 	else
Signature · 1 line
2.1.3
#2 lore raw
Quoted 13 lines · Rusty Russell
Quoted 3 lines · Rastislav Barlik

Use kstrtoint function instead of sscanf and check for return values.

Hmm, this seems like Greg, not me.

Greg, I noticed this in samples/kobject/kobject-example.c:

* Released under the GPL version 2 only.
...
MODULE_LICENSE("GPL");

Quoted 7 lines
Quoted 1 line

From module.h:

* The following license idents are currently accepted as indicating free
* software modules
*
* "GPL" [GNU Public License v2 or later]
* "GPL v2" [GNU Public License v2]

Thanks, I'll fix that up after 3.19-rc1 is out.

greg k-h