From 576dc9a2e1575bbc48e3f82ea63f11f7167ab367 Mon Sep 17 00:00:00 2001 From: InterLinked1 <24227567+InterLinked1@users.noreply.github.com> Date: Fri, 24 Nov 2023 18:09:32 -0500 Subject: [PATCH] gcc: Use __builtin_unreachable where appropriate. There are several instances of code, mostly functions with a switch statement that either have a default or enumerate all cases explicitly with no default, where NULL (or some useless value) is returned to satisfy the function's return value. However, this is dead code; bbs_assert(0) was frequently used to indicate this. Instead, use __builtin_unreachable, which is more semantic, and possibly more performant. --- bbs/logger.c | 5 ++--- modules/mod_discord.c | 3 +-- modules/mod_irc_relay.c | 2 +- modules/mod_mail.c | 3 +-- nets/net_smtp.c | 11 ++++------- tests/test.c | 3 +-- 6 files changed, 10 insertions(+), 17 deletions(-) diff --git a/bbs/logger.c b/bbs/logger.c index 25088309..aaefa1cd 100644 --- a/bbs/logger.c +++ b/bbs/logger.c @@ -273,8 +273,7 @@ static const char *loglevel2str(enum bbs_log_level level, int term) case LOG_DEBUG: return COLOR_LOG_OR(" DEBUG", COLOR_GREEN, term); } - bbs_assert(0); - return NULL; + __builtin_unreachable(); } static const char *verbose_prefix(int level) @@ -301,7 +300,7 @@ static const char *verbose_prefix(int level) default: bbs_assert(0); } - return NULL; + __builtin_unreachable(); } #define log_puts(msg) fprintf(logfp, "%s", msg); fflush(logfp); diff --git a/modules/mod_discord.c b/modules/mod_discord.c index e66c8928..0c684a4a 100644 --- a/modules/mod_discord.c +++ b/modules/mod_discord.c @@ -231,8 +231,7 @@ static const char *status_str(enum user_status status) case STATUS_NONE: return "none"; } - bbs_assert(0); - return NULL; + __builtin_unreachable(); } static void remove_user(struct discord_user *user) diff --git a/modules/mod_irc_relay.c b/modules/mod_irc_relay.c index 057bf4e1..c0a7719c 100644 --- a/modules/mod_irc_relay.c +++ b/modules/mod_irc_relay.c @@ -955,7 +955,7 @@ static void command_cb(const char *clientname, enum irc_callback_msg_type type, } break; case CMD_QUIT: /* This is academic, as QUIT is handled above */ - bbs_assert(0); + __builtin_unreachable(); #if 0 ourchan = MAP1_MATCH(cp, clientname, channel) ? cp->channel2 : cp->channel1; snprintf(sysmsg, sizeof(sysmsg), ":%s/%s QUIT :%s", clientname, prefix, S_IF(msg)); diff --git a/modules/mod_mail.c b/modules/mod_mail.c index b1d273eb..c02a098a 100644 --- a/modules/mod_mail.c +++ b/modules/mod_mail.c @@ -210,8 +210,7 @@ const char *mailbox_event_type_name(enum mailbox_event_type type) return "UIDVALIDITYChange"; /* No default case */ } - bbs_assert(0); - return NULL; + __builtin_unreachable(); } static pthread_mutex_t eventidlock = PTHREAD_MUTEX_INITIALIZER; diff --git a/nets/net_smtp.c b/nets/net_smtp.c index 3487bc17..4b415fd3 100644 --- a/nets/net_smtp.c +++ b/nets/net_smtp.c @@ -621,7 +621,7 @@ static int handle_auth(struct smtp_session *smtp, char *s) free(pass); goto logindone; } else { - bbs_assert(0); + __builtin_unreachable(); } return 0; @@ -1005,8 +1005,7 @@ static const char *smtp_filter_type_name(enum smtp_filter_type type) case SMTP_FILTER_PREPEND: return "PREPEND"; /* No default */ } - bbs_assert(0); - return NULL; + __builtin_unreachable(); } int __smtp_filter_register(struct smtp_filter_provider *provider, enum smtp_filter_type type, enum smtp_filter_scope scope, enum smtp_direction dir, int priority, void *mod) @@ -1417,8 +1416,7 @@ static const char *delivery_action_name(enum smtp_delivery_action action) case DELIVERY_EXPANDED: return "expanded"; /* No default */ } - bbs_assert(0); - return NULL; + __builtin_unreachable(); } static const char *delivery_subject_name(struct smtp_delivery_outcome **f, int n) @@ -1436,8 +1434,7 @@ static const char *delivery_subject_name(struct smtp_delivery_outcome **f, int n case DELIVERY_EXPANDED: return "Delivery Status Notification (Expanded)"; /* No default */ } - bbs_assert(0); - return NULL; + __builtin_unreachable(); } /* Forward declaration */ diff --git a/tests/test.c b/tests/test.c index 4ad4d0b1..c5b64698 100644 --- a/tests/test.c +++ b/tests/test.c @@ -71,8 +71,7 @@ static const char *loglevel2str(enum bbs_log_level level) default: break; } - assert(0); - return NULL; + __builtin_unreachable(); } /*! \brief Minimal implementation of __bbs_log, so we can use the same interface */