Search code examples
mysqlcsplint

Splint and MySQL: Null storage passed as non-null param


I'm trying to use Splint with a short CGI script, but get this error:

Null storage passed as non-null param: mysql_init(NULL)

mysql_init is defined to return a new value if it's param is NULL, or store the result in the param if it's not. Yet, if I try

MYSQL* connection;
mysql_init(connection);

I will get:

Variable connection used before definition

How to resolve this? One way would of course be to annotate mysql.h so Splint won't complain. Is the my only solution?


Solution

  • You may either edit mysql.h to annotate that parameter as /*@null@*/, or deactivate the warning for that specific line of code:

    /*@-nullpass@*/
    connection = mysql_init(NULL);
    /*@=nullpass@*/
    

    By the way, be careful not to pass uninitialized variables to functions:

    MYSQL* connection; /* this pointer contains garbage at this point */
    mysql_init(connection); /* this may get a segmentation fault */
    

    Instead, you should be doing:

    MYSQL* connection = NULL; /* initialize to NULL */
    connection = mysql_init(connection); /* get a hold of the new object created */