Skip to content
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

Transposed argument of calloc detected by GCC >= 14 fails build #37

Open
Lymkwi opened this issue Nov 27, 2024 · 0 comments
Open

Transposed argument of calloc detected by GCC >= 14 fails build #37

Lymkwi opened this issue Nov 27, 2024 · 0 comments

Comments

@Lymkwi
Copy link

Lymkwi commented Nov 27, 2024

Hi,

Attempting to build readsb-protobuf with GCC 14.2.1 currently results in the following error:

$ make
[...]
gcc -DMODES_READSB_VERSION=\"'v4.0.4'\" -DMODES_READSB_VARIANT=\"Mictronics\" -D_GNU_SOURCE -std=c11 -O2 -g -W -D_DEFAULT_SOURCE -Wall -Werror -fno-common -Wmissing-declarations -c net_io.c -o net_io.o
net_io.c: In function ‘serviceInit’:
net_io.c:116:35: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args]
  116 |     if (!(service = calloc(sizeof (*service), 1))) {
      |                                   ^
net_io.c:116:35: note: earlier argument should specify number of elements, later size of each element
[...]

The reason is a transposed argument in the call to calloc at line 116 of net_io.c. The first argument should be the number of members, and the second one their individual size. This error is now picked up by GCC 14 and warnings are treated as errors per -Werror.

Reproduction

  • Clone the repository on a system with GCC 14.2.1.
  • Get inside of the directory
  • make

Background

This repository is currently pointed to by Archlinux User Repository's readsb-git package. Attempting to build it on an up-to-date installation of ArchLinux, however, fails.

Installing gcc version 13 on ArchLinux via the extra/gcc13 package, and exporting CC with value gcc13 fixes the build problem.

Solutions

Either one of these:

  • Change calloc argument order and preserve zero-intialization of memory
diff --git a/net_io.c b/net_io.c
index 167b876e..34502f3f 100644
--- a/net_io.c
+++ b/net_io.c
@@ -113,7 +113,7 @@ struct net_service *serviceInit(const char *descr, struct net_writer *writer, he
         exit(1);
     }

-    if (!(service = calloc(sizeof (*service), 1))) {
+    if (!(service = calloc(1, sizeof (*service)))) {
         fprintf(stderr, "Out of memory allocating service %s\n", descr);
         exit(1);
     }
  • Don't use calloc if you don't actually care about zero-initializing memory)
diff --git a/net_io.c b/net_io.c
index 167b876e..69815cd9 100644
--- a/net_io.c
+++ b/net_io.c
@@ -113,7 +113,7 @@ struct net_service *serviceInit(const char *descr, struct net_writer *writer, he
         exit(1);
     }

-    if (!(service = calloc(sizeof (*service), 1))) {
+    if (!(service = malloc(sizeof (*service)))) {
         fprintf(stderr, "Out of memory allocating service %s\n", descr);
         exit(1);
     }

i could open a PR but for such a minimal change it feels overkill.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant