Corrected -Wshift-sign-overflow warnings to prevent negative results #27427
+5
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.
Fix signed shift overflow errors in OpenCV headers
Address
-Wshift-sign-overflow
errors caused by signed shift operations resulting in negative values. The following issues were resolved:In
opencv2/core/mat.hpp
, the lineFIXED_TYPE = (32768 << KIND_SHIFT)
caused the error: "signed shift result (0x80000000) sets the sign bit of the shift expression's type ('int') and becomes negative [-Werror,-Wshift-sign-overflow]".In
opencv2/core/core_c.h
,(_elem->flags) = (((_elem->flags) & ((1 << 26) - 1)) | (1 << ((sizeof(int) * (8)) - (1))));
caused the error: "signed shift result (0x80000000) sets the sign bit of the shift expression's type ('int') and becomes negative [-Werror,-Wshift-sign-overflow]".In
opencv2/videoio.hpp
,enum { CAP_OPENNI_DEPTH_GENERATOR = (1 << 31),
caused the error: "signed shift result (0x80000000) sets the sign bit of the shift expression's type ('int') and becomes negative [-Werror,-Wshift-sign-overflow]".In
opencv2/imgproc/types_c.h
,CV_CANNY_L2_GRADIENT = (1 << 31)
caused the error: "signed shift result (0x80000000) sets the sign bit of the shift expression's type ('int') and becomes negative [-Werror,-Wshift-sign-overflow]".In
opencv2/videoio/videoio_c.h
,CV_CAP_OPENNI_DEPTH_GENERATOR = (1 << 31),
caused the error: "signed shift result (0x80000000) sets the sign bit of the shift expression's type ('int') and becomes negative [-Werror,-Wshift-sign-overflow]".These changes ensure that the code compiles without errors when
-Werror
is enabled.