OSDN Git Service

samples/kobject: Use kstrtoint instead of sscanf
authorRastislav Barlik <barlik@zoho.com>
Wed, 17 Dec 2014 20:14:48 +0000 (21:14 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 25 Mar 2015 12:40:31 +0000 (13:40 +0100)
Use kstrtoint function instead of sscanf and check for return values.

Signed-off-by: Rastislav Barlik <barlik@zoho.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
samples/kobject/kobject-example.c
samples/kobject/kset-example.c

index 01562e0..063aaec 100644 (file)
@@ -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
index ab5e447..e80ced3 100644 (file)
@@ -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