Re: [PATCH] video/via: Convert to kstrtou8_from_user

From: Alexey Dobriyan
Date: Mon Apr 16 2012 - 12:36:27 EST


On Sun, Apr 15, 2012 at 2:24 AM, Peter Huewe <peterhuewe@xxxxxx> wrote:
> This patch replaces the code for getting an number from a
> userspace buffer by a simple call to kstrou8_from_user.
> This makes it easier to read and less error prone.

Initialization with 0 is not necessary then.
Also "count < 1" check could be dropped, since empty string will fail
with EINVAL.
Otherwise OK.

> --- a/drivers/video/via/viafbdev.c
> +++ b/drivers/video/via/viafbdev.c
> @@ -1276,17 +1276,14 @@ static int viafb_dfph_proc_open(struct inode *inode, struct file *file)
> Âstatic ssize_t viafb_dfph_proc_write(struct file *file,
> Â Â Â Âconst char __user *buffer, size_t count, loff_t *pos)
> Â{
> - Â Â Â char buf[20];
> + Â Â Â int err;
> Â Â Â Âu8 reg_val = 0;
> - Â Â Â unsigned long length;
> Â Â Â Âif (count < 1)
> Â Â Â Â Â Â Â Âreturn -EINVAL;
> - Â Â Â length = count > 20 ? 20 : count;
> - Â Â Â if (copy_from_user(&buf[0], buffer, length))
> - Â Â Â Â Â Â Â return -EFAULT;
> - Â Â Â buf[length - 1] = '\0'; /*Ensure end string */
> - Â Â Â if (kstrtou8(buf, 0, &reg_val) < 0)
> - Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â err = kstrtou8_from_user(buffer, count, 0, &reg_val);
> + Â Â Â if (err)
> + Â Â Â Â Â Â Â return err;
> +
> Â Â Â Âviafb_write_reg_mask(CR97, VIACR, reg_val, 0x0f);
> Â Â Â Âreturn count;
> Â}
> @@ -1316,17 +1313,14 @@ static int viafb_dfpl_proc_open(struct inode *inode, struct file *file)
> Âstatic ssize_t viafb_dfpl_proc_write(struct file *file,
> Â Â Â Âconst char __user *buffer, size_t count, loff_t *pos)
> Â{
> - Â Â Â char buf[20];
> + Â Â Â int err;
> Â Â Â Âu8 reg_val = 0;
> - Â Â Â unsigned long length;
> Â Â Â Âif (count < 1)
> Â Â Â Â Â Â Â Âreturn -EINVAL;
> - Â Â Â length = count > 20 ? 20 : count;
> - Â Â Â if (copy_from_user(&buf[0], buffer, length))
> - Â Â Â Â Â Â Â return -EFAULT;
> - Â Â Â buf[length - 1] = '\0'; /*Ensure end string */
> - Â Â Â if (kstrtou8(buf, 0, &reg_val) < 0)
> - Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â err = kstrtou8_from_user(buffer, count, 0, &reg_val);
> + Â Â Â if (err)
> + Â Â Â Â Â Â Â return err;
> +
> Â Â Â Âviafb_write_reg_mask(CR99, VIACR, reg_val, 0x0f);
> Â Â Â Âreturn count;
> Â}
> --
> 1.7.3.4
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at Âhttp://vger.kernel.org/majordomo-info.html
> Please read the FAQ at Âhttp://www.tux.org/lkml/
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/