[PATCH 2/3] lib: fwts_cpu: return info struct only when requested CPU is found

Ricardo Neri ricardo.neri-calderon at linux.intel.com
Fri Jul 8 23:42:20 UTC 2016


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 <ricardo.neri-calderon at linux.intel.com>
---
 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;
 }
 
-- 
2.9.0




More information about the fwts-devel mailing list