[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: Julien Grall <julien@xxxxxxx>
  • From: Luca Fancellu <Luca.Fancellu@xxxxxxx>
  • Date: Mon, 19 Jun 2023 08:31:25 +0000
  • Accept-language: en-GB, en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.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=8+MM568G307h7Hdb9YhZKvFBcPqXRGgeXKReaksfkoM=; b=Wa0bc4XdUPbnCLNmOdEmIuzDwc/mq0KmX23G4eVgXSW7f5KLTl/scu3fLuKnQ7dtHnE1+3ERw4cyJXv8y0jVnqN2Ybo7EZon7d6nxAg/nw34qJb2rn4sirJAqyKRSgfenM9UWAB31/wkXnu7OUs2OFd5owlqD0ebe5PX7b2gHuJ4uUGmZ/pU9ttvvg7cfVKJ/nw7dTC59NY/p6Ix64/7MfxDjnMTFCI9NJZpcsPv4nqBizoJc45EPx06pgZF8PMxCJYLX/TgY52Sbxw16ZhpWQOxOxIjDr/r8GP+ZZhPkDU0HgGz/bYm6xFW3lgRuicvGMFkWqvjY3Dlu0I7sxTN6A==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hravEeVZLMHGWhvA2y7803vuCPuTSWXR6Af1yHYx2BeCU+oIFTcCj2smTCli0mbzXmOKrGn5fAuxSm3RZDpy0UmWIQR631rn0+B9ouuRHz9UEYoF8cnALvdDI7x3AlH6e95t0fXyfvCxZKzl6DrQZNa7NmSbBuiBjbbygWo7YqMrDW1zkV5ZdXNXK6C0tCkAiXNiXUvVZsNlKgpKVkIpJJYVlwD8jaRinyf2h/AOthato4BhxkomeZKCwvnXZS2UIMhHmOfiv+AP3zbwsckq2S2x3EK37ioKQc37RDOQj+RwUwFIuIQvEKgXzSt6ym6sA5MDgtROqw5iF9tMQps+pA==
  • Authentication-results-original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com;
  • Cc: Jan Beulich <jbeulich@xxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Mon, 19 Jun 2023 08:31:53 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Nodisclaimer: true
  • Original-authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com;
  • Thread-index: AQHZn6gWgGF2ZXPPc02I099nu6PTEa+NBmIAgAAYV4CAAMv8AIAD4yaAgAABdQCAAAIygA==
  • Thread-topic: Refactoring of a possibly unsafe pattern for variable initialization via function calls


> On 19 Jun 2023, at 09:23, Julien Grall <julien@xxxxxxx> wrote:
> 
> 
> 
> On 19/06/2023 09:18, Jan Beulich wrote:
>> 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 ) > > ?
> 
> I would be OK with that. Maybe with an extra set of parentheses around ' np ? 
> ... : NULL' just to make visually easier to parse.

Agree, and for MISRA, we should use a boolean expression as condition, even if 
I know that we would like to deviate from that,
which I dislike.

Anyway I also think that it’s difficult to have a generic rule for cases like 
that, also because for some function
maybe the author intention was to don’t write the *lenp in case some error 
occur before, anyway this can
be easily made clear adding documentation to the function, for these cases.


> 
> Cheers,
> 
> -- 
> Julien Grall
> 


 


Rackspace

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