Is there an equivalent to __attribute__((ns_returns_retained)) for a malloc'd pointer?

时光毁灭记忆、已成空白 提交于 2019-12-05 08:22:46

The function annotation ownership_returns(malloc) will tell the Clang static analyser that the function returns a pointer that should be passed to free() at some point (or a function with ownership_takes(malloc, ...)). For example:

void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
void __attribute((ownership_takes(malloc, 1))) my_free(void *);
...
void af1() {
    int *p = my_malloc(1);
    return; // expected-warning{{Potential leak of memory pointed to by}}
}
void af2() {
    int *p = my_malloc(1);
    my_free(p);
    return; // no-warning
}

(See the malloc-annotations.c test file for some more examples of their use.)

At the moment, these annotations only take effect when the alpha.unix.MallocWithAnnotations checker is run (which is not run by default). If you're using Xcode, you'll need to add -Xclang -analyzer-checker=alpha.unix.MallocWithAnnotations to your build flags.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!