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

Alexander Traud asteriskteam at digium.com
Fri Jan 5 15:45:07 CST 2018


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


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/40/7840/1

diff --git a/main/translate.c b/main/translate.c
index 0721f07..5198aab 100644
--- a/main/translate.c
+++ b/main/translate.c
@@ -34,7 +34,6 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <math.h>
-#include <stdlib.h>
 
 #include "asterisk/lock.h"
 #include "asterisk/channel.h"
@@ -1406,10 +1405,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/7840
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13.19
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib904d9ee0d46b6fdd1476fbc464fbbf813304017
Gerrit-Change-Number: 7840
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/db68b29c/attachment.html>


More information about the asterisk-code-review mailing list