是否有等效于__attribute __((ns_returns_retained))的malloc指针? [英] Is there an equivalent to __attribute__((ns_returns_retained)) for a malloc'd pointer?

查看:130
本文介绍了是否有等效于__attribute __((ns_returns_retained))的malloc指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找类似

-(SomeStruct *) structFromInternals __attribute__((returns_malloced_ptr))
{
    SomeStruct *ret = malloc(sizeof(SomeStruct));
    //do stuff
    return ret;
}

可以缓解叮当声静态分析仪的野兽。
我可以找到的唯一可行的属性链接是对于GCC ,但它甚至不包含 ns_returns_retained ,我认为这是扩展名。

to soothe the clang static analyzer beasts. The only viable attributes link I can find is for GCC, but it doesn't even include ns_returns_retained, which is in an extension, I assume.

编辑:

关于为什么需要这样做,我有一种情况,我无法在简单的情况下进行复制,因此它可能与Objective-c中的ac lib有关。 C项目...要点是,我得到一个静态分析器,警告createStruct中的malloc已泄漏:

as to why this is needed, I have a scenario that I can't repro in a simple case, so it may have to do with a c lib in an Objective-C project... The gist is, I get a static analyzer warning that the malloc in createStruct is leaked:

typedef struct{
    void * data;
    size_t len;
}MyStruct;

void destroyStruct(MyStruct * s)
{
    if (s && s->data) {
        free(s->data);
    }
    if (s) {
        free(s);
    }
}
MyStruct * createStructNoCopy(size_t len, void * data)
{
    MyStruct * retStruct = malloc(sizeof(MyStruct));
    retStruct->len = len;
    retStruct->data = data;
    return retStruct;
}
MyStruct * createStruct(size_t len, void * data)
{
    char * tmpData = malloc(len);
    memcpy(tmpData, data, len);
    return createStructNoCopy(len, tmpData);
}
MyStruct * copyStruct(MyStruct * s)
{
    return createStruct(s->len, s->data);
}


推荐答案

函数注释 ownership_returns(malloc)会告诉Clang静态分析器,该函数返回的指针应在某个时刻传递给 free() (或具有 ownership_takes(malloc,...)的函数)。例如:

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
}

(请参见 malloc-annotations.c 测试文件以获取更多用法示例。)

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

目前,这些注释仅在时生效alpha.unix.MallocWithAnnotations 检查器已运行(默认情况下未运行)。如果您使用的是Xcode,则需要在构建标志中添加 -Xclang -analyzer-checker = alpha.unix.MallocWithAnnotations

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.

这篇关于是否有等效于__attribute __((ns_returns_retained))的malloc指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆