<div>The functions FatGetDirEntInfo and FatOpenDirEnt in the file</div>
<div>FatPkg/EnhancedFatDxe/DirectoryManage.c contains the code statements</div>
<div> </div>
<div>      Cluster            = (Entry->FileClusterHigh << 16) | Entry->FileCluster;</div>
<div>      and</div>
<div>      OFile->FileCluster = ((DirEnt->Entry.FileClusterHigh) << 16) | (DirEnt->Entry.FileCluster);</div>
<div> </div>
<div>respectively. In both these statements, there is an "Operand1" with</div>
<div>type "UINT16" (16 bits, unsigned) which is promoted in the operation</div>
<div>"(Operand1 << 16) | Operand2" to type "int" (32 bits, signed), then</div>
<div>sign-extended to type "unsigned long long" (64 bits, unsigned). If the</div>
<div>result of "(Operand1 << 16) | Operand2" is greater than 0x7FFFFFFF,</div>
<div>the upper bits of the result will all be 1. So to avoid sign-extension,</div>
<div>typecast "Operand1" with UINTN which is the data type of the variable</div>
<div>on the LHS of the assignment.</div>
<div> </div>
<div>The function FatInitializeDiskCache in the file</div>
<div>FatPkg/EnhancedFatDxe/DiskCache.c evaluates an expression</div>
<div> </div>
<div>FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment</div>
<div>and</div>
<div>assigns it to DataCacheSize which is of type UINTN;</div>
<div> </div>
<div>As per Coverity report,</div>
<div>FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment is a</div>
<div>potentially overflowing expression with type "int" (32 bits, signed)</div>
<div>evaluated using 32-bit arithmetic, and then used in a context that</div>
<div>expects an expression of type "UINTN" (64 bits, unsigned).</div>
<div>To avoid overflow, cast "FAT_DATACACHE_GROUP_COUNT" to type "UINTN".</div>
<div> </div>
<div>REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4249</div>
<div>Signed-off-by: Ranbir Singh <Ranbir.Singh3@Dell.com></div>
<div>---</div>
<div> FatPkg/EnhancedFatDxe/DirectoryManage.c | 4 ++--</div>
<div> FatPkg/EnhancedFatDxe/DiskCache.c       | 2 +-</div>
<div> 2 files changed, 3 insertions(+), 3 deletions(-)</div>
<div> </div>
<div>diff --git a/FatPkg/EnhancedFatDxe/DirectoryManage.c b/FatPkg/EnhancedFatDxe/DirectoryManage.c</div>
<div>index 723fc35f38..cdae864be0 100644</div>
<div>--- a/FatPkg/EnhancedFatDxe/DirectoryManage.c</div>
<div>+++ b/FatPkg/EnhancedFatDxe/DirectoryManage.c</div>
<div>@@ -474,7 +474,7 @@ FatGetDirEntInfo (</div>
<div>     Info       = Buffer;</div>
<div>     Info->Size = ResultSize;</div>
<div>     if ((Entry->Attributes & FAT_ATTRIBUTE_DIRECTORY) != 0) {</div>
<div>-      Cluster            = (Entry->FileClusterHigh << 16) | Entry->FileCluster;</div>
<div>+      Cluster            = ((UINTN)(Entry->FileClusterHigh) << 16) | (UINTN)Entry->FileCluster;</div>
<div>       Info->PhysicalSize = FatPhysicalDirSize (Volume, Cluster);</div>
<div>       Info->FileSize     = Info->PhysicalSize;</div>
<div>     } else {</div>
<div>@@ -1167,7 +1167,7 @@ FatOpenDirEnt (</div>
<div>       //</div>
<div>       Volume             = Parent->Volume;</div>
<div>       OFile->FullPathLen = Parent->FullPathLen + 1 + StrLen (DirEnt->FileString);</div>
<div>-      OFile->FileCluster = ((DirEnt->Entry.FileClusterHigh) << 16) | (DirEnt->Entry.FileCluster);</div>
<div>+      OFile->FileCluster = ((UINTN)(DirEnt->Entry.FileClusterHigh) << 16) | (DirEnt->Entry.FileCluster);</div>
<div>       InsertTailList (&Parent->ChildHead, &OFile->ChildLink);</div>
<div>     } else {</div>
<div>       //</div>
<div>diff --git a/FatPkg/EnhancedFatDxe/DiskCache.c b/FatPkg/EnhancedFatDxe/DiskCache.c</div>
<div>index d1a34a6a64..d56e338586 100644</div>
<div>--- a/FatPkg/EnhancedFatDxe/DiskCache.c</div>
<div>+++ b/FatPkg/EnhancedFatDxe/DiskCache.c</div>
<div>@@ -477,7 +477,7 @@ FatInitializeDiskCache (</div>
<div>   DiskCache[CacheFat].BaseAddress   = Volume->FatPos;</div>
<div>   DiskCache[CacheFat].LimitAddress  = Volume->FatPos + Volume->FatSize;</div>
<div>   FatCacheSize                      = FatCacheGroupCount << DiskCache[CacheFat].PageAlignment;</div>
<div>-  DataCacheSize                     = FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment;</div>
<div>+  DataCacheSize                     = (UINTN)FAT_DATACACHE_GROUP_COUNT << DiskCache[CacheData].PageAlignment;</div>
<div>   //</div>
<div>   // Allocate the Fat Cache buffer</div>
<div>   //</div>
<div>--</div>
<div>2.36.1.windows.1</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/97942">View/Reply Online (#97942)</a> |


  

|

  <a target="_blank" href="https://groups.io/mt/96050605/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>