Skip to content

Commit 5450abb

Browse files
tobluxAlexander Gordeev
authored andcommitted
s390/hmcdrv: Replace kmalloc() + copy_from_user() with memdup_user_nul()
Replace kmalloc() followed by copy_from_user() with memdup_user_nul() to improve and simplify hmcdrv_dev_write(). Remove the manual NUL-termination. No functional changes intended. Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Signed-off-by: Alexander Gordeev <agordeev@linux.ibm.com>
1 parent f5507ae commit 5450abb

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

drivers/s390/char/hmcdrv_dev.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,24 +244,17 @@ static ssize_t hmcdrv_dev_write(struct file *fp, const char __user *ubuf,
244244
size_t len, loff_t *pos)
245245
{
246246
ssize_t retlen;
247+
void *pdata;
247248

248249
pr_debug("writing file '/dev/%pD' at pos. %lld with length %zd\n",
249250
fp, (long long) *pos, len);
250251

251252
if (!fp->private_data) { /* first expect a cmd write */
252-
fp->private_data = kmalloc(len + 1, GFP_KERNEL);
253-
254-
if (!fp->private_data)
255-
return -ENOMEM;
256-
257-
if (!copy_from_user(fp->private_data, ubuf, len)) {
258-
((char *)fp->private_data)[len] = '\0';
259-
return len;
260-
}
261-
262-
kfree(fp->private_data);
263-
fp->private_data = NULL;
264-
return -EFAULT;
253+
pdata = memdup_user_nul(ubuf, len);
254+
if (IS_ERR(pdata))
255+
return PTR_ERR(pdata);
256+
fp->private_data = pdata;
257+
return len;
265258
}
266259

267260
retlen = hmcdrv_dev_transfer((char *) fp->private_data,

0 commit comments

Comments
 (0)