From aa2fabcba58246a8d0a99b1407373c7c3a7c0316 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 17 Apr 2023 15:39:37 -0400 Subject: [PATCH] Fixed undefined left shifting of negative number Added explicit cast to unsigned before doing the left shift. This was caught by UBSan which reported things like: drawing.cpp:361:22: runtime error: left shift of negative value -26214 drawing.cpp:383:22: runtime error: left shift of negative value -78642 --- modules/imgproc/src/drawing.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/imgproc/src/drawing.cpp b/modules/imgproc/src/drawing.cpp index dd596acb23..a0cd8ae772 100755 --- a/modules/imgproc/src/drawing.cpp +++ b/modules/imgproc/src/drawing.cpp @@ -358,7 +358,7 @@ LineAA( Mat& img, Point2l pt1, Point2l pt2, const void* color ) pt1.y ^= pt2.y & j; x_step = XY_ONE; - y_step = (dy << XY_SHIFT) / (ax | 1); + y_step = (int64)((uint64_t)dy << XY_SHIFT) / (ax | 1); pt2.x += XY_ONE; ecount = (int)((pt2.x >> XY_SHIFT) - (pt1.x >> XY_SHIFT)); j = -(pt1.x & (XY_ONE - 1)); @@ -380,7 +380,7 @@ LineAA( Mat& img, Point2l pt1, Point2l pt2, const void* color ) pt2.y ^= pt1.y & i; pt1.y ^= pt2.y & i; - x_step = (dx << XY_SHIFT) / (ay | 1); + x_step = (int64)((uint64_t)dx << XY_SHIFT) / (ay | 1); y_step = XY_ONE; pt2.y += XY_ONE; ecount = (int)((pt2.y >> XY_SHIFT) - (pt1.y >> XY_SHIFT)); -- GitLab