Skip to content
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

Overflow in channel convertion #730

Open
tanguimorvan opened this issue Mar 14, 2023 · 2 comments
Open

Overflow in channel convertion #730

tanguimorvan opened this issue Mar 14, 2023 · 2 comments
Labels
cat/bug But reports and bug fixes

Comments

@tanguimorvan
Copy link

Actual behavior

Channel conversion from boost::gil::packed_channel_value to uint16_t fails due to integer overflow in channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,false> .

Expected behavior

The value should not overflow, producing correct results.

C++ Minimal Working Example

#include <boost/gil.hpp>
namespace gil = boost::gil;
int main
{
    boost::gil::packed_channel_value<10> packedChannelValue(3);
    //Expected 192, got 64
    uint16_t convertedValue = boost::gil::channel_convert<uint16_t>(packedChannelValue);
}

Environment

  • Compiler version: GCC 8.4.0
  • Build settings:
  • Version (Git ref or <boost/version.hpp>): 1.68 but seems to still be present in latest version.
@tanguimorvan
Copy link
Author

tanguimorvan commented Mar 14, 2023

Index: channel_algorithm.hpp
===================================================================
--- channel_algorithm.hpp	(revision 56209)
+++ channel_algorithm.hpp	(working copy)
@@ -237,7 +237,7 @@
 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,false> {
     DstChannelV operator()(SrcChannelV src) const {
         typedef typename base_channel_type<DstChannelV>::type dest_t;
-        return DstChannelV(static_cast<dest_t>( src * unsigned_integral_max_value<DstChannelV>::value) / unsigned_integral_max_value<SrcChannelV>::value);
+        return DstChannelV(static_cast<dest_t>( src ) * unsigned_integral_max_value<DstChannelV>::value / unsigned_integral_max_value<SrcChannelV>::value);
     }
 };

@mloskot mloskot added the cat/bug But reports and bug fixes label Mar 14, 2023
@mloskot
Copy link
Member

mloskot commented Mar 14, 2023

@tanguimorvan Thanks, feel free to open PR with your changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat/bug But reports and bug fixes
Projects
None yet
Development

No branches or pull requests

2 participants