diff --git a/src/arm/soc.c b/src/arm/soc.c index 9dc278a..9e05697 100644 --- a/src/arm/soc.c +++ b/src/arm/soc.c @@ -33,8 +33,7 @@ static char* soc_rpi_string[] = { char* toupperstr(char* str) { int len = strlen(str) + 1; - char* ret = emalloc(sizeof(char) * len); - memset(ret, 0, sizeof(char) * len); + char* ret = ecalloc(len, sizeof(char)); for(int i=0; i < len; i++) { ret[i] = toupper((unsigned char) str[i]); diff --git a/src/common/args.c b/src/common/args.c index 719cd7c..635a30f 100644 --- a/src/common/args.c +++ b/src/common/args.c @@ -225,8 +225,7 @@ bool parse_color(char* optarg_str, struct color*** cs) { char* build_short_options(void) { const char *c = args_chr; int len = sizeof(args_chr) / sizeof(args_chr[0]); - char* str = (char *) emalloc(sizeof(char) * (len*2 + 1)); - memset(str, 0, sizeof(char) * (len*2 + 1)); + char* str = (char *) ecalloc(len*2 + 1, sizeof(char)); #ifdef ARCH_X86 sprintf(str, "%c:%c:%c%c%c%c%c%c%c%c%c%c%c%c", diff --git a/src/common/pci.c b/src/common/pci.c index 620b04a..c480ee7 100644 --- a/src/common/pci.c +++ b/src/common/pci.c @@ -66,7 +66,7 @@ struct pci_devices * get_pci_paths(void) { if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { int strLen = min(MAX_LENGTH_PCI_DIR_NAME, strlen(dp->d_name)) + 1; pci->devices[i] = emalloc(sizeof(struct pci_device)); - pci->devices[i]->path = ecalloc(sizeof(char), strLen); + pci->devices[i]->path = ecalloc(strLen, sizeof(char)); strncpy(pci->devices[i]->path, dp->d_name, strLen); i++; } diff --git a/src/common/soc.c b/src/common/soc.c index 85ab1f8..93d2e72 100644 --- a/src/common/soc.c +++ b/src/common/soc.c @@ -42,9 +42,9 @@ char* get_str_process(struct system_on_chip* soc) { snprintf(str, strlen(STRING_UNKNOWN)+1, STRING_UNKNOWN); } else { - str = emalloc(sizeof(char) * 5); - memset(str, 0, sizeof(char) * 5); - snprintf(str, 5, "%dnm", soc->process); + int max_process_len = 5 + 1; + str = ecalloc(max_process_len, sizeof(char)); + snprintf(str, max_process_len, "%dnm", soc->process); } return str; } @@ -70,10 +70,8 @@ void fill_soc(struct system_on_chip* soc, char* soc_name, SOC soc_model, int32_t snprintf(soc->raw_name, strlen(STRING_UNKNOWN)+1, STRING_UNKNOWN); } else { - soc->process = process; int len = strlen(soc_name) + strlen(soc_trademark_string[soc->vendor]) + 1; soc->name = emalloc(sizeof(char) * len); - memset(soc->name, 0, sizeof(char) * len); sprintf(soc->name, "%s%s", soc_trademark_string[soc->vendor], soc_name); } } diff --git a/src/common/udev.c b/src/common/udev.c index d7eb1f3..4683cda 100644 --- a/src/common/udev.c +++ b/src/common/udev.c @@ -146,8 +146,7 @@ char* get_field_from_cpuinfo(char* CPUINFO_FIELD) { char* tmp2 = strstr(tmp1, "\n"); int strlen = (1 + (tmp2-tmp1)); - char* hardware = emalloc(sizeof(char) * strlen); - memset(hardware, 0, sizeof(char) * strlen); + char* hardware = ecalloc(strlen, sizeof(char)); strncpy(hardware, tmp1, tmp2-tmp1); return hardware; diff --git a/src/ppc/udev.c b/src/ppc/udev.c index 601a0b1..ffa26d2 100644 --- a/src/ppc/udev.c +++ b/src/ppc/udev.c @@ -13,7 +13,7 @@ bool fill_array_from_sys(int *core_ids, int total_cores, char* SYS_PATH) { char* buf; char* end; char path[128]; - memset(path, 0, 128); + memset(name, 0, sizeof(char) * 128); for(int i=0; i < total_cores; i++) { sprintf(path, "%s%s/cpu%d/%s", _PATH_SYS_SYSTEM, _PATH_SYS_CPU, i, SYS_PATH); diff --git a/src/riscv/riscv.c b/src/riscv/riscv.c index b378f30..df0073a 100644 --- a/src/riscv/riscv.c +++ b/src/riscv/riscv.c @@ -100,9 +100,8 @@ struct extensions* get_extensions_from_str(char* str) { return ext; } - int len = sizeof(char) * (strlen(str)+1); - ext->str = emalloc(sizeof(char) * len); - memset(ext->str, 0, len); + int len = strlen(str); + ext->str = ecalloc(len+1, sizeof(char)); strncpy(ext->str, str, sizeof(char) * len); // Code inspired in Linux kernel (riscv_fill_hwcap): diff --git a/src/riscv/udev.c b/src/riscv/udev.c index 20254fa..d1a6d31 100644 --- a/src/riscv/udev.c +++ b/src/riscv/udev.c @@ -40,8 +40,7 @@ char* get_field_from_devtree(int DEVTREE_FIELD) { tmp1++; int strlen = filelen-(tmp1-buf); - char* hardware = emalloc(sizeof(char) * strlen); - memset(hardware, 0, sizeof(char) * strlen); + char* hardware = ecalloc(strlen, sizeof(char)); strncpy(hardware, tmp1, strlen-1); return hardware; @@ -70,9 +69,8 @@ char* parse_cpuinfo_field(char* field_str) { } int ret_strlen = (end-tmp); - char* ret = emalloc(sizeof(char) * (ret_strlen+1)); - memset(ret, 0, sizeof(char) * (ret_strlen+1)); - strncpy(ret, tmp, ret_strlen); + char* ret = ecalloc(ret_strlen+1, sizeof(char)); + strncpy(ret, tmp, sizeof(char) * ret_strlen); return ret; } diff --git a/src/x86/apic.c b/src/x86/apic.c index 98dd1e4..b41e2f3 100644 --- a/src/x86/apic.c +++ b/src/x86/apic.c @@ -234,15 +234,11 @@ uint32_t max_apic_id_size(uint32_t** cache_id_apic, struct topology* topo) { bool build_topo_from_apic(uint32_t* apic_pkg, uint32_t* apic_smt, uint32_t** cache_id_apic, struct topology* topo) { uint32_t size = max_apic_id_size(cache_id_apic, topo); - uint32_t* sockets = emalloc(sizeof(uint32_t) * size); - uint32_t* smt = emalloc(sizeof(uint32_t) * size); - uint32_t* apic_id = emalloc(sizeof(uint32_t) * size); + uint32_t* sockets = ecalloc(size, sizeof(uint32_t)); + uint32_t* smt = ecalloc(size, sizeof(uint32_t)); + uint32_t* apic_id = ecalloc(size, sizeof(uint32_t)); uint32_t num_caches = 0; - memset(sockets, 0, sizeof(uint32_t) * size); - memset(smt, 0, sizeof(uint32_t) * size); - memset(apic_id, 0, sizeof(uint32_t) * size); - // System topology for(int i=0; i < topo->total_cores_module; i++) { sockets[apic_pkg[i]] = 1; diff --git a/src/x86/cpuid.c b/src/x86/cpuid.c index 54a120d..dd67ee0 100644 --- a/src/x86/cpuid.c +++ b/src/x86/cpuid.c @@ -91,8 +91,7 @@ char* get_str_cpu_name_internal(void) { uint32_t edx = 0; uint32_t c = 0; - char * name = emalloc(sizeof(char) * CPU_NAME_MAX_LENGTH); - memset(name, 0, CPU_NAME_MAX_LENGTH); + char * name = ecalloc(CPU_NAME_MAX_LENGTH, sizeof(char)); for(int i=0; i < 3; i++) { eax = 0x80000002 + i; @@ -281,7 +280,7 @@ struct hypervisor* get_hp_info(bool hv_present) { } else { char name[13]; - memset(name, 0, 13); + memset(name, 0, sizeof(char) * 13); get_name_cpuid(name, ebx, ecx, edx); bool found = false; @@ -471,7 +470,7 @@ struct cpuInfo* get_cpu_info(void) { //Fill vendor char name[13]; - memset(name,0,13); + memset(name, 0, sizeof(char) * 13); get_name_cpuid(name, ebx, edx, ecx); if(strcmp(CPU_VENDOR_INTEL_STRING,name) == 0)