8000 Corrected -Wshift-sign-overflow warnings to prevent negative results by wenxin0319 · Pull Request #27427 · opencv/opencv · GitHub
[go: up one dir, main page]

Skip to content

Corrected -Wshift-sign-overflow warnings to prevent negative results #27427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from

Conversation

wenxin0319
Copy link

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

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 line FIXED_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.

@asmorkalov asmorkalov self-requested a review June 10, 2025 07:00
@asmorkalov asmorkalov self-assigned this Jun 10, 2025
@asmorkalov asmorkalov added category: build/install cleanup Code cleanup (e.g, drop legacy C-API, legacy unmaintained code) labels Jun 10, 2025
@asmorkalov
Copy link
Contributor

cc @vrabaud

@asmorkalov
Copy link
Contributor

Hm.., the constant leaks to Java and other language bindings. Java does not have unsigned types and does not support U as type hint.

@vrabaud
Copy link
Contributor
vrabaud commented Jun 10, 2025

Can you please replace the "U" with "ll" (for long long, cf https://en.cppreference.com/w/cpp/language/integer_literal.html) instead to go to 64 bits? That should solve most problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: build/install cleanup Code cleanup (e.g, drop legacy C-API, legacy unmaintained code)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0