From patchwork Wed Sep 30 16:30:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Roper X-Patchwork-Id: 7300401 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 8B240BEEA4 for ; Wed, 30 Sep 2015 16:31:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AD33F20425 for ; Wed, 30 Sep 2015 16:31:06 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 149C4204EC for ; Wed, 30 Sep 2015 16:31:05 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C0EF36E3B4; Wed, 30 Sep 2015 09:31:03 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by gabe.freedesktop.org (Postfix) with ESMTP id 6A7B76E3B4 for ; Wed, 30 Sep 2015 09:31:02 -0700 (PDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP; 30 Sep 2015 09:31:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,613,1437462000"; d="scan'208";a="816360423" Received: from mdroper-hswdev.fm.intel.com (HELO mdroper-hswdev) ([10.1.134.215]) by fmsmga002.fm.intel.com with ESMTP; 30 Sep 2015 09:31:01 -0700 Received: from mattrope by mdroper-hswdev with local (Exim 4.84) (envelope-from ) id 1ZhKHV-0007pl-J2; Wed, 30 Sep 2015 09:31:01 -0700 From: Matt Roper To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm] xf86drm: Fix error handling for drmGetDevices() Date: Wed, 30 Sep 2015 09:30:51 -0700 Message-Id: <1443630651-30077-1-git-send-email-matthew.d.roper@intel.com> X-Mailer: git-send-email 2.1.4 Cc: Emil Velikov X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If the opendir() call in drmGetDevices() returns failure, we jump to an error label that calls closedir() and then returns. However this means that we're calling closedir(NULL) which may not be safe on all implementations. We are also leaking the local_devices array that was allocated before the opendir() call. Fix both of these issues by jumping to an earlier error label (to free local_devices) and guarding the closedir() call with a NULL test. Cc: Emil Velikov Signed-off-by: Matt Roper --- xf86drm.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index c1cab1b..763d710 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -3209,7 +3209,7 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices) sysdir = opendir(DRM_DIR_NAME); if (!sysdir) { ret = -errno; - goto close_sysdir; + goto free_locals; } i = 0; @@ -3280,9 +3280,10 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices) free_devices: drmFreeDevices(local_devices, i); +free_locals: free(local_devices); -close_sysdir: - closedir(sysdir); + if (sysdir) + closedir(sysdir); return ret; }