Add release and debug targets in Makefile. Improve robustness by checking cache sizes and snprintf returns

This commit is contained in:
Dr-Noob
2020-06-21 13:37:44 +02:00
parent 0087453eda
commit 1435978b37
3 changed files with 79 additions and 16 deletions

View File

@@ -1,6 +1,6 @@
CXX=gcc CXX=gcc
CXXFLAGS=-g -Wall -Wextra -Werror -fstack-protector-all -pedantic -Wno-unused CXXFLAGS=-Wall -Wextra -Werror -fstack-protector-all -pedantic -Wno-unused
SANITY_FLAGS=-Wfloat-equal -Wshadow -Wpointer-arith -Wstrict-overflow=5 -Wformat=2 SANITY_FLAGS=-Wfloat-equal -Wshadow -Wpointer-arith -Wstrict-overflow=5 -Wformat=2
SRC_DIR=src/ SRC_DIR=src/
@@ -9,10 +9,19 @@ HEADERS=$(SRC_DIR)standart.h $(SRC_DIR)extended.h $(SRC_DIR)cpuid.h $(SRC_DIR)pr
OUTPUT=cpufetch OUTPUT=cpufetch
all: $(OUTPUT)
debug: CXXFLAGS += -g -O0
debug: $(OUTPUT)
release: CXXFLAGS += -static -O3
release: $(OUTPUT)
$(OUTPUT): Makefile $(SOURCE) $(HEADERS) $(OUTPUT): Makefile $(SOURCE) $(HEADERS)
$(CXX) $(CXXFLAGS) $(SANITY_FLAGS) $(SOURCE) -o $(OUTPUT) $(CXX) $(CXXFLAGS) $(SANITY_FLAGS) $(SOURCE) -o $(OUTPUT)
run: run:
./$(OUTPUT) ./$(OUTPUT)
clean: clean:
@rm $(OUTPUT) @rm $(OUTPUT)

View File

@@ -2,10 +2,20 @@
#include <stdio.h> #include <stdio.h>
#include "global.h" #include "global.h"
#ifdef _WIN32
#define RED ""
#define BOLD ""
#define RESET ""
#else
#define RED "\x1b[31;1m" #define RED "\x1b[31;1m"
#define BOLD "\x1b[;1m" #define BOLD "\x1b[;1m"
#define RESET "\x1b[0m" #define RESET "\x1b[0m"
#endif
#define LOG_LEVEL_NORMAL 0 #define LOG_LEVEL_NORMAL 0
#define LOG_LEVEL_VERBOSE 1 #define LOG_LEVEL_VERBOSE 1

View File

@@ -19,6 +19,7 @@
#define STRING_MEGAHERZ "MHz" #define STRING_MEGAHERZ "MHz"
#define STRING_GIGAHERZ "GHz" #define STRING_GIGAHERZ "GHz"
#define STRING_KILOBYTES "KB" #define STRING_KILOBYTES "KB"
#define STRING_MEGABYTES "MB"
#define UNKNOWN -1 #define UNKNOWN -1
@@ -313,6 +314,25 @@ struct cache* get_cache_info(struct cpuInfo* cpu) {
} }
} }
// Sanity checks. If we read values greater than this, they can't be valid ones
// The values were chosen by me
if(cach->L1i > 64 * 1024) {
printBug("Invalid L1i size: %dKB\n", cach->L1i/1024);
return NULL;
}
if(cach->L1d > 64 * 1024) {
printBug("Invalid L1d size: %dKB\n", cach->L1d/1024);
return NULL;
}
if(cach->L2 > 2 * 1048576) {
printBug("Invalid L2 size: %dMB\n", cach->L2/(1048576));
return NULL;
}
if(cach->L3 > 100 * 1048576) {
printBug("Invalid L3 size: %dMB\n", cach->L3/(1048576));
return NULL;
}
return cach; return cach;
} }
@@ -534,39 +554,63 @@ char* get_str_sha(struct cpuInfo* cpu) {
// String functions // String functions
char* get_str_l1(struct cache* cach) { char* get_str_l1(struct cache* cach) {
//Max 2 digits,2 for 'KB',3 for '(D)' and '(I)' // 2*2 for digits, 4 for two 'KB' and 6 for '(D)' and '(I)'
int size = (2*(2+2)+6+1); int size = (2*2+4+6+1);
int sanity_ret;
char* string = malloc(sizeof(char)*size); char* string = malloc(sizeof(char)*size);
snprintf(string,size,"%d"STRING_KILOBYTES"(D)%d"STRING_KILOBYTES"(I)",cach->L1d/1024,cach->L1i/1024); sanity_ret = snprintf(string,size,"%d"STRING_KILOBYTES"(D)%d"STRING_KILOBYTES"(I)",cach->L1d/1024,cach->L1i/1024);
assert(sanity_ret > 0);
return string; return string;
} }
char* get_str_l2(struct cache* cach) { char* get_str_l2(struct cache* cach) {
if(cach->L2 == UNKNOWN) { if(cach->L2 == UNKNOWN) {
char* string = malloc(sizeof(char)*5); char* string = malloc(sizeof(char) * 5);
snprintf(string,5,STRING_NONE); snprintf(string, 5, STRING_NONE);
return string; return string;
} }
else { else {
//Max 4 digits and 2 for 'KB' int sanity_ret;
int size = (4+2+1); char* string;
char* string = malloc(sizeof(char)*size); if(cach->L2/1024 > 1024) {
snprintf(string,size,"%d"STRING_KILOBYTES,cach->L2/1024); //1 for digit, 2 for 'MB'
int size = (1+2+1);
string = malloc(sizeof(char)*size);
sanity_ret = snprintf(string,size,"%d"STRING_MEGABYTES,cach->L2/(1048576));
}
else {
//4 for digits, 2 for 'KB'
int size = (4+2+1);
string = malloc(sizeof(char)*size);
sanity_ret = snprintf(string,size,"%d"STRING_KILOBYTES,cach->L2/1024);
}
assert(sanity_ret > 0);
return string; return string;
} }
} }
char* get_str_l3(struct cache* cach) { char* get_str_l3(struct cache* cach) {
if(cach->L3 == UNKNOWN) { if(cach->L3 == UNKNOWN) {
char* string = malloc(sizeof(char)*5); char* string = malloc(sizeof(char) * 5);
snprintf(string,5,STRING_NONE); snprintf(string, 5, STRING_NONE);
return string; return string;
} }
else { else {
//Max 4 digits and 2 for 'KB' int sanity_ret;
int size = (4+2+1); char* string;
char* string = malloc(sizeof(char)*size); if(cach->L3/1024 > 1024) {
snprintf(string,size,"%d"STRING_KILOBYTES,cach->L3/1024); //1 for digit, 2 for 'MB'
int size = (1+2+1);
string = malloc(sizeof(char)*size);
sanity_ret = snprintf(string,size,"%d"STRING_MEGABYTES,cach->L3/(1048576));
}
else {
//4 for digits, 2 for 'KB'
int size = (4+2+1);
string = malloc(sizeof(char)*size);
sanity_ret = snprintf(string,size,"%d"STRING_KILOBYTES,cach->L3/1024);
}
assert(sanity_ret > 0);
return string; return string;
} }
} }