From b45fb3a6ec107c9a36a0c7badeff881410d58d9d Mon Sep 17 00:00:00 2001 From: Alexander Traud Date: Fri, 5 Jan 2018 12:51:47 +0100 Subject: 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 --- main/translate.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/main/translate.c b/main/translate.c index 0721f07b0..240e4956a 100644 --- a/main/translate.c +++ b/main/translate.c @@ -34,7 +34,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include #include #include -#include #include "asterisk/lock.h" #include "asterisk/channel.h" @@ -1322,6 +1321,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, @@ -1406,10 +1412,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 */ -- cgit v1.2.3