On Tue, 2025-10-21 at 10:06 -0700, Joe Perches wrote:
> On Tue, 2025-10-21 at 17:00 +0530, Ally Heev wrote:
> > uninitialized pointers with __free attribute can cause undefined
> > behaviour as the memory allocated to the pointer is freed
> > automatically when the pointer goes out of scope.
> > add check in checkpatch to detect such issues
> 
> Seems sensible.  Couple minor points below:
> 
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
> > @@ -7721,6 +7721,12 @@ sub process {
> >                             ERROR("MISSING_SENTINEL", "missing sentinel in 
> > ID array\n" . "$here\n$stat\n");
> >                     }
> >             }
> > +
> > +# check for uninitialized pointers with __free attribute
> > +           if ($line =~ 
> > /\s*$Type\s*($Ident)\s+__free\s*\(\s*$Ident\s*\)\s*;/) {
> 
> The leading \s* isn't useful, but \b should be used.

Sure

> 
> Perhaps verify that $Type is a pointer as well
> 
>               if ($line =~ 
> /\b($Type)\s*($Ident)\s*__free\s*\(\s*$Ident\s*\)\s*;/ &&
>                   $1 =~ /\*\s*$/) {
> 
> to avoid things like:
> 
> drivers/net/ethernet/microsoft/mana/gdma_main.c:      cpumask_var_t cpus 
> __free(free_cpumask_var);
> 
> 
> > +                   WARN("UNINITIALIZED_PTR_WITH_FREE",
> > +                         "pointer '$1' with __free attribute should be 
> > initialized\n" . $herecurr);
> 
>                       pointer '$2' etc
> 

Nice find. Thanks! 

> And this would not find uses like the below where another definition
> is done before a definition with __free on the same line:
> 
> crypto/testmgr.c:       u8 *ptr, *key __free(kfree);

I will add the check for each pointer defined on the same line

Reply via email to