Skip to content

Commit

Permalink
fix warning due to signed integer in malloc size
Browse files Browse the repository at this point in the history
  • Loading branch information
lvandeve authored Aug 21, 2018
1 parent f0010d0 commit 56c07b9
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/zopfli/deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ void OptimizeHuffmanForRle(int length, size_t* counts) {
}
/* 2) Let's mark all population counts that already can be encoded
with an rle code.*/
good_for_rle = (int*)malloc(length * sizeof(int));
good_for_rle = (int*)malloc((unsigned)length * sizeof(int));
for (i = 0; i < length; ++i) good_for_rle[i] = 0;

/* Let's not spoil any of the existing good rle codes.
Expand Down

3 comments on commit 56c07b9

@JPeterMugaas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

( was trying to compile in Win32 and I got this:

D:/msys64/home/jpmugaas/exp/mingw-w64-zopfli/src/zopfli-zopfli-1.0.2/src/zopfli/deflate.c: In function 'OptimizeHuffmanForRle':
D:/msys64/home/jpmugaas/exp/mingw-w64-zopfli/src/zopfli-zopfli-1.0.2/src/zopfli/deflate.c:452:16: warning: argument 1 value '4294967292' exceeds maximum object size 2147483647 [-Walloc-size-larger-than=]
good_for_rle = (int*)malloc((unsigned)length * sizeof(int));

In file included from D:/msys64/home/jpmugaas/exp/mingw-w64-zopfli/src/zopfli-zopfli-1.0.2/src/zopfli/lz77.h:28:0,
              from D:/msys64/home/jpmugaas/exp/mingw-w64-zopfli/src/zopfli-zopfli-1.0.2/src/zopfli/deflate.h:28,
              from D:/msys64/home/jpmugaas/exp/mingw-w64-zopfli/src/zopfli-zopfli-1.0.2/src/zopfli/deflate.c:20:
D:/msys64/mingw32/i686-w64-mingw32/include/stdlib.h:503:17: note: in a call to allocation function 'malloc' declared here
void *__cdecl malloc(size_t _Size);
              ^~~~~~

@JPeterMugaas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exp-fix.patch.txt

Alright, I decided to go ahead and try to combin what you did with making some things ssize_t. I have not seen any warnings from GCC using the combination and hopefully, things will be a little more solid in Win64. Microsoft wrote this https://docs.microsoft.com/en-us/windows/desktop/winprog64/rules-for-using-pointers and https://blogs.msdn.microsoft.com/oldnewthing/20050131-00/?p=36563 .

@divinity76
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. is length EVER going to be negative? why not use size_t in the first place?

Please sign in to comment.