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

Compile warning due to calloc parameter reversal #35

Open
garybuhrmaster opened this issue Jun 30, 2024 · 3 comments
Open

Compile warning due to calloc parameter reversal #35

garybuhrmaster opened this issue Jun 30, 2024 · 3 comments

Comments

@garybuhrmaster
Copy link

garybuhrmaster commented Jun 30, 2024

Compile warning due to calloc parameter reversal

When compiling libhdhomerun with a recent gcc one may receive warnings of the form:

hdhomerun_discover.c: In function ‘hdhomerun_discover_sock_add_ipv6’:
hdhomerun_discover.c:123:99: warning: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
  123 |         struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(sizeof(struct hdhomerun_discover_sock_t), 1);
      |                                                                                                   ^~~~~~
hdhomerun_discover.c:123:99: note: earlier argument should specify number of elements, later size of each element

Both hdhomerun_discover.c and hdhomerun_sock_netdevice.c have the reversed order of the parameters (other uses in the library call calloc correctly)

In practice the resulting buffer will be the same size (n elements of size 1, and 1 element of size n, should end up with the same number of bytes), but recent gcc's can detect the misuse when the -Wcalloc-transposed-args is used.

Proposed fix (compile tested only):

diff --git a/hdhomerun_discover.c b/hdhomerun_discover.c
index 8dae716..24e3cb5 100644
--- a/hdhomerun_discover.c
+++ b/hdhomerun_discover.c
@@ -120,7 +120,7 @@ static void hdhomerun_discover_sock_add_ipv6(void *arg, uint32_t ifindex, const
        }
 
        /* Create socket. */
-       struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(sizeof(struct hdhomerun_discover_sock_t), 1);
+       struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(1, sizeof(struct hdhomerun_discover_sock_t));
        if (!dss) {
                hdhomerun_debug_printf(ds->dbg, "discover: resource error\n");
                return;
@@ -181,7 +181,7 @@ static void hdhomerun_discover_sock_add_ipv4(void *arg, uint32_t ifindex, const
        }
 
        /* Create socket. */
-       struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(sizeof(struct hdhomerun_discover_sock_t), 1);
+       struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(1, sizeof(struct hdhomerun_discover_sock_t));
        if (!dss) {
                hdhomerun_debug_printf(ds->dbg, "discover: resource error\n");
                return;
@@ -347,7 +347,7 @@ static void hdhomerun_discover_sock_detect_ipv6_localhost(struct hdhomerun_disco
                return;
        }
 
-       struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(sizeof(struct hdhomerun_discover_sock_t), 1);
+       struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(1, sizeof(struct hdhomerun_discover_sock_t));
        if (!dss) {
                return;
        }
@@ -378,7 +378,7 @@ static void hdhomerun_discover_sock_detect_ipv4_localhost(struct hdhomerun_disco
                return;
        }
 
-       struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(sizeof(struct hdhomerun_discover_sock_t), 1);
+       struct hdhomerun_discover_sock_t *dss = (struct hdhomerun_discover_sock_t *)calloc(1, sizeof(struct hdhomerun_discover_sock_t));
        if (!dss) {
                return;
        }
@@ -820,7 +820,7 @@ static void hdhomerun_discover_recv_internal_device_type(struct hdhomerun_discov
                p = p->next;
        }
 
-       struct hdhomerun_discover2_device_type_t *new_type = (struct hdhomerun_discover2_device_type_t *)calloc(sizeof(struct hdhomerun_discover2_device_type_t), 1);
+       struct hdhomerun_discover2_device_type_t *new_type = (struct hdhomerun_discover2_device_type_t *)calloc(1, sizeof(struct hdhomerun_discover2_device_type_t));
        if (!new_type) {
                return;
        }
@@ -1231,8 +1231,8 @@ static bool hdhomerun_discover_recvfrom(struct hdhomerun_discover_t *ds, struct
                return activity;
        }
 
-       struct hdhomerun_discover2_device_t *device = (struct hdhomerun_discover2_device_t *)calloc(sizeof(struct hdhomerun_discover2_device_t), 1);
-       struct hdhomerun_discover2_device_if_t *device_if = (struct hdhomerun_discover2_device_if_t *)calloc(sizeof(struct hdhomerun_discover2_device_if_t), 1);
+       struct hdhomerun_discover2_device_t *device = (struct hdhomerun_discover2_device_t *)calloc(1, sizeof(struct hdhomerun_discover2_device_t));
+       struct hdhomerun_discover2_device_if_t *device_if = (struct hdhomerun_discover2_device_if_t *)calloc(1, sizeof(struct hdhomerun_discover2_device_if_t));
        if (!device || !device_if) {
                if (device) {
                        free(device);
diff --git a/hdhomerun_sock_netdevice.c b/hdhomerun_sock_netdevice.c
index cd3eb2b..0d8901e 100644
--- a/hdhomerun_sock_netdevice.c
+++ b/hdhomerun_sock_netdevice.c
@@ -59,7 +59,7 @@ bool hdhomerun_local_ip_info2(int af, hdhomerun_local_ip_info2_callback_t callba
        }
 
        int ifreq_buffer_size = 128 * sizeof(struct ifreq);
-       char *ifreq_buffer = (char *)calloc(ifreq_buffer_size, 1);
+       char *ifreq_buffer = (char *)calloc(1, ifreq_buffer_size);
        if (!ifreq_buffer) {
                close(sock);
                return false;
@garybuhrmaster
Copy link
Author

minor edit to add a space after a comma in the first loc change for legibility.

@nickkelsey
Copy link
Contributor

Good catch... will get it changed. Thanks

@garybuhrmaster
Copy link
Author

Good catch... will get it changed. Thanks

Thanks. Obviously low priority in the grand scheme of things.

TBH, I didn't catch anything (and I probably would not have on my own as I usually use others packages), but gcc did as part of my initial/early/preliminary builds of packages for (the future) CentOS Stream 10 that I find of interest at this time, and it was a warning that I could easily understand how to address.

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

2 participants