[Asterisk-code-review] Clang: change previous tautological-compare fixes. (asterisk[11])

Mark Michelson asteriskteam at digium.com
Thu Apr 23 17:23:08 CDT 2015


Mark Michelson has submitted this change and it was merged.

Change subject: Clang: change previous tautological-compare fixes.
......................................................................


Clang: change previous tautological-compare fixes.

clang can warn about a so called tautological-compare, when it finds
comparisons which are logically always true, and are therefor deemed
unnecessary.

Exanple:
unsigned int x = 4;
if (x > 0)    // x is always going to be bigger than 0

Enum Case:
Each enumeration is its own type. Enums are an integer type but they
do not have to be *signed*. C leaves it up to the compiler as an
implementation option what to consider the integer type of a particu-
lar enumeration is. Gcc treats an enum without negative values as
an int while clang treats this enum as an unsigned int.

rmudgett & mmichelson: cast the enum to (unsigned int) in assert.
The cast does have an effect. For gcc, which seems to treat all enums
as int, the cast to unsigned int will eliminate the possibility of
negative values being allowed. For clang, which seems to treat enums
without any negative members as unsigned int, the cast will have no
effect. If for some reason in the future a negative value is ever
added to the enum the assert will still catch the negative value.

ASTERISK-24917

Change-Id: I0557ae0154a0b7de68883848a609309cdf0aee6a
---
M main/event.c
M main/security_events.c
2 files changed, 3 insertions(+), 3 deletions(-)

Approvals:
  Mark Michelson: Looks good to me, approved; Verified
  Richard Mudgett: Looks good to me, but someone else must approve
  Diederik de Groot: Looks good to me, but someone else must approve



diff --git a/main/event.c b/main/event.c
index 5796aab..94365e2 100644
--- a/main/event.c
+++ b/main/event.c
@@ -295,7 +295,7 @@
 
 	type = ast_event_get_type(event);
 
-	if (type >= ARRAY_LEN(event_names)) {
+	if ((unsigned int)type >= ARRAY_LEN(event_names)) {
 		ast_log(LOG_ERROR, "Invalid event type - '%u'\n", type);
 		return "";
 	}
diff --git a/main/security_events.c b/main/security_events.c
index ca24921..cb89590 100644
--- a/main/security_events.c
+++ b/main/security_events.c
@@ -427,7 +427,7 @@
 
 static int check_event_type(const enum ast_security_event_type event_type)
 {
-	if (event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
+	if ((unsigned int)event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
 		ast_log(LOG_ERROR, "Invalid security event type %u\n", event_type);
 		return -1;
 	}
@@ -680,7 +680,7 @@
 {
 	int res;
 
-	if (sec->event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
+	if ((unsigned int)sec->event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
 		ast_log(LOG_ERROR, "Invalid security event type\n");
 		return -1;
 	}

-- 
To view, visit https://gerrit.asterisk.org/235
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I0557ae0154a0b7de68883848a609309cdf0aee6a
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 11
Gerrit-Owner: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Diederik de Groot <dkgroot at talon.nl>
Gerrit-Reviewer: Mark Michelson <mmichelson at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>



More information about the asterisk-code-review mailing list