From 46661f64e7bdbac145f5fe07c016cdfa8a8a641a Mon Sep 17 00:00:00 2001 From: 4zv4l Date: Wed, 3 Nov 2021 13:28:27 +0100 Subject: [PATCH] check open file before close() --- uwufetch.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/uwufetch.c b/uwufetch.c index 5bfa206..9a87573 100644 --- a/uwufetch.c +++ b/uwufetch.c @@ -666,10 +666,11 @@ void get_info() #else FILE *cpuinfo = popen("sysctl -a | egrep -i 'hw.model'", "r"); #endif - FILE *host_model_info = fopen("/sys/devices/virtual/dmi/id/board_name", "r"); - if (!host_model_info) - if ((host_model_info = fopen("/sys/devices/virtual/dmi/id/product_name", "r")) != NULL) - fclose(host_model_info); + FILE *host_model_info = fopen("/sys/devices/virtual/dmi/id/board_name", "r"); // first try open the file + if (!host_model_info) // if failed + host_model_info = fopen("/sys/devices/virtual/dmi/id/product_name", "r"); // try open another file + if(host_model_info) // if one of the file could be open then close it + fclose(host_model_info); #ifdef __WINDOWS__ host_model_info = popen("wmic computersystem get model", "r"); while (fgets(line, sizeof(line), host_model_info))