From 92e483f8ed70d88d4f64337f65bae212502735d4 Mon Sep 17 00:00:00 2001 From: Ganesh Ajjanagadde Date: Sun, 1 Nov 2015 10:43:56 -0500 Subject: all: use FFDIFFSIGN to resolve possible undefined behavior in comparators MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FFDIFFSIGN was created explicitly for this purpose, since the common return a - b idiom is unsafe regarding overflow on signed integers. It optimizes to branchless code on common compilers. FFDIFFSIGN also has the subjective benefit of being easier to read due to lack of ternary operators. Tested with FATE. Things not covered by this are unsigned integers, for which overflows are well defined, and also places where overflow is clearly impossible, e.g an instance where the a - b was being done on 24 bit values. Reviewed-by: Michael Niedermayer Reviewed-by: Clément Bœsch Signed-off-by: Ganesh Ajjanagadde --- libavformat/subtitles.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'libavformat/subtitles.c') diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c index 471d600c10..7c6cd5f353 100644 --- a/libavformat/subtitles.c +++ b/libavformat/subtitles.c @@ -146,12 +146,9 @@ static int cmp_pkt_sub_ts_pos(const void *a, const void *b) { const AVPacket *s1 = a; const AVPacket *s2 = b; - if (s1->pts == s2->pts) { - if (s1->pos == s2->pos) - return 0; - return s1->pos > s2->pos ? 1 : -1; - } - return s1->pts > s2->pts ? 1 : -1; + if (s1->pts == s2->pts) + return FFDIFFSIGN(s1->pos, s2->pos); + return FFDIFFSIGN(s1->pts , s2->pts); } static int cmp_pkt_sub_pos_ts(const void *a, const void *b) -- cgit v1.2.3