From patchwork Fri Jul 8 23:42:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Neri X-Patchwork-Id: 646606 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) by ozlabs.org (Postfix) with ESMTP id 3rmWKB0yQpz9t0T; Sat, 9 Jul 2016 09:43:42 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1bLfQg-00051c-5L; Fri, 08 Jul 2016 23:43:30 +0000 Received: from mga11.intel.com ([192.55.52.93]) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1bLfQX-00050u-9O for fwts-devel@lists.ubuntu.com; Fri, 08 Jul 2016 23:43:21 +0000 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga102.fm.intel.com with ESMTP; 08 Jul 2016 16:43:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.28,332,1464678000"; d="scan'208"; a="1013463036" Received: from unknown (HELO luv-build.sc.intel.com) ([172.25.110.25]) by orsmga002.jf.intel.com with ESMTP; 08 Jul 2016 16:43:18 -0700 From: Ricardo Neri To: fwts-devel@lists.ubuntu.com Subject: [PATCH 2/3] lib: fwts_cpu: return info struct only when requested CPU is found Date: Fri, 8 Jul 2016 16:42:20 -0700 Message-Id: <1468021341-12067-3-git-send-email-ricardo.neri-calderon@linux.intel.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1468021341-12067-1-git-send-email-ricardo.neri-calderon@linux.intel.com> References: <1468021341-12067-1-git-send-email-ricardo.neri-calderon@linux.intel.com> X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.14 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: fwts-devel-bounces@lists.ubuntu.com If the requested CPU is not found, we would be returning an empty structure (along with NULL pointers). It makes no sense to return such useless structure. Instead, return NULL. Signed-off-by: Ricardo Neri Acked-by: Colin Ian King Acked-by: Alex Hung --- src/lib/src/fwts_cpu.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lib/src/fwts_cpu.c b/src/lib/src/fwts_cpu.c index 053f850..2e0834d 100644 --- a/src/lib/src/fwts_cpu.c +++ b/src/lib/src/fwts_cpu.c @@ -106,7 +106,7 @@ fwts_cpuinfo_x86 *fwts_cpu_get_info(const int which_cpu) FILE *fp; char buffer[1024]; fwts_cpuinfo_x86 *cpu; - int cpu_num = -1; + int cpu_num = -1, found = 0; if ((cpu = (fwts_cpuinfo_x86*)calloc(1, sizeof(fwts_cpuinfo_x86))) == NULL) return NULL; @@ -135,6 +135,8 @@ fwts_cpuinfo_x86 *fwts_cpu_get_info(const int which_cpu) continue; } + found = 1; + if (!strncmp(buffer, "vendor_id", 9)) { cpu->vendor_id = strdup(ptr); continue; @@ -162,6 +164,11 @@ fwts_cpuinfo_x86 *fwts_cpu_get_info(const int which_cpu) } fclose(fp); + if (!found) { + free(cpu); + cpu = NULL; + } + return cpu; }