Transfer ownership of storage in Splint

孤者浪人 提交于 2019-12-13 00:42:52

问题


Using a simple linked list implementation in C, how do I tell Splint that I am transfer ownership of data?

typedef struct {
    void* data;
    /*@null@*/ void* next;
} list;

static /*@null@*/ list* new_list(/*@notnull@*/ void* data)
{
    list* l;

    l = malloc(sizeof(list));

    if (l == NULL)
        return NULL;

    l->next = NULL;
    l->data = data;

    return l;
}

I get this error message:

Implicitly temp storage data assigned to implicitly
                             only: list->data = data
  Temp storage (associated with a formal parameter) is transferred to a
  non-temporary reference. The storage may be released or new aliases created.
  (Use -temptrans to inhibit warning)

I want to tell Splint that responsibility of freeing data is transfered to the list data-structure.


回答1:


The solution is in the Splint manual for function interfaces. Basically, change the function signature to this:

static /*@null@*/ list* new_list(/*@notnull@*/ /*@only@*/ void* data)
    /*@defines result->data @*/

Although we'll get a new error when doing this:

int main()
{
    list* l = new_list("hej");

    return 0;
}


 Observer storage passed as only param:
                              new_list ("hej")
  Observer storage is transferred to a non-observer reference. (Use
  -observertrans to inhibit warning)


来源:https://stackoverflow.com/questions/25351332/transfer-ownership-of-storage-in-splint

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