Keriew / augustus

An open source re-implementation of Caesar III
GNU Affero General Public License v3.0
1.54k stars 119 forks source link

Fix order of arguments to calloc() #1097

Closed sulix closed 4 months ago

sulix commented 4 months ago

calloc() accepts two arguments, nmemb and size, which are multiplied together. nmemb is supposed to be the number of elements, and size the size of each element, but we're passing the size first, then the number of elements.

Because we use sizeof(), this now triggers a warning in gcc:

/home/david/Development/augustus/src/core/memory_block.c: In function ‘core_memory_block_init’:
/home/david/Development/augustus/src/core/memory_block.c:7:35: warning: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
    7 |     block->memory = calloc(sizeof(char), initial_size);
      |                                   ^~~~
/home/david/Development/augustus/src/core/memory_block.c:7:35: note: earlier argument should specify number of elements, later size of each element
/home/david/Development/augustus/ext/zip/zip.c: In function ‘zip_stream_copy’:
/home/david/Development/augustus/ext/zip/zip.c:1925:24: warning: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
 1925 |   *buf = calloc(sizeof(unsigned char), n);
      |                        ^~~~~~~~
/home/david/Development/augustus/ext/zip/zip.c:1925:24: note: earlier argument should specify number of elements, later size of each element

So, while I can't see how this could actually cause an issue, let's get rid of the warning. One of the warnings is in the external zip library. I've just fixed it here, rather than updating the whole library (which seems to have diverged slightly). It seems to have been fixed there, too, though: https://github.com/kuba--/zip/pull/345

crudelios commented 4 months ago

Thanks for this, this was one of the pending warnings I forgot to fix!