<div dir="ltr"><div dir="ltr">Thanks Ard for adding the explanation.<br><div><br></div><div>Ok, so retaining outer cast along with intermediate casting</div><div>
        
        

<p style="line-height:100%;margin-bottom:0cm;background:transparent">
<span style="font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal"><font color="#500050"><font face="Arial, Helvetica, sans-serif"><font style="font-size:12pt">BlockSize
= </font></font></font></span><span style="color:rgb(80,0,80);font-size:16px">(UINT32)</span><span style="font-size:12pt;color:rgb(80,0,80);background-color:transparent">(((UINT32)(IdentifyData->AtaData.logic_sector_size_hi <<
16) | IdentifyData->AtaData.logic_sector_size_lo) * sizeof
(UINT16));</span></p><p style="line-height:100%;margin-bottom:0cm;background:transparent">should be acceptable. I considered outer cast is being implicitly done as per the data type of <b>BlockSize </b>and also there was no Coverity warning after the removal<b>.</b></p><p style="line-height:100%;margin-bottom:0cm;background:transparent"><span style="font-size:12pt;color:rgb(80,0,80);background-color:transparent"><br></span></p><p style="line-height:100%;margin-bottom:0cm;background:transparent"><span style="font-size:12pt;color:rgb(80,0,80);background-color:transparent"><br></span></p></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jun 5, 2023 at 2:14 PM Ard Biesheuvel <<a href="mailto:ardb@kernel.org">ardb@kernel.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Mon, 5 Jun 2023 at 09:54, Ranbir Singh <<a href="mailto:rsingh@ventanamicro.com" target="_blank">rsingh@ventanamicro.com</a>> wrote:<br>
><br>
> Please read the Coverity report attached in BZ 4204 for more details on sign-extension issue.<br>
><br>
<br>
I did read it - explanation below.<br>
<br>
> The data types of logic_sector_size_hi and logic_sector_size_lo are UINT16 and Isn't the return type of sizeof already unsigned ?<br>
><br>
<br>
Yes, it is unsigned long, so 64 bits wide on LP64 architectures<br>
<br>
> Now I have no means to run Coverity and test further changes.<br>
> Anyway, my understanding back then was that the sign-extension was primarily happening because of the 16 bits left shift operation. I did test the patch for coverity warning resolution back in Jan'23 which went off.<br>
><br>
<br>
The warning actually describes exactly what is happening:<br>
- The type of a UINT16 shifted left by 16 is promoted to signed integer.<br>
- The multiplication by sizeof() converts the former result to<br>
unsigned integer before widening to unsigned long integer.<br>
<br>
This means that the widening does not perform any sign extension,<br>
which is what Coverity warns about.<br>
<br>
None of this actually matters, given that those upper bits get<br>
truncated again anyway when we assign to BlockSize, which is only 32<br>
bits wide.<br>
<br>
Removing the outer (UINT32) cast is not the correct solution, as it<br>
may result in spurious truncation warnings on some toolchains.<br>
<br>
If you want to silence this warning (I wouldn't call it a fix), you<br>
need to either prevent the widening, or cast the intermediate result<br>
to (UINT32), and the latter is what your patch does. So it silences<br>
the warning, but now, the type of the right hand side of the<br>
assignment is UINTN not UINT32.<br>
<br>
So either leave the outer (UINT32) cast in place, or move it to before<br>
the sizeof() as I suggested before: in both cases, there is no longer<br>
any promotion to unsigned long, which should make the warning go away.<br>
<br>
<br>
<br>
><br>
> On Mon, Jun 5, 2023 at 4:28 AM Ard Biesheuvel <<a href="mailto:ardb@kernel.org" target="_blank">ardb@kernel.org</a>> wrote:<br>
>><br>
>> On Fri, 2 Jun 2023 at 21:42, Ranbir Singh <<a href="mailto:rsingh@ventanamicro.com" target="_blank">rsingh@ventanamicro.com</a>> wrote:<br>
>> ><br>
>> > From: Ranbir Singh <Ranbir.Singh3@Dell.com><br>
>> ><br>
>> > Line number 1348 does contain a typecast with UINT32, but it is after<br>
>> > all the operations (16-bit left shift followed by OR'ing) are over.<br>
>> > To avoid any SIGN_EXTENSION, typecast the intermediate result after<br>
>> > 16-bit left shift operation immediately with UINT32.<br>
>> ><br>
>><br>
>> What is wrong with sign extension?<br>
>><br>
>> > Cc: Hao A Wu <<a href="mailto:hao.a.wu@intel.com" target="_blank">hao.a.wu@intel.com</a>><br>
>> > Cc: Ray Ni <<a href="mailto:ray.ni@intel.com" target="_blank">ray.ni@intel.com</a>><br>
>> > REF: <a href="https://bugzilla.tianocore.org/show_bug.cgi?id=4204" rel="noreferrer" target="_blank">https://bugzilla.tianocore.org/show_bug.cgi?id=4204</a><br>
>> > Signed-off-by: Ranbir Singh <Ranbir.Singh3@Dell.com><br>
>> > ---<br>
>> >  MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c | 2 +-<br>
>> >  1 file changed, 1 insertion(+), 1 deletion(-)<br>
>> ><br>
>> > diff --git a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c<br>
>> > index 50406fe0270d..70c4ca27dc68 100644<br>
>> > --- a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c<br>
>> > +++ b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c<br>
>> > @@ -1345,7 +1345,7 @@ AtaPassThruPassThru (<br>
>> >      // Check logical block size<br>
>> >      //<br>
>> >      if ((IdentifyData->AtaData.phy_logic_sector_support & BIT12) != 0) {<br>
>> > -      BlockSize = (UINT32)(((IdentifyData->AtaData.logic_sector_size_hi << 16) | IdentifyData->AtaData.logic_sector_size_lo) * sizeof (UINT16));<br>
>> > +      BlockSize = (((UINT32)(IdentifyData->AtaData.logic_sector_size_hi << 16) | IdentifyData->AtaData.logic_sector_size_lo) * sizeof (UINT16));<br>
>><br>
>><br>
>> The outer parens are now redundant, which means you're assigning<br>
>> something to BlockSize whose type is based on the type of <something><br>
>> * sizeof(UINT16), which is unsigned long not unsigned int, so this<br>
>> will produce a truncation warning on some compilers.<br>
>><br>
>> If you want to suppress the coverity warning without introducing new<br>
>> ones, better to cast the sizeof() to (UINT32).<br>
</blockquote></div></div>


<div width="1" style="color:white;clear:both">_._,_._,_</div>
<hr>


Groups.io Links:<p>


  
    You receive all messages sent to this group.
  
  


<p>
<a target="_blank" href="https://edk2.groups.io/g/devel/message/105721">View/Reply Online (#105721)</a> |


  

|

  <a target="_blank" href="https://groups.io/mt/99293622/1813853">Mute This Topic</a>


| <a href="https://edk2.groups.io/g/devel/post">New Topic</a>

<br>




<a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> |
<a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |

<a href="https://edk2.groups.io/g/devel/unsub">Unsubscribe</a>

 [edk2-devel-archive@redhat.com]<br>
<div width="1" style="color:white;clear:both">_._,_._,_</div>