Skip to content

Fix -Walloc-size#12292

Closed
thesamesam wants to merge 1 commit into
curl:masterfrom
thesamesam:alloc-size
Closed

Fix -Walloc-size#12292
thesamesam wants to merge 1 commit into
curl:masterfrom
thesamesam:alloc-size

Conversation

@thesamesam

Copy link
Copy Markdown
Contributor

GCC 14 introduces a new -Walloc-size included in -Wextra which gives:

src/tool_operate.c: In function ‘add_per_transfer’:
src/tool_operate.c:213:5: warning: allocation of insufficient size ‘1’ for type ‘struct per_transfer’ with size ‘480’ [-Walloc-size]
  213 |   p = calloc(sizeof(struct per_transfer), 1);
      |     ^
src/var.c: In function ‘addvariable’:
src/var.c:361:5: warning: allocation of insufficient size ‘1’ for type ‘struct var’ with size ‘32’ [-Walloc-size]
  361 |   p = calloc(sizeof(struct var), 1);
      |     ^

The calloc prototype is:

void *calloc(size_t nmemb, size_t size);
    ```

So, just swap the number of members and size arguments to match the prototype, as
we're initialising 1 struct of size `sizeof(struct ...)`. GCC then sees we're not
doing anything wrong.

@jay

jay commented Nov 8, 2023

Copy link
Copy Markdown
Member

There's a bunch of places where we do that. Why are just those two triggering a warning?

lib/asyn-ares.c:  res = calloc(sizeof(struct thread_data) + namelen, 1);
lib/asyn-thread.c:  *resolver = calloc(1, sizeof(struct resdata));
lib/asyn-thread.c:  struct thread_data *td = calloc(1, sizeof(struct thr
lib/bufq.c:  chunk = calloc(1, sizeof(*chunk) + pool->chunk_size);
lib/bufq.c:    chunk = calloc(1, sizeof(*chunk) + q->chunk_size);
lib/cf-h1-proxy.c:  ts = calloc(1, sizeof(*ts));
lib/cf-h2-proxy.c:  ctx = calloc(sizeof(*ctx), 1);
lib/cf-haproxy.c:  ctx = calloc(sizeof(*ctx), 1);
lib/cf-https-connect.c:  ctx = calloc(sizeof(*ctx), 1);
lib/cf-socket.c:  ctx = calloc(sizeof(*ctx), 1);
lib/cf-socket.c:  ctx = calloc(sizeof(*ctx), 1);
lib/cf-socket.c:  ctx = calloc(sizeof(*ctx), 1);
lib/cf-socket.c:  ctx = calloc(sizeof(*ctx), 1);
lib/cfilters.c:  cf = calloc(sizeof(*cf), 1);
lib/connect.c:  baller = calloc(1, sizeof(*baller) + 1000);

@thesamesam

Copy link
Copy Markdown
Contributor Author

I only hit two in my build and didn't think to check the rest. I'll fix those up later.

@thesamesam thesamesam marked this pull request as draft November 8, 2023 07:33
@bagder

bagder commented Nov 8, 2023

Copy link
Copy Markdown
Member

Talk about silly warning...

GCC 14 introduces a new -Walloc-size included in -Wextra which gives:
```
src/tool_operate.c: In function ‘add_per_transfer’:
src/tool_operate.c:213:5: warning: allocation of insufficient size ‘1’ for type ‘struct per_transfer’ with size ‘480’ [-Walloc-size]
  213 |   p = calloc(sizeof(struct per_transfer), 1);
      |     ^
src/var.c: In function ‘addvariable’:
src/var.c:361:5: warning: allocation of insufficient size ‘1’ for type ‘struct var’ with size ‘32’ [-Walloc-size]
  361 |   p = calloc(sizeof(struct var), 1);
      |     ^
```

The calloc prototype is:
```
void *calloc(size_t nmemb, size_t size);
    ```

So, just swap the number of members and size arguments to match the prototype, as
we're initialising 1 struct of size `sizeof(struct ...)`. GCC then sees we're not
doing anything wrong.
@thesamesam thesamesam marked this pull request as ready for review November 11, 2023 07:46
@bagder bagder closed this in bc8509a Nov 11, 2023
@bagder

bagder commented Nov 11, 2023

Copy link
Copy Markdown
Member

Thanks!

@thesamesam

Copy link
Copy Markdown
Contributor Author

Thank you!

@thesamesam thesamesam deleted the alloc-size branch November 11, 2023 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants