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

Fix -Walloc-size #12292

Closed
wants to merge 1 commit into from
Closed

Fix -Walloc-size #12292

wants to merge 1 commit into from

Conversation

thesamesam
Copy link
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
Copy link
Member

jay commented Nov 8, 2023

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
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
Copy link
Member

bagder commented Nov 8, 2023

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
Copy link
Member

bagder commented Nov 11, 2023

Thanks!

@thesamesam
Copy link
Contributor Author

Thank you!

@thesamesam thesamesam deleted the alloc-size branch November 11, 2023 22:39
zuoxiaofeng pushed a commit to zuoxiaofeng/curl that referenced this pull request Nov 28, 2023
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.

Closes curl#12292
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.

None yet

3 participants