From 6afe26f884d2fdf3a604b0f41d4693ba0c503ec0 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Mon, 16 Aug 2021 22:07:52 +0200 Subject: [PATCH] [v0.05] Fix a bug that caused segfault when terminal size cannot be retrieved (e.g, redirection) --- src/common/printer.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/common/printer.cpp b/src/common/printer.cpp index 93a5f3b..bee156f 100644 --- a/src/common/printer.cpp +++ b/src/common/printer.cpp @@ -27,6 +27,7 @@ #define max(a,b) (((a)>(b))?(a):(b)) #define MAX_ATTRIBUTES 100 +#define MAX_TERM_SIZE 1024 enum { ATTRIBUTE_NAME, @@ -360,7 +361,7 @@ bool print_gpufetch_cuda(struct gpu_info* gpu, STYLE s, struct color** cs, struc char* mem = (char *) emalloc(sizeof(char) * (strlen(mem_size) + strlen(mem_type) + 2)); sprintf(mem, "%s %s", mem_size, mem_type); - char* uarch_cc = (char *) emalloc(sizeof(char) * (strlen(uarch) + strlen(comp_cap) + 3)); + char* uarch_cc = (char *) emalloc(sizeof(char) * (strlen(uarch) + strlen(comp_cap) + 4)); sprintf(uarch_cc, "%s (%s)", uarch, comp_cap); setAttribute(art, ATTRIBUTE_NAME, gpu_name); @@ -409,15 +410,19 @@ struct terminal* get_terminal_size() { CONSOLE_SCREEN_BUFFER_INFO csbi; if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) == 0) { printWarn("GetConsoleScreenBufferInfo failed"); - return NULL; + term->w = MAX_TERM_SIZE; + term->h = MAX_TERM_SIZE; + return term; } term->w = csbi.srWindow.Right - csbi.srWindow.Left + 1; term->h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; #else struct winsize w; if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) { - printErr("ioctl: %s", strerror(errno)); - return NULL; + printWarn("get_terminal_size: ioctl: %s", strerror(errno)); + term->w = MAX_TERM_SIZE; + term->h = MAX_TERM_SIZE; + return term; } term->h = w.ws_row; term->w = w.ws_col;