cownershipsplint

Transfer ownership of storage in Splint


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.


Solution

  • 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)