summaryrefslogtreecommitdiffstats
path: root/src/hardware
diff options
context:
space:
mode:
authorPeter Schiffer <pschiffe@redhat.com>2013-08-29 14:50:10 +0200
committerPeter Schiffer <pschiffe@redhat.com>2013-08-29 14:50:10 +0200
commit1c9b7c9781b92c4c21e62ecf47cf3413d2d2298a (patch)
tree415b2d8f300bb635cf6628ce2112aa27cb2aa373 /src/hardware
parent229d407bf7d03cda4c6b5b9fc5c1102ffc06c2f8 (diff)
downloadopenlmi-providers-1c9b7c9781b92c4c21e62ecf47cf3413d2d2298a.tar.gz
openlmi-providers-1c9b7c9781b92c4c21e62ecf47cf3413d2d2298a.tar.xz
openlmi-providers-1c9b7c9781b92c4c21e62ecf47cf3413d2d2298a.zip
Hardware: Fixed Coverity findings
Fixed Coverity findings, enhanced fail case scenarios.
Diffstat (limited to 'src/hardware')
-rw-r--r--src/hardware/LMI_ProcessorCacheMemoryProvider.c5
-rw-r--r--src/hardware/dmidecode.c72
-rw-r--r--src/hardware/lscpu.c8
-rw-r--r--src/hardware/procfs.c32
-rw-r--r--src/hardware/sysfs.c50
-rw-r--r--src/hardware/utils.c29
6 files changed, 121 insertions, 75 deletions
diff --git a/src/hardware/LMI_ProcessorCacheMemoryProvider.c b/src/hardware/LMI_ProcessorCacheMemoryProvider.c
index 9fe0230..2791b95 100644
--- a/src/hardware/LMI_ProcessorCacheMemoryProvider.c
+++ b/src/hardware/LMI_ProcessorCacheMemoryProvider.c
@@ -80,11 +80,8 @@ static CMPIStatus LMI_ProcessorCacheMemoryEnumInstances(
if (dmi_cpu_caches_nb > 0) {
caches = dmi_cpu_caches_nb;
- } else if (sysfs_cpu_caches_nb > 0) {
- caches = sysfs_cpu_caches_nb;
} else {
- error_msg = "Unable to get processor cache information.";
- goto done;
+ caches = sysfs_cpu_caches_nb;
}
for (i = 0; i < caches; i++) {
diff --git a/src/hardware/dmidecode.c b/src/hardware/dmidecode.c
index d246f8e..84ce0c1 100644
--- a/src/hardware/dmidecode.c
+++ b/src/hardware/dmidecode.c
@@ -159,7 +159,7 @@ short dmi_get_processors(DmiProcessor **cpus, unsigned *cpus_nb)
unsigned i, buffer_size = 0;
char **buffer = NULL, *buf;
- *cpus_nb = 0;
+ dmi_free_processors(cpus, cpus_nb);
/* get dmidecode output */
if (run_command("dmidecode -t 4", &buffer, &buffer_size) != 0) {
@@ -185,6 +185,7 @@ short dmi_get_processors(DmiProcessor **cpus, unsigned *cpus_nb)
*cpus = (DmiProcessor *)calloc(*cpus_nb, sizeof(DmiProcessor));
if (!(*cpus)) {
warn("Failed to allocate memory.");
+ *cpus_nb = 0;
ret = -4;
goto done;
}
@@ -236,24 +237,30 @@ short dmi_get_processors(DmiProcessor **cpus, unsigned *cpus_nb)
}
/* Current Speed */
buf = copy_string_part_after_delim(buffer[i], "Current Speed: ");
- if (buf && strcmp(buf, "Unknown") != 0) {
- sscanf(buf, "%u", &(*cpus)[curr_cpu].current_speed);
+ if (buf) {
+ if (strcmp(buf, "Unknown") != 0) {
+ sscanf(buf, "%u", &(*cpus)[curr_cpu].current_speed);
+ }
free(buf);
buf = NULL;
continue;
}
/* Max Speed */
buf = copy_string_part_after_delim(buffer[i], "Max Speed: ");
- if (buf && strcmp(buf, "Unknown") != 0) {
- sscanf(buf, "%u", &(*cpus)[curr_cpu].max_speed);
+ if (buf) {
+ if (strcmp(buf, "Unknown") != 0) {
+ sscanf(buf, "%u", &(*cpus)[curr_cpu].max_speed);
+ }
free(buf);
buf = NULL;
continue;
}
/* External Clock Speed */
buf = copy_string_part_after_delim(buffer[i], "External Clock: ");
- if (buf && strcmp(buf, "Unknown") != 0) {
- sscanf(buf, "%u", &(*cpus)[curr_cpu].external_clock);
+ if (buf) {
+ if (strcmp(buf, "Unknown") != 0) {
+ sscanf(buf, "%u", &(*cpus)[curr_cpu].external_clock);
+ }
free(buf);
buf = NULL;
continue;
@@ -358,6 +365,7 @@ short dmi_get_processors(DmiProcessor **cpus, unsigned *cpus_nb)
(char **)calloc((*cpus)[curr_cpu].charact_nb, sizeof(char *));
if (!(*cpus)[curr_cpu].characteristics) {
warn("Failed to allocate memory.");
+ (*cpus)[curr_cpu].charact_nb = 0;
ret = -5;
goto done;
}
@@ -405,7 +413,7 @@ void dmi_free_processors(DmiProcessor **cpus, unsigned *cpus_nb)
{
unsigned i, j;
- if (*cpus_nb > 0) {
+ if (*cpus && *cpus_nb > 0) {
for (i = 0; i < *cpus_nb; i++) {
free((*cpus)[i].id);
(*cpus)[i].id = NULL;
@@ -422,7 +430,7 @@ void dmi_free_processors(DmiProcessor **cpus, unsigned *cpus_nb)
free((*cpus)[i].upgrade);
(*cpus)[i].upgrade = NULL;
- if ((*cpus)[i].charact_nb > 0) {
+ if ((*cpus)[i].characteristics && (*cpus)[i].charact_nb > 0) {
for (j = 0; j < (*cpus)[i].charact_nb; j++) {
free((*cpus)[i].characteristics[j]);
(*cpus)[i].characteristics[j] = NULL;
@@ -536,7 +544,7 @@ short dmi_get_cpu_caches(DmiCpuCache **caches, unsigned *caches_nb)
unsigned i, buffer_size = 0;
char **buffer = NULL, *buf;
- *caches_nb = 0;
+ dmi_free_cpu_caches(caches, caches_nb);
/* get dmidecode output */
if (run_command("dmidecode -t 7", &buffer, &buffer_size) != 0) {
@@ -562,6 +570,7 @@ short dmi_get_cpu_caches(DmiCpuCache **caches, unsigned *caches_nb)
*caches = (DmiCpuCache *)calloc(*caches_nb, sizeof(DmiCpuCache));
if (!(*caches)) {
warn("Failed to allocate memory.");
+ *caches_nb = 0;
ret = -4;
goto done;
}
@@ -677,7 +686,7 @@ void dmi_free_cpu_caches(DmiCpuCache **caches, unsigned *caches_nb)
{
unsigned i;
- if (*caches_nb > 0) {
+ if (*caches && *caches_nb > 0) {
for (i = 0; i < *caches_nb; i++) {
free((*caches)[i].id);
(*caches)[i].id = NULL;
@@ -867,6 +876,7 @@ short dmi_get_memory(DmiMemory *memory)
memory->modules = (DmiMemoryModule *)calloc(memory->modules_nb, sizeof(DmiMemoryModule));
if (!memory->modules) {
warn("Failed to allocate memory.");
+ memory->modules_nb = 0;
ret = -4;
goto done;
}
@@ -1030,6 +1040,7 @@ short dmi_get_memory(DmiMemory *memory)
memory->slots = (DmiMemorySlot *)calloc(memory->slots_nb, sizeof(DmiMemorySlot));
if (!memory->slots) {
warn("Failed to allocate memory.");
+ memory->slots_nb = 0;
ret = -10;
goto done;
}
@@ -1129,7 +1140,11 @@ void dmi_free_memory(DmiMemory *memory)
{
unsigned i;
- if (memory->modules_nb > 0) {
+ if (!memory) {
+ return;
+ }
+
+ if (memory->modules && memory->modules_nb > 0) {
for (i = 0; i < memory->modules_nb; i++) {
free(memory->modules[i].serial_number);
memory->modules[i].serial_number = NULL;
@@ -1148,7 +1163,7 @@ void dmi_free_memory(DmiMemory *memory)
}
free(memory->modules);
}
- if (memory->slots_nb > 0) {
+ if (memory->slots && memory->slots_nb > 0) {
for (i = 0; i < memory->slots_nb; i++) {
free(memory->slots[i].name);
memory->slots[i].name = NULL;
@@ -1396,6 +1411,10 @@ done:
void dmi_free_chassis(DmiChassis *chassis)
{
+ if (!chassis) {
+ return;
+ }
+
free(chassis->serial_number);
chassis->serial_number = NULL;
free(chassis->type);
@@ -1567,6 +1586,10 @@ done:
void dmi_free_baseboard(DmiBaseboard *baseboard)
{
+ if (!baseboard) {
+ return;
+ }
+
free(baseboard->serial_number);
baseboard->serial_number = NULL;
free(baseboard->manufacturer);
@@ -1638,7 +1661,7 @@ short dmi_get_ports(DmiPort **ports, unsigned *ports_nb)
unsigned i, buffer_size = 0;
char **buffer = NULL, *buf;
- *ports_nb = 0;
+ dmi_free_ports(ports, ports_nb);
/* get dmidecode output */
if (run_command("dmidecode -t 8", &buffer, &buffer_size) != 0) {
@@ -1664,6 +1687,7 @@ short dmi_get_ports(DmiPort **ports, unsigned *ports_nb)
*ports = (DmiPort *)calloc(*ports_nb, sizeof(DmiPort));
if (!(*ports)) {
warn("Failed to allocate memory.");
+ *ports_nb = 0;
ret = -4;
goto done;
}
@@ -1726,7 +1750,7 @@ void dmi_free_ports(DmiPort **ports, unsigned *ports_nb)
{
unsigned i;
- if (*ports_nb > 0) {
+ if (*ports && *ports_nb > 0) {
for (i = 0; i < *ports_nb; i++) {
free((*ports)[i].name);
(*ports)[i].name = NULL;
@@ -1804,9 +1828,9 @@ short dmi_get_system_slots(DmiSystemSlot **slots, unsigned *slots_nb)
short ret = -1;
int curr_slot = -1;
unsigned i, buffer_size = 0;
- char **buffer = NULL, *buf;
+ char **buffer = NULL, *buf = NULL;
- *slots_nb = 0;
+ dmi_free_system_slots(slots, slots_nb);
/* get dmidecode output */
if (run_command("dmidecode -t 9", &buffer, &buffer_size) != 0) {
@@ -1832,6 +1856,7 @@ short dmi_get_system_slots(DmiSystemSlot **slots, unsigned *slots_nb)
*slots = (DmiSystemSlot *)calloc(*slots_nb, sizeof(DmiSystemSlot));
if (!(*slots)) {
warn("Failed to allocate memory.");
+ *slots_nb = 0;
ret = -4;
goto done;
}
@@ -1924,6 +1949,7 @@ short dmi_get_system_slots(DmiSystemSlot **slots, unsigned *slots_nb)
done:
free_2d_buffer(&buffer, &buffer_size);
+ free(buf);
if (ret != 0) {
dmi_free_system_slots(slots, slots_nb);
@@ -1936,7 +1962,7 @@ void dmi_free_system_slots(DmiSystemSlot **slots, unsigned *slots_nb)
{
unsigned i;
- if (*slots_nb > 0) {
+ if (*slots && *slots_nb > 0) {
for (i = 0; i < *slots_nb; i++) {
free((*slots)[i].name);
(*slots)[i].name = NULL;
@@ -2000,7 +2026,7 @@ short dmi_get_pointing_devices(DmiPointingDevice **devices, unsigned *devices_nb
unsigned i, buffer_size = 0;
char **buffer = NULL, *buf;
- *devices_nb = 0;
+ dmi_free_pointing_devices(devices, devices_nb);
/* get dmidecode output */
if (run_command("dmidecode -t 21", &buffer, &buffer_size) != 0) {
@@ -2026,6 +2052,7 @@ short dmi_get_pointing_devices(DmiPointingDevice **devices, unsigned *devices_nb
*devices = (DmiPointingDevice *)calloc(*devices_nb, sizeof(DmiPointingDevice));
if (!(*devices)) {
warn("Failed to allocate memory.");
+ *devices_nb = 0;
ret = -4;
goto done;
}
@@ -2082,7 +2109,7 @@ void dmi_free_pointing_devices(DmiPointingDevice **devices, unsigned *devices_nb
{
unsigned i;
- if (*devices_nb > 0) {
+ if (*devices && *devices_nb > 0) {
for (i = 0; i < *devices_nb; i++) {
free((*devices)[i].type);
(*devices)[i].type = NULL;
@@ -2185,7 +2212,7 @@ short dmi_get_batteries(DmiBattery **batteries, unsigned *batteries_nb)
unsigned i, buffer_size = 0;
char **buffer = NULL, *buf;
- *batteries_nb = 0;
+ dmi_free_batteries(batteries, batteries_nb);
/* get dmidecode output */
if (run_command("dmidecode -t 22", &buffer, &buffer_size) != 0) {
@@ -2211,6 +2238,7 @@ short dmi_get_batteries(DmiBattery **batteries, unsigned *batteries_nb)
*batteries = (DmiBattery *)calloc(*batteries_nb, sizeof(DmiBattery));
if (!(*batteries)) {
warn("Failed to allocate memory.");
+ *batteries_nb = 0;
ret = -4;
goto done;
}
@@ -2329,7 +2357,7 @@ void dmi_free_batteries(DmiBattery **batteries, unsigned *batteries_nb)
{
unsigned i;
- if (*batteries_nb > 0) {
+ if (*batteries && *batteries_nb > 0) {
for (i = 0; i < *batteries_nb; i++) {
free((*batteries)[i].name);
(*batteries)[i].name = NULL;
diff --git a/src/hardware/lscpu.c b/src/hardware/lscpu.c
index 3a2a420..be00f87 100644
--- a/src/hardware/lscpu.c
+++ b/src/hardware/lscpu.c
@@ -149,10 +149,10 @@ done:
void lscpu_free_processor(LscpuProcessor *cpus)
{
- if (cpus) {
- free(cpus->stepping);
- cpus->stepping = NULL;
+ if (!cpus) {
+ return;
}
- return;
+ free(cpus->stepping);
+ cpus->stepping = NULL;
}
diff --git a/src/hardware/procfs.c b/src/hardware/procfs.c
index 288c8a9..3c01cd4 100644
--- a/src/hardware/procfs.c
+++ b/src/hardware/procfs.c
@@ -63,7 +63,7 @@ short cpuinfo_get_processor(CpuinfoProcessor *cpu)
{
short ret = -1;
unsigned i, buffer_size = 0;
- char **buffer = NULL, *buf;
+ char **buffer = NULL, *buf = NULL;
/* read /proc/cpuinfo file */
if (read_file("/proc/cpuinfo", &buffer, &buffer_size) != 0) {
@@ -112,6 +112,7 @@ short cpuinfo_get_processor(CpuinfoProcessor *cpu)
done:
free_2d_buffer(&buffer, &buffer_size);
+ free(buf);
if (ret != 0) {
cpuinfo_free_processor(cpu);
@@ -122,23 +123,24 @@ done:
void cpuinfo_free_processor(CpuinfoProcessor *cpu)
{
- if (cpu) {
- if (cpu->flags_nb > 0) {
- unsigned i;
- for (i = 0; i < cpu->flags_nb; i++) {
- free(cpu->flags[i]);
- cpu->flags[i] = NULL;
- }
- free(cpu->flags);
- }
- cpu->flags_nb = 0;
- cpu->flags = NULL;
+ unsigned i;
- free(cpu->model_name);
- cpu->model_name = NULL;
+ if (!cpu) {
+ return;
}
- return;
+ if (cpu->flags && cpu->flags_nb > 0) {
+ for (i = 0; i < cpu->flags_nb; i++) {
+ free(cpu->flags[i]);
+ cpu->flags[i] = NULL;
+ }
+ free(cpu->flags);
+ }
+ cpu->flags_nb = 0;
+ cpu->flags = NULL;
+
+ free(cpu->model_name);
+ cpu->model_name = NULL;
}
unsigned long meminfo_get_memory_size()
diff --git a/src/hardware/sysfs.c b/src/hardware/sysfs.c
index 565269f..084a11d 100644
--- a/src/hardware/sysfs.c
+++ b/src/hardware/sysfs.c
@@ -159,6 +159,12 @@ short copy_sysfs_cpu_cache(SysfsCpuCache *to, const SysfsCpuCache from)
if (!to->id || !to->name || !to->type) {
warn("Failed to allocate memory.");
+ free(to->id);
+ to->id = NULL;
+ free(to->name);
+ to->name = NULL;
+ free(to->type);
+ to->type = NULL;
return -1;
}
@@ -176,6 +182,25 @@ short sysfs_get_cpu_caches(SysfsCpuCache **caches, unsigned *caches_nb)
*caches_nb = 0;
+ /* get processor information */
+ if (dmi_get_processors(&dmi_cpus, &dmi_cpus_nb) != 0 || dmi_cpus_nb < 1) {
+ dmi_free_processors(&dmi_cpus, &dmi_cpus_nb);
+
+ if (lscpu_get_processor(&lscpu) != 0) {
+ ret = -11;
+ goto done;
+ }
+ }
+ if (dmi_cpus_nb > 0) {
+ cpus_nb = dmi_cpus_nb;
+ } else if (lscpu.processors > 0) {
+ cpus_nb = lscpu.processors;
+ } else {
+ warn("No processor found.");
+ ret = -12;
+ goto done;
+ }
+
/* count caches */
DIR *dir;
char *cache_dir = SYSFS_CPU_PATH "/cpu0/cache";
@@ -201,29 +226,11 @@ short sysfs_get_cpu_caches(SysfsCpuCache **caches, unsigned *caches_nb)
goto done;
}
- /* get processor information */
- if (dmi_get_processors(&dmi_cpus, &dmi_cpus_nb) != 0 || dmi_cpus_nb < 1) {
- dmi_free_processors(&dmi_cpus, &dmi_cpus_nb);
-
- if (lscpu_get_processor(&lscpu) != 0) {
- ret = -11;
- goto done;
- }
- }
- if (dmi_cpus_nb > 0) {
- cpus_nb = dmi_cpus_nb;
- } else if (lscpu.processors > 0) {
- cpus_nb = lscpu.processors;
- } else {
- warn("No processor found.");
- ret = -12;
- goto done;
- }
-
/* allocate memory for caches */
*caches = (SysfsCpuCache *)calloc(*caches_nb * cpus_nb, sizeof(SysfsCpuCache));
if (!(*caches)) {
warn("Failed to allocate memory.");
+ *caches_nb = 0;
ret = -4;
goto done;
}
@@ -336,7 +343,7 @@ void sysfs_free_cpu_caches(SysfsCpuCache **caches, unsigned *caches_nb)
{
unsigned i;
- if (*caches_nb > 0) {
+ if (*caches && *caches_nb > 0) {
for (i = 0; i < *caches_nb; i++) {
free((*caches)[i].id);
(*caches)[i].id = NULL;
@@ -366,7 +373,7 @@ short sysfs_has_numa()
short sysfs_get_sizes_of_hugepages(unsigned **sizes, unsigned *sizes_nb)
{
short ret = -1;
- DIR *dir;
+ DIR *dir = NULL;
*sizes_nb = 0;
*sizes = NULL;
@@ -398,6 +405,7 @@ short sysfs_get_sizes_of_hugepages(unsigned **sizes, unsigned *sizes_nb)
*sizes = (unsigned *)calloc(*sizes_nb, sizeof(unsigned));
if (!(*sizes)) {
warn("Failed to allocate memory.");
+ *sizes_nb = 0;
ret = -4;
goto done;
}
diff --git a/src/hardware/utils.c b/src/hardware/utils.c
index 1d80411..f23be27 100644
--- a/src/hardware/utils.c
+++ b/src/hardware/utils.c
@@ -26,8 +26,8 @@ short read_fp_to_2d_buffer(FILE *fp, char ***buffer, unsigned *buffer_size)
short ret = -1;
ssize_t read;
size_t line_len = 0;
- unsigned tmp_buffer_lines, lines_read = 0;
- char **tmp_buffer, *line = NULL;
+ unsigned tmp_buffer_lines = 0, lines_read = 0;
+ char **tmp_buffer = NULL, *line = NULL;
free_2d_buffer(buffer, buffer_size);
@@ -42,6 +42,7 @@ short read_fp_to_2d_buffer(FILE *fp, char ***buffer, unsigned *buffer_size)
tmp_buffer = (char **)calloc(tmp_buffer_lines, sizeof(char *));
if (!tmp_buffer) {
warn("Failed to allocate memory.");
+ tmp_buffer_lines = 0;
ret = -3;
goto done;
}
@@ -59,6 +60,7 @@ short read_fp_to_2d_buffer(FILE *fp, char ***buffer, unsigned *buffer_size)
tmp_buffer_lines * sizeof(char *));
if (!newtmp) {
warn("Failed to allocate memory.");
+ tmp_buffer_lines /= 2;
ret = -4;
goto done;
}
@@ -86,15 +88,15 @@ short read_fp_to_2d_buffer(FILE *fp, char ***buffer, unsigned *buffer_size)
/* reallocate buffer to free unused space */
if (tmp_buffer_lines > lines_read) {
- tmp_buffer_lines = lines_read;
char **newtmp = (char **)realloc(tmp_buffer,
- tmp_buffer_lines * sizeof(char *));
+ lines_read * sizeof(char *));
if (!newtmp) {
warn("Failed to allocate memory.");
ret = -7;
goto done;
}
tmp_buffer = newtmp;
+ tmp_buffer_lines = lines_read;
}
*buffer_size = tmp_buffer_lines;
@@ -117,7 +119,7 @@ void free_2d_buffer(char ***buffer, unsigned *buffer_size)
unsigned i, tmp_buffer_lines = *buffer_size;
char **tmp_buffer = *buffer;
- if (tmp_buffer_lines > 0) {
+ if (tmp_buffer && tmp_buffer_lines > 0) {
for (i = 0; i < tmp_buffer_lines; i++) {
free(tmp_buffer[i]);
tmp_buffer[i] = NULL;
@@ -132,7 +134,7 @@ void free_2d_buffer(char ***buffer, unsigned *buffer_size)
short run_command(const char *command, char ***buffer, unsigned *buffer_size)
{
- FILE *fp;
+ FILE *fp = NULL;
short ret = -1;
/* if command is empty */
@@ -185,9 +187,16 @@ done:
short read_file(const char *filename, char ***buffer, unsigned *buffer_size)
{
- FILE *fp;
+ FILE *fp = NULL;
short ret = -1;
+ /* if filename is empty */
+ if (!filename || strlen(filename) < 1) {
+ warn("Given file name is empty.");
+ ret = -4;
+ goto done;
+ }
+
/* open file */
debug("Reading \"%s\" file.", filename);
fp = fopen(filename, "r");
@@ -307,6 +316,7 @@ short explode(const char *str, const char *delims, char ***buffer, unsigned *buf
tmp_buffer = (char **)calloc(tmp_buffer_size, sizeof(char *));
if (!tmp_buffer) {
warn("Failed to allocate memory.");
+ tmp_buffer_size = 0;
ret = -2;
goto done;
}
@@ -329,6 +339,7 @@ short explode(const char *str, const char *delims, char ***buffer, unsigned *buf
tmp_buffer_size * sizeof(char *));
if (!new_temp) {
warn("Failed to allocate memory.");
+ tmp_buffer_size /= 2;
ret = -3;
goto done;
}
@@ -347,15 +358,15 @@ short explode(const char *str, const char *delims, char ***buffer, unsigned *buf
/* reallocate to save unused space */
if (tmp_buffer_size > item) {
- tmp_buffer_size = item;
char **new_temp = (char **)realloc(tmp_buffer,
- tmp_buffer_size * sizeof(char *));
+ item * sizeof(char *));
if (!new_temp) {
warn("Failed to allocate memory.");
ret = -5;
goto done;
}
tmp_buffer = new_temp;
+ tmp_buffer_size = item;
}
*buffer_size = tmp_buffer_size;