[Asterisk-code-review] translate: Avoid absolute value on unsigned substraction. (asterisk[15.2])

Jenkins2 asteriskteam at digium.com
Tue Jan 9 13:31:36 CST 2018


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/7818 )

Change subject: translate: Avoid absolute value on unsigned substraction.
......................................................................

translate: Avoid absolute value on unsigned substraction.

ast_format_get_sample_rate(.) returns an unsigned type. The difference of a
substraction between two unsigned types does not get implicitly converted to a
signed type. Therefore, using abs(.) did not make sense.

ASTERISK-27549

Change-Id: Ib904d9ee0d46b6fdd1476fbc464fbbf813304017
---
M main/translate.c
1 file changed, 9 insertions(+), 5 deletions(-)

Approvals:
  Richard Mudgett: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, but someone else must approve
  Sean Bright: Looks good to me, approved
  Jenkins2: Approved for Submit



diff --git a/main/translate.c b/main/translate.c
index 02717c5..226d098 100644
--- a/main/translate.c
+++ b/main/translate.c
@@ -32,7 +32,6 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <math.h>
-#include <stdlib.h>
 
 #include "asterisk/lock.h"
 #include "asterisk/channel.h"
@@ -1331,6 +1330,13 @@
 	AST_RWLIST_UNLOCK(&translators);
 }
 
+/*! Calculate the absolute difference between sample rate of two formats. */
+#define format_sample_rate_absdiff(fmt1, fmt2) ({ \
+	unsigned int rate1 = ast_format_get_sample_rate(fmt1); \
+	unsigned int rate2 = ast_format_get_sample_rate(fmt2); \
+	(rate1 > rate2 ? rate1 - rate2 : rate2 - rate1); \
+})
+
 /*! \brief Calculate our best translator source format, given costs, and a desired destination */
 int ast_translator_best_choice(struct ast_format_cap *dst_cap,
 	struct ast_format_cap *src_cap,
@@ -1415,10 +1421,8 @@
 				beststeps = matrix_get(x, y)->multistep;
 			} else if (matrix_get(x, y)->table_cost == besttablecost
 					&& matrix_get(x, y)->multistep == beststeps) {
-				int gap_selected = abs(ast_format_get_sample_rate(best)
-					- ast_format_get_sample_rate(bestdst));
-				int gap_current = abs(ast_format_get_sample_rate(src)
-					- ast_format_get_sample_rate(dst));
+				unsigned int gap_selected = format_sample_rate_absdiff(best, bestdst);
+				unsigned int gap_current = format_sample_rate_absdiff(src, dst);
 
 				if (gap_current < gap_selected) {
 					/* better than what we have so far */

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

Gerrit-Project: asterisk
Gerrit-Branch: 15.2
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib904d9ee0d46b6fdd1476fbc464fbbf813304017
Gerrit-Change-Number: 7818
Gerrit-PatchSet: 2
Gerrit-Owner: Alexander Traud <pabstraud at compuserve.com>
Gerrit-Reviewer: Alexander Traud <pabstraud at compuserve.com>
Gerrit-Reviewer: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Matthew Fredrickson <creslin at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Sean Bright <sean.bright at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180109/19f822ea/attachment.html>


More information about the asterisk-code-review mailing list