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

Alexander Traud asteriskteam at digium.com
Fri Jan 5 05:52:52 CST 2018


Alexander Traud has uploaded this change for review. ( https://gerrit.asterisk.org/7817


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, 10 insertions(+), 5 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/17/7817/1

diff --git a/main/translate.c b/main/translate.c
index 02717c5..6da60ad 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"
@@ -1415,10 +1414,16 @@
 				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_src = ast_format_get_sample_rate(best);
+				unsigned int gap_selected_dst = ast_format_get_sample_rate(bestdst);
+				unsigned int gap_selected = (gap_selected_src > gap_selected_dst)
+					? gap_selected_src - gap_selected_dst
+					: gap_selected_dst - gap_selected_src;
+				unsigned int gap_current_src = ast_format_get_sample_rate(src);
+				unsigned int gap_current_dst = ast_format_get_sample_rate(dst);
+				unsigned int gap_current = (gap_current_src > gap_current_dst)
+					? gap_current_src - gap_current_dst
+					: gap_current_dst - gap_current_src;
 
 				if (gap_current < gap_selected) {
 					/* better than what we have so far */

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

Gerrit-Project: asterisk
Gerrit-Branch: 15
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib904d9ee0d46b6fdd1476fbc464fbbf813304017
Gerrit-Change-Number: 7817
Gerrit-PatchSet: 1
Gerrit-Owner: Alexander Traud <pabstraud at compuserve.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180105/82930dbc/attachment.html>


More information about the asterisk-code-review mailing list