-
Notifications
You must be signed in to change notification settings - Fork 329
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
OptimizeHuffmanForRle #150
Comments
Thanks for pointing this out. The warning afaik was due to length being a signed integer. Does commit 56c07b9 fix it for you? |
I don't think so. When I compiled for Win32 using GCC, I got this: 42%] Building C object CMakeFiles/zopflilib_obj.dir/src/zopfli/lz77.c.obj
|
Isn't the problem rather that in the loop right above length is counted down until it reaches -1. The compiler cannot see if the loop will always break early, so it warns that multiplying -1 with sizeof(int) is unlikely to be meaningful? Perhaps something like: for (; length > 0; --length) {
if (counts[length - 1] != 0) {
/* Now counts[0..length - 1] does not have trailing zeros. */
break;
}
}
if (length <= 0) {
return;
} |
I still have to run a test, but I have to ask this question, why does the procedure have this declaration? void OptimizeHuffmanForRle(int length, size_t* counts) The problem in Windows 64-bit is that an int can only accommodate 2GB's of data (2 to 31's power). If you use a size_t for the length parameter, you could get up 2 to the 63rd power as a maximum data size. |
I would think the correct type might be a ssize_t (signed size_t). |
Well, anyway, I did test your proposed loop with the compiler and it doesn't give warnings. But I still don't like that procedure because of that concern I mentioned. I'm going to look at something else. |
I got that warning too at some point, and for me making sure that the malloc gets something unsigned fixed it, it seemed as if the warning came from having int. Though you still seem to get it for (unsigned)length * sizeof(int). Maybe casting the entire argument to (unsigned) helps? No need to have different integer types for different platforms with #ifdefs imho. We'll find a type that works for everyone :) The length is always small, it's bounded to the max amount of huffman symbols, so 32 bits are more than enough. No need to change the signature of the function, that may only cause warnings elsewhere. |
Please forgive any confusion on my part since I don't know the algorithm. But the reason I was thinking some things needed to be 8 bytes was that I know that an "int" is 8-bytes wide on many 64-bit systems (not 4-bytes like Windows). |
8 bytes are definitely not needed in this algorithm, a cast to fix the warning is good here. Does changing fix the warning in both win64 and win32 for you? Thanks |
good_for_rle = (int*)malloc((unsigned)(length * sizeof(int))); Gives warning for Win32 but not Win64. D:/msys64/home/jpmugaas/exp/mingw-w64-zopfli/src/zopfli-zopfli-1.0.2/src/zopfli/deflate.c: In function 'OptimizeHuffmanForRle':
|
OptimizeHuffmanForRle is only called from TryOptimizeHuffmanForRle with fixed length values of ZOPFLI_NUM_LL and ZOPFLI_NUM_D. Rather than fighting with compilers about warnings in the malloc call, use a fixed size array the size of the larger of these. Fixes google#150
OptimizeHuffmanForRle is only called from TryOptimizeHuffmanForRle with fixed length values of ZOPFLI_NUM_LL and ZOPFLI_NUM_D. To avoid warnings about the malloc call, use a fixed size array the size of the larger of these. Fixes google#150
I got a ping from a comment on the related commit, which made me take another look at this. I realized I have a branch with the change here. Happy to make a PR if there is any interest, but it's only 3 lines changed. |
When compiling zopfli for Win64 using MINGW-W64, I got a warning about invalid typecasting and a malloc value being out of potential range. I fixed this issue with a patch I'm proving. Please feel free to use it. The thing is that in Win64, malloc takes an 8-byte unsigned value while in Win32, it takes a 4-byte value
. Int is always 4 bytes even in Win64. That effects OptimizeHuffmanForRle mostly.
win32-fixes.patch.txt
The text was updated successfully, but these errors were encountered: