Splint doesn't know that library function is freeing memory

172 Views Asked by At

When using MySQL in C you free the memory using the MySQL API, like so:

MYSQL* connection = NULL;
connection = mysql_init(NULL);
// Stuff...
mysql_close(connection);

But Splint doesn't know that mysql_close is in fact freeing memory, so I get this error:

Fresh storage connection not released before return
A memory leak has been detected. Storage allocated locally is 
not released before the last reference to it is lost. (Use 
-mustfreefresh to inhibit warning)

How do I tell Splint that mysql_close is deallocating memory? A special annotation to the mysql.h file?

EDIT: OK, maybe the releases *p annotation, if that can be used in a header file. Will try.

EDIT 2: Added /*@releases *sock@*/ to mysql.h, but now get this error:

Releases clauses includes *sock of non-dynamically allocated
              type MYSQL
A declaration uses an invalid annotation. (Use -annotationerror to inhibit
warning)

This is the signaure of mysql_close:

void STDCALL mysql_close(/*@notnull@*/ MYSQL *sock) /*@releases *sock@*/;
1

There are 1 best solutions below

2
On BEST ANSWER

I believe that proper annotation would be:

void STDCALL mysql_close(/*@special@*/ /*@notnull@*/ MYSQL *sock)
    /*@releases sock@*/;

The key, that you have missed is /*@special@*/ annotation, that is required to "activate" so called state clauses. From Splint's documentation, 7.4 State Clauses:

The /*@special@*/ annotation is used to mark a parameter, global variable, or return value that is described using state clauses.