Skip to content

Commit

Permalink
lib: cleanup kstrto*() usage
Browse files Browse the repository at this point in the history
Use proper conversion functions.  kstrto*() variants exist for all
standard types.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Alexey Dobriyan <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Alexey Dobriyan authored and torvalds committed Dec 16, 2020
1 parent d58b0b1 commit 506dfc9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
9 changes: 3 additions & 6 deletions lib/test_firmware.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,15 @@ static ssize_t test_dev_config_show_int(char *buf, int val)

static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg)
{
u8 val;
int ret;
long new;

ret = kstrtol(buf, 10, &new);
ret = kstrtou8(buf, 10, &val);
if (ret)
return ret;

if (new > U8_MAX)
return -EINVAL;

mutex_lock(&test_fw_mutex);
*(u8 *)cfg = new;
*(u8 *)cfg = val;
mutex_unlock(&test_fw_mutex);

/* Always return full write size even if we didn't consume all */
Expand Down
26 changes: 10 additions & 16 deletions lib/test_kmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,20 +877,17 @@ static int test_dev_config_update_uint_sync(struct kmod_test_device *test_dev,
int (*test_sync)(struct kmod_test_device *test_dev))
{
int ret;
unsigned long new;
unsigned int val;
unsigned int old_val;

ret = kstrtoul(buf, 10, &new);
ret = kstrtouint(buf, 10, &val);
if (ret)
return ret;

if (new > UINT_MAX)
return -EINVAL;

mutex_lock(&test_dev->config_mutex);

old_val = *config;
*(unsigned int *)config = new;
*(unsigned int *)config = val;

ret = test_sync(test_dev);
if (ret) {
Expand All @@ -914,18 +911,18 @@ static int test_dev_config_update_uint_range(struct kmod_test_device *test_dev,
unsigned int min,
unsigned int max)
{
unsigned int val;
int ret;
unsigned long new;

ret = kstrtoul(buf, 10, &new);
ret = kstrtouint(buf, 10, &val);
if (ret)
return ret;

if (new < min || new > max)
if (val < min || val > max)
return -EINVAL;

mutex_lock(&test_dev->config_mutex);
*config = new;
*config = val;
mutex_unlock(&test_dev->config_mutex);

/* Always return full write size even if we didn't consume all */
Expand All @@ -936,18 +933,15 @@ static int test_dev_config_update_int(struct kmod_test_device *test_dev,
const char *buf, size_t size,
int *config)
{
int val;
int ret;
long new;

ret = kstrtol(buf, 10, &new);
ret = kstrtoint(buf, 10, &val);
if (ret)
return ret;

if (new < INT_MIN || new > INT_MAX)
return -EINVAL;

mutex_lock(&test_dev->config_mutex);
*config = new;
*config = val;
mutex_unlock(&test_dev->config_mutex);
/* Always return full write size even if we didn't consume all */
return size;
Expand Down

0 comments on commit 506dfc9

Please sign in to comment.