From 4921660c2441bf5184fbc6935139cfb9c7d3fba1 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Tue, 21 Dec 2021 17:09:32 +0100 Subject: [PATCH] [v0.20] Properly check GPU index range --- src/common/main.cpp | 8 ++++---- src/common/master.cpp | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/common/main.cpp b/src/common/main.cpp index a5cba6b..9eddad3 100644 --- a/src/common/main.cpp +++ b/src/common/main.cpp @@ -82,15 +82,15 @@ int main(int argc, char* argv[]) { set_log_level(true); + struct gpu_info* gpu = get_gpu_info(list, get_gpu_idx()); + if(gpu == NULL) + return EXIT_FAILURE; + printf("[WARNING]: gpufetch is in beta. The provided information may be incomplete or wrong.\n\ If you want to help to improve gpufetch, please compare the output of the program\n\ with a reliable source which you know is right (e.g, techpowerup.com) and report\n\ any inconsistencies to https://github.com/Dr-Noob/gpufetch/issues\n"); - struct gpu_info* gpu = get_gpu_info(list, get_gpu_idx()); - if(gpu == NULL) - return EXIT_FAILURE; - if(print_gpufetch(gpu, get_style(), get_colors())) return EXIT_SUCCESS; else diff --git a/src/common/master.cpp b/src/common/master.cpp index 735992d..aa599e9 100644 --- a/src/common/master.cpp +++ b/src/common/master.cpp @@ -1,6 +1,7 @@ #include #include +#include "global.hpp" #include "colors.hpp" #include "master.hpp" #include "../cuda/cuda.hpp" @@ -78,6 +79,10 @@ int get_num_gpus_available(struct gpu_list* list) { } struct gpu_info* get_gpu_info(struct gpu_list* list, int idx) { + if(idx >= list->num_gpus || idx < 0) { + printErr("Specified GPU index is out of range: %d", idx); + return NULL; + } return list->gpus[idx]; }