[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Refactoring of a possibly unsafe pattern for variable initialization via function calls


  • To: Stefano Stabellini <sstabellini@xxxxxxxxxx>, Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Mon, 19 Jun 2023 10:18:11 +0200
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=BvT+tZ0fHR54WGKnWebSqCvCP4gxyCoL07tHTph59i8=; b=Z+3R0OicFs4Q7AbqsY6jVLQnTR2LheEqol/TUavjFC1Sxu3twSTnzGN6HdMOrEdptuPX85lDqXjFjMLUFMVi44n6ia3bKizZVYo8GJYsZol0MZGaI9KUAfYvfyBDDch13mlMRwas5en/tYmiQNnXaJtlwBtN4dC0sYzCdFi/0YKcdXx9VInwka9fr82UzSrEOdCCA4YmCsi3HLW3SSB7Mtoul8iOAqylsFso93fHQPSeKSCiIkke13X1EHA97iKPr0BRT2ZzkRJjmzjS/RpgkZdrd6M0RowCgpVQKXHS8aG0zr46TQGWeBgHrsrExWtWRVq/Kl5dY3tPC7b6vlVStw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ain+BMaT2ua8zSmT0ijwNM++zUaNPduSQ9zQAjjVrBw0WsTo2Uq0CucgSESTAS2E7VVNGUIomjvchQYpMw9IsJFEQ1kMi2QGdHRhtdMkKUeU52hZdd4Ustg9H24UWwjCGhP9QK981Ek4UGY5JLjm76PwiWmHws47v3zRZx6gR513rOIT9dy98K+u99LWlrtt6fK6jU4NeoXGqh7Z9mu8y36lFdFAZefpOg1pT3uTCuKxz5LF54X0DnlcJ1le/WokufEDInZg/h5kse/wWleYkYbnfE+NkYcayy/7IkGr+eE/So5XONf/yFF3RAPizipOW5qrC0NJutcf5VoyRZgbbw==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com;
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Wei Liu <wl@xxxxxxx>, Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Mon, 19 Jun 2023 08:18:26 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 16.06.2023 22:56, Stefano Stabellini wrote:
> On Fri, 16 Jun 2023, Nicola Vetrini wrote:
>> On 16/06/23 09:19, Jan Beulich wrote:
>>> On 15.06.2023 18:39, nicola wrote:
>>>> while investigating possible patches regarding Mandatory Rule 9.1, I
>>>> found the following pattern, that is likely to results in a lot possible
>>>> positives from many (all) static analysis tools for this rule.
>>>>
>>>> This is the current status (taken from `xen/common/device_tree.c:135')
>>>>
>>>>
>>>> const struct dt_property *dt_find_property(const struct dt_device_node
>>>> *np,
>>>>                                              const char *name, u32 *lenp)
>>>> {
>>>>       const struct dt_property *pp;
>>>>
>>>>       if ( !np )
>>>>           return NULL;
>>>>
>>>>       for ( pp = np->properties; pp; pp = pp->next )
>>>>       {
>>>>           if ( dt_prop_cmp(pp->name, name) == 0 )
>>>>           {
>>>>               if ( lenp )
>>>>                   *lenp = pp->length;
>>>>               break;
>>>>           }
>>>>       }
>>>>
>>>>       return pp;
>>>> }
>>>>
>>>>
>>>>
>>>>
>>>> It's very hard to detect that the pointee is always written whenever a
>>>> non-NULL pointer for `lenp' is supplied, and it can safely be read in
>>>> the callee, so a sound analysis will err on the cautious side.
>>>
>>> I'm having trouble seeing why this is hard to recognize: The loop can
>>> only be exited two ways: pp == NULL or with *lenp written.
>>>
>>> For rule 9.1 I'd rather expect the scanning tool (and often the compiler)
>>> to get into trouble with the NULL return value case, and *lenp not being
>>> written yet apparently consumed in the caller. Then, however, ...
>>
>>
>> You're right, I made a mistake, thank you for finding it.
>> I meant to write on `*lenp' in all execution paths.
>> Please, take a look at this revised version:
>>
>>
>> const struct dt_property *dt_find_property(const struct dt_device_node *np,
>>                                            const char *name, u32 *lenp)
>> {
>>     u32 len = 0;
>>     const struct dt_property *pp = NULL;
>>
>>     if ( np )
>>     {
>>         for ( pp = np->properties; pp; pp = pp->next )
>>         {
>>             if ( dt_prop_cmp(pp->name, name) == 0 )
>>             {
>>                 len = pp->length;
>>                 break;
>>             }
>>         }
>>     }
>>
>>     if ( lenp )
>>         *lenp = len;
>>     return pp;
>> }
> 
> Nesting more will make the code less readable and also cause other code
> quality metrics to deteriorate (cyclomatic complexity).
> 
> Would the below work?
> 
> 
> const struct dt_property *dt_find_property(const struct dt_device_node *np,
>                                            const char *name, u32 *lenp)
> {
>     u32 len = 0;
>     const struct dt_property *pp = NULL;
> 
>     if ( !np )
>         return NULL

That's what we started from, but leaving *lenp not written to. How
about ...

>     for ( pp = np->properties; pp; pp = pp->next )

    for ( pp = np ? np->properties : NULL; pp; pp = pp->next )

?

Jan

>     {
>         if ( dt_prop_cmp(pp->name, name) == 0 )
>         {
>             len = pp->length;
>             break;
>         }
>     }
> 
>     if ( lenp )
>         *lenp = len;
>     return pp;
> }
> 




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.