DentonW / DevIL

Developer's Image Library (DevIL) is a cross-platform image library utilizing a simple syntax to load, save, convert, manipulate, filter, and display a variety of images with ease. It is highly portable and has been ported to several platforms.
http://openil.sourceforge.net/
GNU Lesser General Public License v2.1
446 stars 137 forks source link

cast from 'ILubyte* {aka unsigned char*}' to 'ILint {aka int}' loses precision #47

Closed klausenbusk closed 7 years ago

klausenbusk commented 7 years ago

Hello

I get the following, when trying to compile DevIL from ArchLinux.

git clone git@github.com:DentonW/DevIL.git
cd DevIL/DevIL
cmake .
make
[  1%] Building CXX object src-IL/CMakeFiles/IL.dir/src/il_rle.cpp.o
/home/kristian/Hentninger/DevIL/DevIL/src-IL/src/il_rle.cpp: In function 'ILboolean ilRleCompressLine(ILubyte*, ILuint, ILubyte, ILubyte*, ILuint*, ILenum)':
/home/kristian/Hentninger/DevIL/DevIL/src-IL/src/il_rle.cpp:20:52: error: cast from 'ILubyte* {aka unsigned char*}' to 'ILint {aka int}' loses precision [-fpermissive]
  const ILint bmp_pad_to_even = (ILint)(1 - ((ILint)q - *DestWidth) % 2);
                                                    ^
make[2]: *** [src-IL/CMakeFiles/IL.dir/build.make:1479: src-IL/CMakeFiles/IL.dir/src/il_rle.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:86: src-IL/CMakeFiles/IL.dir/all] Error 2
make: *** [Makefile:128: all] Error 2

Removing the q, seems to fix it, but I'm not sure if that yields other problems?

Regards Kristian

andrewmcwatters commented 7 years ago

I'm also experiencing this issue in master 247fd43 from macOS.

Thanks for all your hard work, Denton!

andrewmcwatters commented 7 years ago

Error

[  1%] Building CXX object src-IL/CMakeFiles/IL.dir/src/il_rle.cpp.o
/Users/andrewmcwatters/DevIL/DevIL/src-IL/src/il_rle.cpp:20:45: error: cast from
      pointer to smaller type 'ILint' (aka 'int') loses information
        const ILint bmp_pad_to_even = (ILint)(1 - ((ILint)q - *DestWidth) % 2);
                                                   ^~~~~~~~
1 error generated.
make[2]: *** [src-IL/CMakeFiles/IL.dir/src/il_rle.cpp.o] Error 1
make[1]: *** [src-IL/CMakeFiles/IL.dir/all] Error 2
make: *** [all] Error 2

Fix

/Users/andrewmcwatters/DevIL/DevIL/src-IL/src/il_rle.cpp:20:45

const ILint bmp_pad_to_even = (ILint)(1 - ((ILint)q - *DestWidth) % 2);
const ILint bmp_pad_to_even = (ILint)(1 - ((ILint)(size_t)q - *DestWidth) % 2);

References

[1] http://stackoverflow.com/questions/22419063/error-cast-from-pointer-to-smaller-type-int-loses-information-in-eaglview-mm

Result

DevIL successfully compiles.

DentonW commented 7 years ago

Thanks, Visual Studio complained about that when compiling 64-bit but just gave a warning. I just committed your change.