From ea0098724efbd64a06b3103d19bb5f711f6f3cd7 Mon Sep 17 00:00:00 2001
From: Matthew Jordan <mjordan@digium.com>
Date: Thu, 9 Apr 2015 12:57:21 +0000
Subject: [PATCH] clang compiler warnings: Fix autological comparisons

This fixes autological comparison warnings in the following:
 * chan_skinny: letohl may return a signed or unsigned value, depending on the
   macro chosen
 * func_curl: Provide a specific cast to CURLoption to prevent mismatch
 * cel: Fix enum comparisons where the enum can never be negative
 * enum: Fix comparison of return result of dn_expand, which returns a signed
   int value
 * event: Fix enum comparisons where the enum can never be negative
 * indications: tone_data.freq1 and freq2 are unsigned, and hence can never be
   negative
 * presencestate: Use the actual enum value for INVALID state
 * security_events: Fix enum comparisons where the enum can never be negative
 * udptl: Don't bother to check if the return value from encode_length is less
   than 0, as it returns an unsigned int
 * translate: Since the parameters are unsigned int, don't bother checking
   to see if they are negative. The cast to unsigned int would already blow
   past the matrix bounds.
 * res_pjsip_exten_state: Use a temporary value to cache the return of
   ast_hint_presence_state
 * res_stasis_playback: Fix enum comparisons where the enum can never be
   negative
 * res_stasis_recording: Add an enum value for the case where the recording
   operation is in error; fix enum comparisons
 * resource_bridges: Use enum value as opposed to -1
 * resource_channels: Use enum value as opposed to -1

Review: https://reviewboard.asterisk.org/r/4533
ASTERISK-24917
Reported by: dkdegroot
patches:
  rb4533.patch submitted by dkdegroot (License 6600)
........

Merged revisions 434469 from http://svn.asterisk.org/svn/asterisk/branches/11
........

Merged revisions 434470 from http://svn.asterisk.org/svn/asterisk/branches/13


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@434471 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channels/chan_skinny.c              |  6 +++++-
 channels/pjsip/dialplan_functions.c |  4 ++--
 funcs/func_curl.c                   |  2 +-
 include/asterisk/app.h              |  2 ++
 include/asterisk/cel.h              |  4 +++-
 include/asterisk/logger.h           |  2 +-
 include/asterisk/utils.h            |  2 +-
 main/app.c                          |  3 +++
 main/cel.c                          | 16 ++++++----------
 main/enum.c                         |  2 +-
 main/event.c                        |  2 +-
 main/indications.c                  |  5 ++---
 main/presencestate.c                |  2 +-
 main/security_events.c              |  4 ++--
 main/udptl.c                        |  9 +++------
 res/ari/resource_bridges.c          |  2 +-
 res/ari/resource_channels.c         |  2 +-
 res/res_pjsip_exten_state.c         |  6 ++++--
 res/res_stasis_playback.c           |  4 ++--
 res/res_stasis_recording.c          |  6 +++---
 20 files changed, 45 insertions(+), 40 deletions(-)

diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c
index 3d56cc840d..d1c05abecb 100644
--- a/channels/chan_skinny.c
+++ b/channels/chan_skinny.c
@@ -2374,6 +2374,7 @@ static char *callstate2str(int ind)
 static int transmit_response_bysession(struct skinnysession *s, struct skinny_req *req)
 {
 	int res = 0;
+	unsigned long len;
 
 	if (!s) {
 		ast_log(LOG_WARNING, "Asked to transmit to a non-existent session!\n");
@@ -2382,7 +2383,10 @@ static int transmit_response_bysession(struct skinnysession *s, struct skinny_re
 
 	ast_mutex_lock(&s->lock);
 
-	if ((letohl(req->len) > SKINNY_MAX_PACKET) || (letohl(req->len) < 0)) {
+	/* Don't optimize out assigning letohl() to len. It is necessary to guarantee that the comparison will always catch invalid values.
+	 * letohl() may or may not return a signed value depending upon which definition is used. */
+	len = letohl(req->len);
+	if (SKINNY_MAX_PACKET < len) {
 		ast_log(LOG_WARNING, "transmit_response: the length of the request (%u) is out of bounds (%d)\n", letohl(req->len), SKINNY_MAX_PACKET);
 		ast_mutex_unlock(&s->lock);
 		return -1;
diff --git a/channels/pjsip/dialplan_functions.c b/channels/pjsip/dialplan_functions.c
index 8080f86a43..e291110e22 100644
--- a/channels/pjsip/dialplan_functions.c
+++ b/channels/pjsip/dialplan_functions.c
@@ -866,11 +866,11 @@ static int media_offer_read_av(struct ast_sip_session *session, char *buf,
 
 		/* add one since we'll include a comma */
 		size = strlen(ast_format_get_name(fmt)) + 1;
-		len -= size;
-		if ((len) < 0) {
+		if (len < size) {
 			ao2_ref(fmt, -1);
 			break;
 		}
+		len -= size;
 
 		/* no reason to use strncat here since we have already ensured buf has
                    enough space, so strcat can be safely used */
diff --git a/funcs/func_curl.c b/funcs/func_curl.c
index 7fd56beb40..beab0c144c 100644
--- a/funcs/func_curl.c
+++ b/funcs/func_curl.c
@@ -171,7 +171,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #define CURLVERSION_ATLEAST(a,b,c) \
 	((LIBCURL_VERSION_MAJOR > (a)) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR > (b))) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR == (b)) && (LIBCURL_VERSION_PATCH >= (c))))
 
-#define CURLOPT_SPECIAL_HASHCOMPAT -500
+#define CURLOPT_SPECIAL_HASHCOMPAT ((CURLoption) -500)
 
 static void curlds_free(void *data);
 
diff --git a/include/asterisk/app.h b/include/asterisk/app.h
index d70d8a6f2e..6171dd4f26 100644
--- a/include/asterisk/app.h
+++ b/include/asterisk/app.h
@@ -985,6 +985,8 @@ int ast_play_and_wait(struct ast_channel *chan, const char *fn);
  * \since 12
  */
 enum ast_record_if_exists {
+	/*! Return an Error State for IF_Exists */
+	AST_RECORD_IF_EXISTS_ERROR = -1,
 	/*! Fail the recording. */
 	AST_RECORD_IF_EXISTS_FAIL,
 	/*! Overwrite the existing recording. */
diff --git a/include/asterisk/cel.h b/include/asterisk/cel.h
index 833b48b85c..350b4bf9f8 100644
--- a/include/asterisk/cel.h
+++ b/include/asterisk/cel.h
@@ -39,6 +39,8 @@ extern "C" {
  * \brief CEL event types
  */
 enum ast_cel_event_type {
+	AST_CEL_INVALID_VALUE = -1,
+	AST_CEL_ALL = 0,
 	/*! \brief channel birth */
 	AST_CEL_CHANNEL_START = 1,
 	/*! \brief channel end */
@@ -75,7 +77,7 @@ enum ast_cel_event_type {
 	AST_CEL_LOCAL_OPTIMIZE = 17,
 };
 
-/*! 
+/*!
  * \brief Check to see if CEL is enabled
  *
  * \since 1.8
diff --git a/include/asterisk/logger.h b/include/asterisk/logger.h
index efaac48872..c701d17cea 100644
--- a/include/asterisk/logger.h
+++ b/include/asterisk/logger.h
@@ -371,7 +371,7 @@ void ast_callid_strnprint(char *buffer, size_t buffer_size, ast_callid callid);
 
 #define DEBUG_ATLEAST(level) \
 	(option_debug >= (level) \
-		|| (ast_opt_dbg_module && ast_debug_get_by_module(AST_MODULE) >= (level)))
+		|| (ast_opt_dbg_module && (int)ast_debug_get_by_module(AST_MODULE) >= (level)))
 
 /*!
  * \brief Log a DEBUG message
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index bbcc2da938..f3f571972e 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -816,7 +816,7 @@ int ast_safe_mkdir(const char *base_path, const char *path, int mode);
  * \param a the array to bound check
  * \return 0 if value out of bounds, otherwise true (non-zero)
  */
-#define ARRAY_IN_BOUNDS(v, a) IN_BOUNDS(v, 0, ARRAY_LEN(a) - 1)
+#define ARRAY_IN_BOUNDS(v, a) IN_BOUNDS((int) (v), 0, ARRAY_LEN(a) - 1)
 
 /* Definition for Digest authorization */
 struct ast_http_digest {
diff --git a/main/app.c b/main/app.c
index f3fc2980f5..37afe42978 100644
--- a/main/app.c
+++ b/main/app.c
@@ -1515,6 +1515,9 @@ static int __ast_play_and_record(struct ast_channel *chan, const char *playfile,
 	case AST_RECORD_IF_EXISTS_APPEND:
 		ioflags |= O_APPEND;
 		break;
+	case AST_RECORD_IF_EXISTS_ERROR:
+		ast_assert(0);
+		break;
 	}
 
 	if (silencethreshold < 0) {
diff --git a/main/cel.c b/main/cel.c
index 93655c741b..0c1e37b689 100644
--- a/main/cel.c
+++ b/main/cel.c
@@ -284,7 +284,7 @@ static struct aco_type *general_options[] = ACO_TYPES(&general_option);
  * \brief Map of ast_cel_event_type to strings
  */
 static const char * const cel_event_types[CEL_MAX_EVENT_IDS] = {
-	[0]                        = "ALL",
+	[AST_CEL_ALL]              = "ALL",
 	[AST_CEL_CHANNEL_START]    = "CHAN_START",
 	[AST_CEL_CHANNEL_END]      = "CHAN_END",
 	[AST_CEL_ANSWER]           = "ANSWER",
@@ -524,16 +524,13 @@ enum ast_cel_event_type ast_cel_str_to_event_type(const char *name)
 	unsigned int i;
 
 	for (i = 0; i < ARRAY_LEN(cel_event_types); i++) {
-		if (!cel_event_types[i]) {
-			continue;
-		}
-
-		if (!strcasecmp(name, cel_event_types[i])) {
+		if (cel_event_types[i] && !strcasecmp(name, cel_event_types[i])) {
 			return i;
 		}
 	}
 
-	return -1;
+	ast_log(LOG_ERROR, "Unknown event name '%s'\n", name);
+	return AST_CEL_INVALID_VALUE;
 }
 
 static int ast_cel_track_event(enum ast_cel_event_type et)
@@ -563,11 +560,10 @@ static int events_handler(const struct aco_option *opt, struct ast_variable *var
 
 		event_type = ast_cel_str_to_event_type(cur_event);
 
-		if (event_type == 0) {
+		if (event_type == AST_CEL_ALL) {
 			/* All events */
 			cfg->events = (int64_t) -1;
-		} else if (event_type == -1) {
-			ast_log(LOG_ERROR, "Unknown event name '%s'\n", cur_event);
+		} else if (event_type == AST_CEL_INVALID_VALUE) {
 			return -1;
 		} else {
 			cfg->events |= ((int64_t) 1 << event_type);
diff --git a/main/enum.c b/main/enum.c
index e36a9c7456..bae1299656 100644
--- a/main/enum.c
+++ b/main/enum.c
@@ -257,7 +257,7 @@ struct ebl_context {
 static int ebl_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
 {
 	struct ebl_context *c = context;
-	unsigned int i;
+	int i;
 
 	c->pos = 0;	/* default to empty */
 	c->separator[0] = 0;
diff --git a/main/event.c b/main/event.c
index 92adc03684..8880b96993 100644
--- a/main/event.c
+++ b/main/event.c
@@ -199,7 +199,7 @@ const char *ast_event_get_type_name(const struct ast_event *event)
 
 	type = ast_event_get_type(event);
 
-	if (type < 0 || type >= ARRAY_LEN(event_names)) {
+	if (type >= ARRAY_LEN(event_names)) {
 		ast_log(LOG_ERROR, "Invalid event type - '%u'\n", type);
 		return "";
 	}
diff --git a/main/indications.c b/main/indications.c
index af9cfc8ef7..02a68b7cac 100644
--- a/main/indications.c
+++ b/main/indications.c
@@ -359,14 +359,13 @@ int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst,
 
 		if (tone_data.midinote) {
 			/* midi notes must be between 0 and 127 */
-
-			if (tone_data.freq1 >= 0 && tone_data.freq1 <= 127) {
+			if (tone_data.freq1 <= 127) {
 				tone_data.freq1 = midi_tohz[tone_data.freq1];
 			} else {
 				tone_data.freq1 = 0;
 			}
 
-			if (tone_data.freq2 >= 0 && tone_data.freq2 <= 127) {
+			if (tone_data.freq2 <= 127) {
 				tone_data.freq2 = midi_tohz[tone_data.freq2];
 			} else {
 				tone_data.freq2 = 0;
diff --git a/main/presencestate.c b/main/presencestate.c
index 529979bac7..4b3e94e61b 100644
--- a/main/presencestate.c
+++ b/main/presencestate.c
@@ -323,7 +323,7 @@ static void do_presence_state_change(const char *provider)
 
 	state = ast_presence_state_helper(provider, &subtype, &message, 0);
 
-	if (state < 0) {
+	if (state == AST_PRESENCE_INVALID) {
 		return;
 	}
 
diff --git a/main/security_events.c b/main/security_events.c
index 0546bfe738..5a8df66d11 100644
--- a/main/security_events.c
+++ b/main/security_events.c
@@ -886,7 +886,7 @@ const char *ast_security_event_severity_get_name(
 
 static int check_event_type(const enum ast_security_event_type event_type)
 {
-	if (event_type < 0 || event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
+	if (event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
 		ast_log(LOG_ERROR, "Invalid security event type %u\n", event_type);
 		return -1;
 	}
@@ -1172,7 +1172,7 @@ return_error:
 
 int ast_security_event_report(const struct ast_security_event_common *sec)
 {
-	if (sec->event_type < 0 || sec->event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
+	if (sec->event_type >= AST_SECURITY_EVENT_NUM_TYPES) {
 		ast_log(LOG_ERROR, "Invalid security event type\n");
 		return -1;
 	}
diff --git a/main/udptl.c b/main/udptl.c
index 91458489d8..4e878195c4 100644
--- a/main/udptl.c
+++ b/main/udptl.c
@@ -362,8 +362,7 @@ static int encode_open_type(const struct ast_udptl *udptl, uint8_t *buf, unsigne
 	}
 	/* Encode the open type */
 	for (octet_idx = 0; ; num_octets -= enclen, octet_idx += enclen) {
-		if ((enclen = encode_length(buf, len, num_octets)) < 0)
-			return -1;
+		enclen = encode_length(buf, len, num_octets);
 		if (enclen + *len > buflen) {
 			ast_log(LOG_ERROR, "UDPTL (%s): Buffer overflow detected (%u + %u > %u)\n",
 				LOG_TAG(udptl), enclen, *len, buflen);
@@ -646,8 +645,7 @@ static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, unsigned int bu
 		buf[len++] = 0x00;
 		/* The number of entries will always be zero, so it is pointless allowing
 		   for the fragmented case here. */
-		if (encode_length(buf, &len, 0) < 0)
-			return -1;
+		encode_length(buf, &len, 0);
 		break;
 	case UDPTL_ERROR_CORRECTION_REDUNDANCY:
 		/* Encode the error recovery type */
@@ -658,8 +656,7 @@ static int udptl_build_packet(struct ast_udptl *s, uint8_t *buf, unsigned int bu
 			entries = s->tx_seq_no;
 		/* The number of entries will always be small, so it is pointless allowing
 		   for the fragmented case here. */
-		if (encode_length(buf, &len, entries) < 0)
-			return -1;
+		encode_length(buf, &len, entries);
 		/* Encode the elements */
 		for (i = 0; i < entries; i++) {
 			j = (entry - i - 1) & UDPTL_BUF_MASK;
diff --git a/res/ari/resource_bridges.c b/res/ari/resource_bridges.c
index bcd27af146..5914a07113 100644
--- a/res/ari/resource_bridges.c
+++ b/res/ari/resource_bridges.c
@@ -692,7 +692,7 @@ void ast_ari_bridges_record(struct ast_variable *headers,
 		return;
 	}
 
-	if (options->if_exists == -1) {
+	if (options->if_exists == AST_RECORD_IF_EXISTS_ERROR) {
 		ast_ari_response_error(
 			response, 400, "Bad Request",
 			"ifExists invalid");
diff --git a/res/ari/resource_channels.c b/res/ari/resource_channels.c
index fb1aa039c6..5943158317 100644
--- a/res/ari/resource_channels.c
+++ b/res/ari/resource_channels.c
@@ -626,7 +626,7 @@ void ast_ari_channels_record(struct ast_variable *headers,
 		return;
 	}
 
-	if (options->if_exists == -1) {
+	if (options->if_exists == AST_RECORD_IF_EXISTS_ERROR) {
 		ast_ari_response_error(
 			response, 400, "Bad Request",
 			"ifExists invalid");
diff --git a/res/res_pjsip_exten_state.c b/res/res_pjsip_exten_state.c
index 45bfff605e..da9b133f9c 100644
--- a/res/res_pjsip_exten_state.c
+++ b/res/res_pjsip_exten_state.c
@@ -401,6 +401,7 @@ static struct ast_sip_exten_state_data *exten_state_data_alloc(struct ast_sip_su
 	struct ast_sip_exten_state_data *exten_state_data;
 	char *subtype = NULL;
 	char *message = NULL;
+	int presence_state;
 
 	exten_state_data = ao2_alloc(sizeof(*exten_state_data), exten_state_data_destructor);
 	if (!exten_state_data) {
@@ -408,11 +409,12 @@ static struct ast_sip_exten_state_data *exten_state_data_alloc(struct ast_sip_su
 	}
 
 	exten_state_data->exten = exten_state_sub->exten;
-	if ((exten_state_data->presence_state = ast_hint_presence_state(NULL, exten_state_sub->context,
-			exten_state_sub->exten, &subtype, &message)) == -1) {
+	presence_state = ast_hint_presence_state(NULL, exten_state_sub->context, exten_state_sub->exten, &subtype, &message);
+	if (presence_state  == -1 || presence_state == AST_PRESENCE_INVALID) {
 		ao2_cleanup(exten_state_data);
 		return NULL;
 	}
+	exten_state_data->presence_state = presence_state;
 	exten_state_data->presence_subtype = subtype;
 	exten_state_data->presence_message = message;
 	exten_state_data->user_agent = exten_state_sub->user_agent;
diff --git a/res/res_stasis_playback.c b/res/res_stasis_playback.c
index 1de774ff5e..2eac55f190 100644
--- a/res/res_stasis_playback.c
+++ b/res/res_stasis_playback.c
@@ -629,9 +629,9 @@ enum stasis_playback_oper_results stasis_app_playback_operation(
 	playback_opreation_cb cb;
 	SCOPED_AO2LOCK(lock, playback);
 
-	ast_assert(playback->state >= 0 && playback->state < STASIS_PLAYBACK_STATE_MAX);
+	ast_assert(playback->state < STASIS_PLAYBACK_STATE_MAX);
 
-	if (operation < 0 || operation >= STASIS_PLAYBACK_MEDIA_OP_MAX) {
+	if (operation >= STASIS_PLAYBACK_MEDIA_OP_MAX) {
 		ast_log(LOG_ERROR, "Invalid playback operation %u\n", operation);
 		return -1;
 	}
diff --git a/res/res_stasis_recording.c b/res/res_stasis_recording.c
index 433adb3d50..df7f8b33a5 100644
--- a/res/res_stasis_recording.c
+++ b/res/res_stasis_recording.c
@@ -212,7 +212,7 @@ enum ast_record_if_exists stasis_app_recording_if_exists_parse(
 		return AST_RECORD_IF_EXISTS_APPEND;
 	}
 
-	return -1;
+	return AST_RECORD_IF_EXISTS_ERROR;
 }
 
 static void recording_publish(struct stasis_app_recording *recording, const char *cause)
@@ -595,13 +595,13 @@ enum stasis_app_recording_oper_results stasis_app_recording_operation(
 	recording_operation_cb cb;
 	SCOPED_AO2LOCK(lock, recording);
 
-	if (recording->state < 0 || recording->state >= STASIS_APP_RECORDING_STATE_MAX) {
+	if (recording->state >= STASIS_APP_RECORDING_STATE_MAX) {
 		ast_log(LOG_WARNING, "Invalid recording state %u\n",
 			recording->state);
 		return -1;
 	}
 
-	if (operation < 0 || operation >= STASIS_APP_RECORDING_OPER_MAX) {
+	if (operation >= STASIS_APP_RECORDING_OPER_MAX) {
 		ast_log(LOG_WARNING, "Invalid recording operation %u\n",
 			operation);
 		return -1;
-- 
GitLab