Message ID | 1468021341-12067-3-git-send-email-ricardo.neri-calderon@linux.intel.com |
---|---|
State | Accepted |
Headers | show |
On 09/07/16 00:42, Ricardo Neri wrote: > 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@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; > } > > Acked-by: Colin Ian King <colin.king@canonical.com>
On 2016-07-09 07:42 AM, Ricardo Neri wrote: > 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@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; > } > > Acked-by: Alex Hung <alex.hung@canonical.com>
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; }
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@linux.intel.com> --- src/lib/src/fwts_cpu.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)