translate: Avoid absolute value on unsigned substraction.
authorAlexander Traud <pabstraud@compuserve.com>
Fri, 5 Jan 2018 11:51:47 +0000 (12:51 +0100)
committerCorey Farrell <git@cfware.com>
Tue, 9 Jan 2018 16:37:08 +0000 (11:37 -0500)
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

main/translate.c

index 02717c5..226d098 100644 (file)
@@ -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 @@ void ast_translator_deactivate(struct ast_translator *t)
        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 @@ int ast_translator_best_choice(struct ast_format_cap *dst_cap,
                                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 */