[splint-discuss] incomplete deallocation

Michael Wojcik Michael.Wojcik at MicroFocus.com
Tue Nov 11 06:17:11 PST 2008


> From: splint-discuss-bounces at cs.virginia.edu [mailto:splint-discuss-
> bounces at cs.virginia.edu] On Behalf Of Greg White
> Sent: Monday, 10 November, 2008 20:32
> 
> > Date: Mon, 10 Nov 2008 07:04:21 -0800
> > From: Michael.Wojcik at microfocus.com
> > To: splint-discuss at cs.virginia.edu
> > A better way to
> > initialize dynamically-allocated structures is with structure copy:
> >
> > 	static const nk_hdr nk_hdr0 = {0};
> > 	...
> > 	n = malloc(sizeof *n);
> > 	*n = nk_hdr0;
> 
> OK I made the changes you suggested but splint is now giving me the
> following message:
> Initializer block for nk_hdr0 has 1 field, but nk_hdr has 5 fields: 0
> Initializer does not set every field in the structure. (Use -
> fullinitblock  to inhibit warning)
> 
> Code:
> static const nk_hdr nk_hdr0 = {0};
> nk_hdr *n = NULL;
> 
>   n = (nk_hdr*) malloc(sizeof(*n));
>   if (n == NULL) {
>     printf("can't allocate memory for n\n");
>     return -1;
>   }
>   *n = nk_hdr0;
> 
> Did I do something wrong?

No. This is a misfeature in Splint. Compound initializers with fewer
fields than the aggregate type being initialized are well-defined by the
ISO C standard, and the "{0}" initializer in particular is a
widely-recognized idiom. (Splint should really silently ignore any
"short" initializer that ends with a 0 constant.)

This sort of false-positive message is a common problem with lint-style
static analyzers, which is why they introduced source code annotations.

The proper way to fix it is with an annotation. This should do it:

	/*@ -fullinitblock @*/
	static const nk_hdr nk_hdr0 = {0};
	/*@ +fullinitblock @*/

or alternatively:

	static const nk_hdr nk_hdr0 = /*@i1@*/ {0};

(The "i1" annotation means "ignore exactly one item from here to the end
of this line".)

You can also just remove the "= {0}" part, since static variables are
implicitly initialized to {0} if no explicit initializer is present.

-- 
Michael Wojcik
Principal Software Systems Developer, Micro Focus




More information about the splint-discuss mailing list