malloc attribute takes no arguments
Asked Answered
E

1

7

I've created a pair of functions:

void destroy_foo(void *ptr);
void *create_foo(void);

As the names suggest, these function akin to malloc and free. I'd like to use the malloc gcc function attribute to inform the compiler of this relationship so that this code would raise a warning with -fanalyzer:

void *ptr = create_foo();
destroy_foo(ptr);
destroy_foo(ptr);

Following the examples from the fore-mentioned link, I did

void *create_foo(void) __attribute__ ((malloc (destroy_foo)));

While gcc (11.4.0) is fine with this, clang (14.0.0) complains

error: 'malloc' attribute takes no arguments
void *create_foo(void) __attribute__ ((malloc (destroy_foo)));
                                       ^

My understanding was that gcc attributes worked just fine with clang.

Ethno answered 30/8, 2023 at 15:31 Comment(0)
H
9

The forms currently implemented by GCC are:

malloc
malloc (deallocator)
malloc (deallocator, ptr-index)

They are documented in Common Function Attributes.

Clang seems to be behind in this game and implements only the first form:

Attributes in Clang

Hunkydory answered 30/8, 2023 at 16:7 Comment(1)
You must remember that these __attribute__s are all very implementation-defined language extensions, so it is not unexpected that different compilers exhibit differing behaviours and levels of support!Ladin

© 2022 - 2024 — McMap. All rights reserved.