From 8b55113ea8924d6e4ace14a3d54c9d9aca23eaa7 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Fri, 18 Sep 2020 20:38:25 +0200 Subject: [PATCH 1/5] Reimplement xAsnprintf and xSnprintf as type-safe functions --- XAlloc.c | 28 ++++++++++++++++++++++++++++ XAlloc.h | 8 ++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/XAlloc.c b/XAlloc.c index 294eb36a..c43eca76 100644 --- a/XAlloc.c +++ b/XAlloc.c @@ -5,6 +5,8 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif + +#include #include #include @@ -39,6 +41,32 @@ void* xRealloc(void* ptr, size_t size) { return data; } +int xAsprintf(char** strp, const char* fmt, ...) { + va_list vl; + va_start(vl, fmt); + int _r = vasprintf(strp, fmt, vl); + va_end(vl); + + if (_r < 0) { + fail(); + } + + return _r; +} + +int xSnprintf(char* buf, int len, const char* fmt, ...) { + va_list vl; + va_start(vl, fmt); + int _n=vsnprintf(buf, len, fmt, vl); + va_end(vl); + + if (!(_n > -1 && _n < len)) { + fail(); + } + + return _n; +} + char* xStrdup_(const char* str) { char* data = strdup(str); if (!data) { diff --git a/XAlloc.h b/XAlloc.h index cebe095c..70dfc59f 100644 --- a/XAlloc.h +++ b/XAlloc.h @@ -19,11 +19,11 @@ void* xCalloc(size_t nmemb, size_t size); void* xRealloc(void* ptr, size_t size); -#undef xAsprintf +ATTR_FORMAT(printf, 2, 3) +int xAsprintf(char **strp, const char* fmt, ...); -#define xAsprintf(strp, fmt, ...) do { int _r=asprintf(strp, fmt, __VA_ARGS__); if (_r < 0) { fail(); } } while(0) - -#define xSnprintf(fmt, len, ...) do { int _l=len; int _n=snprintf(fmt, _l, __VA_ARGS__); if (!(_n > -1 && _n < _l)) { curs_set(1); endwin(); err(1, NULL); } } while(0) +ATTR_FORMAT(printf, 3, 4) +int xSnprintf(char *buf, int len, const char* fmt, ...); #undef xStrdup #undef xStrdup_ From 736c496dbf15c1a665ba7e0b03d7994117a0b215 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Fri, 18 Sep 2020 20:39:50 +0200 Subject: [PATCH 2/5] Cleanse xStrdup mess --- XAlloc.c | 2 +- XAlloc.h | 10 +--------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/XAlloc.c b/XAlloc.c index c43eca76..20ddafc8 100644 --- a/XAlloc.c +++ b/XAlloc.c @@ -67,7 +67,7 @@ int xSnprintf(char* buf, int len, const char* fmt, ...) { return _n; } -char* xStrdup_(const char* str) { +char* xStrdup(const char* str) { char* data = strdup(str); if (!data) { fail(); diff --git a/XAlloc.h b/XAlloc.h index 70dfc59f..32389e57 100644 --- a/XAlloc.h +++ b/XAlloc.h @@ -25,14 +25,6 @@ int xAsprintf(char **strp, const char* fmt, ...); ATTR_FORMAT(printf, 3, 4) int xSnprintf(char *buf, int len, const char* fmt, ...); -#undef xStrdup -#undef xStrdup_ -#ifdef NDEBUG -# define xStrdup_ xStrdup -#else -# define xStrdup(str_) (assert(str_), xStrdup_(str_)) -#endif - -char* xStrdup_(const char* str) ATTR_NONNULL; +char* xStrdup(const char* str) ATTR_NONNULL; #endif From dac1e05a2c7ac68993f6d046c43fbef209458206 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Fri, 18 Sep 2020 21:55:09 +0200 Subject: [PATCH 3/5] Fix FreeBSD compile issue This issue was previously hidden as xSnprintf expanded to only one large command that didn't trigger the GCC formatting check. --- freebsd/FreeBSDProcessList.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freebsd/FreeBSDProcessList.c b/freebsd/FreeBSDProcessList.c index 11cfe7e1..6e7f6ecb 100644 --- a/freebsd/FreeBSDProcessList.c +++ b/freebsd/FreeBSDProcessList.c @@ -355,7 +355,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) { if (jid < 0) { if (!jail_errmsg[0]) xSnprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno)); - return NULL; + return NULL; } else if (jid == kproc->ki_jid) { jname = xStrdup(jnamebuf); if (jname == NULL) From e1c96879f444150b01c7f556cacdca82c811efb1 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Fri, 18 Sep 2020 19:23:04 +0200 Subject: [PATCH 4/5] Sort headers/includes --- Affinity.c | 2 ++ AvailableMetersPanel.h | 4 ++-- CategoriesPanel.h | 4 ++-- ColorsPanel.h | 2 +- DisplayOptionsPanel.h | 2 +- IncSet.h | 3 ++- InfoScreen.h | 4 ++-- Meter.h | 3 ++- MetersPanel.h | 2 +- Panel.h | 2 +- Process.h | 4 ++-- ProcessList.h | 4 ++-- ScreenManager.h | 2 +- Settings.h | 3 ++- XAlloc.h | 2 +- 15 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Affinity.c b/Affinity.c index 098e5c16..d207e5a3 100644 --- a/Affinity.c +++ b/Affinity.c @@ -6,6 +6,8 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ +#include "config.h" + #include "Affinity.h" #include diff --git a/AvailableMetersPanel.h b/AvailableMetersPanel.h index ecebb28d..92d7c941 100644 --- a/AvailableMetersPanel.h +++ b/AvailableMetersPanel.h @@ -7,10 +7,10 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#include "Settings.h" #include "Panel.h" -#include "ScreenManager.h" #include "ProcessList.h" +#include "ScreenManager.h" +#include "Settings.h" typedef struct AvailableMetersPanel_ { Panel super; diff --git a/CategoriesPanel.h b/CategoriesPanel.h index fefc3e93..5f8cb0f5 100644 --- a/CategoriesPanel.h +++ b/CategoriesPanel.h @@ -8,9 +8,9 @@ in the source distribution for its full text. */ #include "Panel.h" -#include "Settings.h" -#include "ScreenManager.h" #include "ProcessList.h" +#include "ScreenManager.h" +#include "Settings.h" typedef struct CategoriesPanel_ { Panel super; diff --git a/ColorsPanel.h b/ColorsPanel.h index 479fad08..8dd61711 100644 --- a/ColorsPanel.h +++ b/ColorsPanel.h @@ -8,8 +8,8 @@ in the source distribution for its full text. */ #include "Panel.h" -#include "Settings.h" #include "ScreenManager.h" +#include "Settings.h" typedef struct ColorsPanel_ { Panel super; diff --git a/DisplayOptionsPanel.h b/DisplayOptionsPanel.h index 499b2fc1..b103503b 100644 --- a/DisplayOptionsPanel.h +++ b/DisplayOptionsPanel.h @@ -8,8 +8,8 @@ in the source distribution for its full text. */ #include "Panel.h" -#include "Settings.h" #include "ScreenManager.h" +#include "Settings.h" typedef struct DisplayOptionsPanel_ { Panel super; diff --git a/IncSet.h b/IncSet.h index 2fb22c49..81f0f54c 100644 --- a/IncSet.h +++ b/IncSet.h @@ -7,9 +7,10 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ +#include + #include "FunctionBar.h" #include "Panel.h" -#include #define INCMODE_MAX 40 diff --git a/InfoScreen.h b/InfoScreen.h index a5a5abf6..196c56e1 100644 --- a/InfoScreen.h +++ b/InfoScreen.h @@ -1,10 +1,10 @@ #ifndef HEADER_InfoScreen #define HEADER_InfoScreen -#include "Process.h" -#include "Panel.h" #include "FunctionBar.h" #include "IncSet.h" +#include "Panel.h" +#include "Process.h" typedef struct InfoScreen_ InfoScreen; diff --git a/Meter.h b/Meter.h index 03b49608..da1dc5df 100644 --- a/Meter.h +++ b/Meter.h @@ -7,9 +7,10 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#include "ListItem.h" #include +#include "ListItem.h" + #define METER_BUFFER_LEN 256 typedef struct Meter_ Meter; diff --git a/MetersPanel.h b/MetersPanel.h index dbc31ee4..919b117d 100644 --- a/MetersPanel.h +++ b/MetersPanel.h @@ -8,8 +8,8 @@ in the source distribution for its full text. */ #include "Panel.h" -#include "Settings.h" #include "ScreenManager.h" +#include "Settings.h" typedef struct MetersPanel_ MetersPanel; diff --git a/Panel.h b/Panel.h index 1c7c171d..e5b28a41 100644 --- a/Panel.h +++ b/Panel.h @@ -7,9 +7,9 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ +#include "FunctionBar.h" #include "Object.h" #include "Vector.h" -#include "FunctionBar.h" typedef struct Panel_ Panel; diff --git a/Process.h b/Process.h index 404f73e6..a2f266f7 100644 --- a/Process.h +++ b/Process.h @@ -20,10 +20,10 @@ in the source distribution for its full text. #endif #define PAGE_SIZE_KB ( PAGE_SIZE / ONE_K ) -#include "Object.h" - #include +#include "Object.h" + #define PROCESS_FLAG_IO 0x0001 typedef enum ProcessFields { diff --git a/ProcessList.h b/ProcessList.h index 7b572d8f..34f92b1b 100644 --- a/ProcessList.h +++ b/ProcessList.h @@ -7,12 +7,12 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#include "Vector.h" #include "Hashtable.h" -#include "UsersTable.h" #include "Panel.h" #include "Process.h" #include "Settings.h" +#include "UsersTable.h" +#include "Vector.h" #ifdef HAVE_LIBHWLOC #include diff --git a/ScreenManager.h b/ScreenManager.h index 6f19ab8f..4c78efd3 100644 --- a/ScreenManager.h +++ b/ScreenManager.h @@ -7,10 +7,10 @@ Released under the GNU GPL, see the COPYING file in the source distribution for its full text. */ -#include "Vector.h" #include "Header.h" #include "Settings.h" #include "Panel.h" +#include "Vector.h" typedef enum Orientation_ { VERTICAL, diff --git a/Settings.h b/Settings.h index e1518eca..2f52681c 100644 --- a/Settings.h +++ b/Settings.h @@ -9,9 +9,10 @@ in the source distribution for its full text. #define DEFAULT_DELAY 15 -#include "Process.h" #include +#include "Process.h" + typedef struct { int len; char** names; diff --git a/XAlloc.h b/XAlloc.h index 32389e57..97c15519 100644 --- a/XAlloc.h +++ b/XAlloc.h @@ -7,8 +7,8 @@ #include "Macros.h" -#include #include +#include #include void fail(void) ATTR_NORETURN; From 2cde4a7f8eef5f4b4701128b272d529c74f5cfc1 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Fri, 18 Sep 2020 19:32:41 +0200 Subject: [PATCH 5/5] Enable NULL pointer checks via compiler if supported --- configure.ac | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/configure.ac b/configure.ac index d06c598f..5302f1e6 100644 --- a/configure.ac +++ b/configure.ac @@ -169,6 +169,23 @@ m4_define([HTOP_CHECK_LIB], ], [$4]) ]) +dnl https://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html +AC_DEFUN([AX_CHECK_COMPILE_FLAG], +[AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF +AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl +AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [ + ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS + _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1" + AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], + [AS_VAR_SET(CACHEVAR,[yes])], + [AS_VAR_SET(CACHEVAR,[no])]) + _AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags]) +AS_VAR_IF(CACHEVAR,yes, + [m4_default([$2], :)], + [m4_default([$3], :)]) +AS_VAR_POPDEF([CACHEVAR])dnl +])dnl AX_CHECK_COMPILE_FLAGS + AC_ARG_ENABLE(unicode, [AS_HELP_STRING([--enable-unicode], [enable Unicode support])], ,enable_unicode="yes") if test "x$enable_unicode" = xyes; then HTOP_CHECK_SCRIPT([ncursesw6], [addnwstr], [HAVE_LIBNCURSESW], "ncursesw6-config", @@ -291,6 +308,8 @@ AM_CFLAGS="\ -Wunused\ -Wwrite-strings" +AX_CHECK_COMPILE_FLAG([-Wnull-dereference], [AM_CFLAGS="$AM_CFLAGS -Wnull-dereference"], , [-Werror]) + AC_ARG_ENABLE([werror], [AS_HELP_STRING([--enable-werror], [Treat warnings as errors (default: warnings are not errors)])], [enable_werror="$enableval"], [enable_werror=no]) AS_IF([test "x$enable_werror" = "xyes"], [AM_CFLAGS="$AM_CFLAGS -Werror"]) AC_SUBST([AM_CFLAGS])