crashappsec / libcon4m

Base Compiler and Runtime Support for con4m
Apache License 2.0
0 stars 0 forks source link

Resolve warnings for -Wcalloc-transposed-args #35

Closed ee7 closed 3 weeks ago

ee7 commented 1 month ago

Building the code with GCC 14.1 (the latest stable release, 2024-05-07) produced warnings of the form:

../src/con4m/subproc.c: In function ‘c4m_subproc_new_party_callback’:
../src/con4m/subproc.c:495:56: warning: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
  495 |     c4m_party_t *result = (c4m_party_t *)calloc(sizeof(c4m_party_t), 1);
      |                                                        ^~~~~~~~~~~
../src/con4m/subproc.c:495:56: note: earlier argument should specify number of elements, later size of each element

as that version introduced the -Wcalloc-transposed-args warning, and enables it when -Wextra is used (as our meson.build does).

Resolve those warnings as suggested, so there are no warnings when running ./dev build on x86_64 Linux with GCC 14 installed.

The -Wcalloc-transposed-args option is documented as:

Warn about calls to allocation functions decorated with attribute alloc_size with two arguments, which use sizeof operator as the earlier size argument and don't use it as the later size argument. This is a coding style warning. The first argument to calloc is documented to be number of elements in array, while the second argument is size of each element, so calloc (n, sizeof (int)) is preferred over calloc (sizeof (int), n). If sizeof in the earlier argument and not the latter is intentional, the warning can be suppressed by using calloc (sizeof (struct S) + 0, n) or calloc (1 * sizeof (struct S), 4) or using sizeof in the later argument as well.

ee7 commented 1 month ago

Ah, sure. Done (I think).

Clearly this isn't the most important PR, but I'm making it with a view towards enforcing in CI that a pinned modern versions of GCC and Clang don't produce some agreed subset of warnings. That OK?

ee7 commented 3 weeks ago

It looks like the same change will be made in PR 49 here. Closing.