Skip to content

Commit

Permalink
samples/kobject: Use kstrtoint instead of sscanf
Browse files Browse the repository at this point in the history
Use kstrtoint function instead of sscanf and check for return values.

Signed-off-by: Rastislav Barlik <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
barlik authored and gregkh committed Mar 25, 2015
1 parent 1c34203 commit 5fd637e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions samples/kobject/kobject-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
Expand Down
14 changes: 11 additions & 3 deletions samples/kobject/kset-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
Expand Down

0 comments on commit 5fd637e

Please sign in to comment.