From 5310652f30dbd6127bb02a30d28c1995aa532602 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 18 Feb 2021 19:47:36 -0800 Subject: [PATCH] Fix compile warnings in client/ and lib/. Check returns from fgets(), sscanf() etc. Use %.*s to fix truncation warnings. Fix code formatting. Fix Makefile.linux --- client/Makefile.linux | 3 ++- client/acct_setup.cpp | 5 ++++- client/app_control.cpp | 24 ++++++++++++++++-------- client/app_start.cpp | 6 ++++-- client/hostinfo_unix.cpp | 35 +++++++++++++++++++++++------------ client/project.cpp | 6 +++--- lib/Makefile.linux | 2 +- lib/crypt.cpp | 4 +++- lib/diagnostics.cpp | 9 +++++---- lib/gui_rpc_client_ops.cpp | 30 +++++++++++++++++++++--------- 10 files changed, 82 insertions(+), 42 deletions(-) diff --git a/client/Makefile.linux b/client/Makefile.linux index 36e6fbf5d7..4a9b4ab8fd 100644 --- a/client/Makefile.linux +++ b/client/Makefile.linux @@ -6,7 +6,7 @@ # make -f Makefile.linux clean all # 3) do the same in this dir -CC = g++ -I ../ -I ../lib/ +CC = g++ -O4 -Wall -I ../ -I ../lib/ PROGS = boinc boinccmd @@ -135,6 +135,7 @@ clean: LIBS = ../lib/lib.a \ -lpthread \ + -lX11 -lXss \ -lssl -lcrypto \ -L /usr/local/lib/ -lcurl -lz -ldl diff --git a/client/acct_setup.cpp b/client/acct_setup.cpp index defd8f4a46..a69507c96e 100644 --- a/client/acct_setup.cpp +++ b/client/acct_setup.cpp @@ -289,8 +289,11 @@ void CLIENT_STATE::process_autologin(bool first) { // FILE* f = boinc_fopen(ACCOUNT_DATA_FILENAME, "r"); if (!f) return; - fgets(buf, 256, f); + p = fgets(buf, 256, f); fclose(f); + if (p == NULL) { + return; + } p = strstr(buf, "__"); if (!p) { boinc_delete_file(ACCOUNT_DATA_FILENAME); diff --git a/client/app_control.cpp b/client/app_control.cpp index a09b590ca7..e133bce5bd 100644 --- a/client/app_control.cpp +++ b/client/app_control.cpp @@ -642,11 +642,12 @@ bool ACTIVE_TASK::finish_file_present(int &exit_code) { } p = fgets(buf, sizeof(buf), f); if (p && strlen(buf)) { - fgets(buf2, sizeof(buf2), f); - msg_printf(result->project, - strstr(buf2, "notice")?MSG_USER_ALERT:MSG_INFO, - "Message from task: %s", buf - ); + if (fgets(buf2, sizeof(buf2), f)) { + msg_printf(result->project, + strstr(buf2, "notice")?MSG_USER_ALERT:MSG_INFO, + "Message from task: %s", buf + ); + } } fclose(f); return true; @@ -667,8 +668,12 @@ bool ACTIVE_TASK::temporary_exit_file_present( } else { x = y; } - (void) fgets(buf, 256, f); // read the \n - (void) fgets(buf, 256, f); + char *p = fgets(buf, 256, f); // read the \n + p = fgets(buf, 256, f); + if (p == NULL) { + fclose(f); + return false; + } strip_whitespace(buf); is_notice = false; if (fgets(buf2, 256, f)) { @@ -1617,8 +1622,11 @@ void ACTIVE_TASK::read_task_state_file() { FILE* f = boinc_fopen(path, "r"); if (!f) return; buf[0] = 0; - (void) fread(buf, 1, 4096, f); + size_t n = fread(buf, 1, 4096, f); fclose(f); + if (n == 0) { + return; + } buf[4095] = 0; double x; // TODO: use XML parser diff --git a/client/app_start.cpp b/client/app_start.cpp index d5ea25b27f..0e98e5c6a8 100644 --- a/client/app_start.cpp +++ b/client/app_start.cpp @@ -1065,7 +1065,8 @@ int ACTIVE_TASK::start(bool test) { // hook up stderr to a specially-named file // - (void) freopen(STDERR_FILE, "a", stderr); + if (freopen(STDERR_FILE, "a", stderr) == NULL) { + } // lower our priority if needed // @@ -1250,7 +1251,8 @@ void run_test_app() { int retval; char buf[256]; - getcwd(buf, sizeof(buf)); // so we can see where we're running + if (getcwd(buf, sizeof(buf)) == NULL) { // so we can see where we're running + } gstate.run_test_app = true; diff --git a/client/hostinfo_unix.cpp b/client/hostinfo_unix.cpp index fa65e55a6c..f287e4ec78 100644 --- a/client/hostinfo_unix.cpp +++ b/client/hostinfo_unix.cpp @@ -294,8 +294,11 @@ bool HOST_INFO::host_is_running_on_batteries() { ); fsys = fopen(path, "r"); if (!fsys) continue; - (void) fgets(buf, sizeof(buf), fsys); + char *p = fgets(buf, sizeof(buf), fsys); fclose(fsys); + if (p == NULL) { + break; + } // AC adapters have type "Mains" if ((strstr(buf, "mains") != NULL) || (strstr(buf, "Mains") != NULL)) { method = SysClass; @@ -327,7 +330,7 @@ bool HOST_INFO::host_is_running_on_batteries() { int apm_ac_line_status=1; // supposedly we're on batteries if the 5th entry is zero. - (void) fscanf(fapm, "%10s %d.%d %x %x", + int n = fscanf(fapm, "%10s %d.%d %x %x", apm_driver_version, &apm_major_version, &apm_minor_version, @@ -336,6 +339,7 @@ bool HOST_INFO::host_is_running_on_batteries() { ); fclose(fapm); + if (n != 5) return false; return (apm_ac_line_status == 0); } @@ -346,13 +350,15 @@ bool HOST_INFO::host_is_running_on_batteries() { if (!facpi) return false; char buf[128]; - (void) fgets(buf, sizeof(buf), facpi); + char *p = fgets(buf, sizeof(buf), facpi); fclose(facpi); + if (p == NULL) return false; - if ((strstr(buf, "state:") != NULL) || (strstr(buf, "Status:") != NULL)) + if ((strstr(buf, "state:") != NULL) || (strstr(buf, "Status:") != NULL)) { // on batteries if ac adapter is "off-line" (or maybe "offline") return (strstr(buf, "off") != NULL); + } return false; } @@ -363,8 +369,9 @@ bool HOST_INFO::host_is_running_on_batteries() { if (!fsys) return false; int online; - (void) fscanf(fsys, "%d", &online); + int n = fscanf(fsys, "%d", &online); fclose(fsys); + if (n != 1) return false; // online is 1 if on AC power, 0 if on battery return (0 == online); @@ -387,10 +394,12 @@ bool HOST_INFO::host_is_running_on_batteries() { for (devn = 0;; devn++) { mib[2] = devn; if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1) { - if (errno == ENXIO) + if (errno == ENXIO) { continue; - if (errno == ENOENT) + } + if (errno == ENOENT) { break; + } } if (!strcmp("acpiac0", snsrdev.xname)) { break; @@ -401,11 +410,12 @@ bool HOST_INFO::host_is_running_on_batteries() { } if (sysctl(mib, 5, &s, &slen, NULL, 0) != -1) { - if (s.value) + if (s.value) { // AC present return false; - else + } else { return true; + } } return false; @@ -414,11 +424,12 @@ bool HOST_INFO::host_is_running_on_batteries() { size_t len = sizeof(ac); if (sysctlbyname("hw.acpi.acline", &ac, &len, NULL, 0) != -1) { - if (ac) + if (ac) { // AC present return false; - else + } else { return true; + } } return false; @@ -1384,7 +1395,7 @@ int HOST_INFO::get_memory_info() { // return ERR_NOT_FOUND if ldd couldn't be opened or no version information was found // #ifdef __GLIBC__ -int get_libc_version(string& version, string& extra_info) { +int get_libc_version(string& version, string&) { version = string(gnu_get_libc_version()); return BOINC_SUCCESS; } diff --git a/client/project.cpp b/client/project.cpp index b460f8900d..20f0085cdf 100644 --- a/client/project.cpp +++ b/client/project.cpp @@ -719,7 +719,7 @@ void PROJECT::delete_project_file_symlinks() { for (i=0; iname); + snprintf(link_path, sizeof(link_path), "%.*s/%s", DIR_LEN, project_dir(), fref.open_name); + snprintf(file_path, sizeof(file_path), "%.*s/%s", DIR_LEN, project_dir(), fip->name); make_soft_link(this, link_path, file_path); } return 0; diff --git a/lib/Makefile.linux b/lib/Makefile.linux index 90761f9270..6f65599813 100644 --- a/lib/Makefile.linux +++ b/lib/Makefile.linux @@ -1,6 +1,6 @@ # make libraries for Linux client and boinccmd -CC = g++ -I ../ +CC = g++ -O4 -Wall -I ../ all: boinc.a boinc_cmd.a diff --git a/lib/crypt.cpp b/lib/crypt.cpp index 78de8c2955..01249cfc34 100644 --- a/lib/crypt.cpp +++ b/lib/crypt.cpp @@ -215,7 +215,9 @@ int scan_key_hex(FILE* f, KEY* key, int size) { len = size - sizeof(key->bits); for (i=0; idata[i] = n; } fs = fscanf(f, "."); diff --git a/lib/diagnostics.cpp b/lib/diagnostics.cpp index 731ec63052..75aa968638 100644 --- a/lib/diagnostics.cpp +++ b/lib/diagnostics.cpp @@ -792,7 +792,7 @@ void boinc_catch_signal(int signal) { size = backtrace (array, 64); // Anything that calls malloc here (i.e *printf()) will probably fail // so we'll do it the hard way. - (void) write(fileno(stderr),"Stack trace (",strlen("Stack trace (")); + int retval = write(fileno(stderr),"Stack trace (",strlen("Stack trace (")); char mbuf[10]; char *p=mbuf+9; int i=size; @@ -801,11 +801,12 @@ void boinc_catch_signal(int signal) { *(p--)=i%10+'0'; i/=10; } - (void) write(fileno(stderr),p+1,strlen(p+1)); - (void) write(fileno(stderr)," frames):",strlen(" frames):")); + retval = write(fileno(stderr),p+1,strlen(p+1)); + retval = write(fileno(stderr)," frames):",strlen(" frames):")); mbuf[0]=10; - (void) write(fileno(stderr),mbuf,1); + retval = write(fileno(stderr),mbuf,1); backtrace_symbols_fd(array, size, fileno(stderr)); + if (retval) {} #endif #ifdef __APPLE__ diff --git a/lib/gui_rpc_client_ops.cpp b/lib/gui_rpc_client_ops.cpp index fdd48c8e81..faa0e16d16 100644 --- a/lib/gui_rpc_client_ops.cpp +++ b/lib/gui_rpc_client_ops.cpp @@ -1970,26 +1970,38 @@ int RPC_CLIENT::run_benchmarks() { // if slot = -1, start the default screensaver // screensaverLoginUser is the login name of the user running the screensaver // -int RPC_CLIENT::run_graphics_app(const char *operation, int& operand, const char *screensaverLoginUser) { +int RPC_CLIENT::run_graphics_app( + const char *operation, int& operand, const char *screensaverLoginUser +) { char buf[256]; SET_LOCALE sl; RPC rpc(this); int thePID = -1; - bool stop = false; bool test = false; snprintf(buf, sizeof(buf), "\n"); if (!strcmp(operation, "run")) { - snprintf(buf, sizeof(buf), "\n%d\n\n%s\n", operand, screensaverLoginUser); + snprintf(buf, sizeof(buf), + "\n%d\n\n%s\n", + operand, screensaverLoginUser + ); } else if (!strcmp(operation, "runfullscreen")) { - snprintf(buf, sizeof(buf), "\n%d\n\n%s\n", operand, screensaverLoginUser); + snprintf(buf, sizeof(buf), + "\n%d\n\n%s\n", + operand, screensaverLoginUser + ); } else if (!strcmp(operation, "stop")) { - snprintf(buf, sizeof(buf), "\n%d\n\n%s\n", operand, screensaverLoginUser); - stop = true; - } else if (!strcmp(operation, "test")) { - snprintf(buf, sizeof(buf), "\n%d\n\n", operand); - test = true; + snprintf(buf, sizeof(buf), + "\n%d\n\n%s\n", + operand, screensaverLoginUser + ); + } else if (!strcmp(operation, "test")) { + snprintf(buf, sizeof(buf), + "\n%d\n\n", + operand + ); + test = true; } else { operand = -1; return -1;