Skip to contentSkip to conversation

objtool: Fix calloc call for new -Walloc-size

PATCH Applied Sam James 4 participants 4 messages Nov 7, 2023 – Nov 20, 2023 objtool

lore
Share & tools
lore

Sam James

author
#0 lore raw

PATCH 1/1 objtool: Fix calloc call for new -Walloc-size

GCC 14 introduces a new -Walloc-size included in -Wextra which errors out like:

```
check.c: In function ‘cfi_alloc’:
check.c:294:33: error: allocation of insufficient size ‘1’ for type ‘struct cfi_state’ with size ‘320’ [-Werror=alloc-size]
  294 |         struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 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.

---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/objtool/check.c +1 -1
index e94756e09ca9..548ec3cd7c00 100644
@@ -291,7 +291,7 @@ static void init_insn_state(struct objtool_file *file, struct insn_state *state,
 
 static struct cfi_state *cfi_alloc(void)
 {
-	struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
+	struct cfi_state *cfi = calloc(1, sizeof(struct cfi_state));
 	if (!cfi) {
 		WARN("calloc failed");
 		exit(1);
Signature · 1 line
2.42.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 28 lines · Sam James

GCC 14 introduces a new -Walloc-size included in -Wextra which errors out like:

```
check.c: In function ‘cfi_alloc’:
check.c:294:33: error: allocation of insufficient size ‘1’ for type ‘struct cfi_state’ with size ‘320’ [-Werror=alloc-size]
  294 |         struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 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.

---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/objtool/check.c +0 -0
index e94756e09ca9..548ec3cd7c00 100644

tools/objtool/check.c static void init_insn_state(struct objtool_file *file, struct insn_state *state, · line 297

Quoted 8 lines
tools/objtool/check.c +1 -1
@@ -291,7 +291,7 @@ static void init_insn_state(struct objtool_file *file, struct insn_state *state,
 
 static struct cfi_state *cfi_alloc(void)
 {
-	struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
+	struct cfi_state *cfi = calloc(1, sizeof(struct cfi_state));
 	if (!cfi) {
 		WARN("calloc failed");
 		exit(1);

Thanks!

Peter are you able to grab this or should I put it in my tree first?

Signature · 1 line
Josh
maintainer
#2 lore raw
Quoted 40 lines · Josh Poimboeuf
Quoted 28 lines · Sam James

GCC 14 introduces a new -Walloc-size included in -Wextra which errors out like:

```
check.c: In function ‘cfi_alloc’:
check.c:294:33: error: allocation of insufficient size ‘1’ for type ‘struct cfi_state’ with size ‘320’ [-Werror=alloc-size]
  294 |         struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 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.

---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/objtool/check.c +0 -0
index e94756e09ca9..548ec3cd7c00 100644

tools/objtool/check.c static void init_insn_state(struct objtool_file *file, struct insn_state *state, · line 297

Quoted 8 lines
tools/objtool/check.c +1 -1
@@ -291,7 +291,7 @@ static void init_insn_state(struct objtool_file *file, struct insn_state *state,
 
 static struct cfi_state *cfi_alloc(void)
 {
-	struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
+	struct cfi_state *cfi = calloc(1, sizeof(struct cfi_state));
 	if (!cfi) {
 		WARN("calloc failed");
 		exit(1);

Thanks!

Quoted 3 lines

Peter are you able to grab this or should I put it in my tree first?

Got it, thanks!

merge bot Applied
#3 lore raw
In reply to Sam James
The following commit has been merged into the objtool/core branch of tip:

The following commit has been merged into the objtool/core branch of tip:

Commit-ID:     e2e13630f93d942d02f3b3f98660228a3545c60e
Gitweb:        https://git.kernel.org/tip/e2e13630f93d942d02f3b3f98660228a3545c60e
Author:        Sam James <[email protected]>
AuthorDate:    Tue, 07 Nov 2023 20:55:00 
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 17 Nov 2023 10:54:50 +01:00

objtool: Fix calloc call for new -Walloc-size

GCC 14 introduces a new -Walloc-size included in -Wextra which errors out like:

```
check.c: In function ‘cfi_alloc’:
check.c:294:33: error: allocation of insufficient size ‘1’ for type ‘struct cfi_state’ with size ‘320’ [-Werror=alloc-size]
  294 |         struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 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.

---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/objtool/check.c +1 -1
index e94756e..548ec3c 100644
@@ -291,7 +291,7 @@ static void init_insn_state(struct objtool_file *file, struct insn_state *state,
 
 static struct cfi_state *cfi_alloc(void)
 {
-	struct cfi_state *cfi = calloc(sizeof(struct cfi_state), 1);
+	struct cfi_state *cfi = calloc(1, sizeof(struct cfi_state));
 	if (!cfi) {
 		WARN("calloc failed");
 		exit(1);