From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751953AbaLXMAb (ORCPT ); Wed, 24 Dec 2014 07:00:31 -0500 Received: from hqemgate14.nvidia.com ([216.228.121.143]:14977 "EHLO hqemgate14.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751427AbaLXMA3 (ORCPT ); Wed, 24 Dec 2014 07:00:29 -0500 X-PGP-Universal: processed; by hqnvupgp07.nvidia.com on Wed, 24 Dec 2014 03:55:10 -0800 From: Rohith Seelaboyina To: , , , , CC: , , , Rohith Seelaboyina Subject: [PATCH v3] usb: gadget: ffs: Fix sparse error Date: Wed, 24 Dec 2014 17:30:01 +0530 Message-ID: <1419422401-8401-1-git-send-email-rseelaboyina@nvidia.com> X-Mailer: git-send-email 1.9.1 X-NVConfidentiality: public MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch fixes the sparse error in functionfs driver. drivers/usb/gadget/function/f_fs.c:400:44: error: bad constant experssion. Dynamic memory allocation through kcalloc is more safer than declaring variable array size, Fix this error by using kcalloc for memory allocation, Check if memory allocation is successful and return -ENOMEM on failure. Signed-off-by: Rohith Seelaboyina --- Changes in v3: - Follow kernel coding style to seperate variable declaration and assignment. Changes in v2: - Use kcalloc to allocate memory instead of using kmalloc and memset. drivers/usb/gadget/function/f_fs.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c index 63314ede7ba6..8af3f7906c83 100644 --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -397,10 +397,13 @@ static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf, * We are holding ffs->ev.waitq.lock and ffs->mutex and we need * to release them. */ - struct usb_functionfs_event events[n]; unsigned i = 0; + int ret; + struct usb_functionfs_event *events; - memset(events, 0, sizeof events); + events = kcalloc(n, sizeof(*events), GFP_KERNEL); + if (!events) + return -ENOMEM; do { events[i].type = ffs->ev.types[i]; @@ -421,8 +424,10 @@ static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf, spin_unlock_irq(&ffs->ev.waitq.lock); mutex_unlock(&ffs->mutex); - return unlikely(__copy_to_user(buf, events, sizeof events)) - ? -EFAULT : sizeof events; + ret = unlikely(__copy_to_user(buf, events, n * sizeof(*events))) + ? -EFAULT : n * sizeof(*events); + kfree(events); + return ret; } static ssize_t ffs_ep0_read(struct file *file, char __user *buf, -- 1.9.1