We need to free buffer on return if BlockRemainder != 0. Also changed return logic from function to use use common exit to prevent code duplication.
Cc: Marvin Häuser <mhaeu...@posteo.de> Cc: Pedro Falcato <pedro.falc...@gmail.com> Cc: Vitaly Cheptsov <vit9...@protonmail.com> Fixes: e55f0527dde48a5f139c1b8f35acc4e6b59dd794 Signed-off-by: Savva Mitrofanov <savva...@gmail.com> --- Features/Ext4Pkg/Ext4Dxe/Directory.c | 30 +++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Features/Ext4Pkg/Ext4Dxe/Directory.c b/Features/Ext4Pkg/Ext4Dxe/Directory.c index 4441e6d192b6..8b8fce568e43 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Directory.c +++ b/Features/Ext4Pkg/Ext4Dxe/Directory.c @@ -112,8 +112,7 @@ Ext4RetrieveDirent ( UINTN ToCopy; UINTN BlockOffset; - Status = EFI_NOT_FOUND; - Buf = AllocatePool (Partition->BlockSize); + Buf = AllocatePool (Partition->BlockSize); if (Buf == NULL) { return EFI_OUT_OF_RESOURCES; @@ -127,7 +126,8 @@ Ext4RetrieveDirent ( DivU64x32Remainder (DirInoSize, Partition->BlockSize, &BlockRemainder); if (BlockRemainder != 0) { // Directory inodes need to have block aligned sizes - return EFI_VOLUME_CORRUPTED; + Status = EFI_VOLUME_CORRUPTED; + goto Out; } while (Off < DirInoSize) { @@ -136,8 +136,7 @@ Ext4RetrieveDirent ( Status = Ext4Read (Partition, Directory, Buf, Off, &Length); if (Status != EFI_SUCCESS) { - FreePool (Buf); - return Status; + goto Out; } for (BlockOffset = 0; BlockOffset < Partition->BlockSize; ) { @@ -145,19 +144,19 @@ Ext4RetrieveDirent ( RemainingBlock = Partition->BlockSize - BlockOffset; // Check if the minimum directory entry fits inside [BlockOffset, EndOfBlock] if (RemainingBlock < EXT4_MIN_DIR_ENTRY_LEN) { - FreePool (Buf); - return EFI_VOLUME_CORRUPTED; + Status = EFI_VOLUME_CORRUPTED; + goto Out; } if (!Ext4ValidDirent (Entry)) { - FreePool (Buf); - return EFI_VOLUME_CORRUPTED; + Status = EFI_VOLUME_CORRUPTED; + goto Out; } if ((Entry->name_len > RemainingBlock) || (Entry->rec_len > RemainingBlock)) { // Corrupted filesystem - FreePool (Buf); - return EFI_VOLUME_CORRUPTED; + Status = EFI_VOLUME_CORRUPTED; + goto Out; } // Unused entry @@ -186,8 +185,8 @@ Ext4RetrieveDirent ( ToCopy = MIN (Entry->rec_len, sizeof (EXT4_DIR_ENTRY)); CopyMem (Result, Entry, ToCopy); - FreePool (Buf); - return EFI_SUCCESS; + Status = EFI_SUCCESS; + goto Out; } BlockOffset += Entry->rec_len; @@ -196,8 +195,11 @@ Ext4RetrieveDirent ( Off += Partition->BlockSize; } + Status = EFI_NOT_FOUND; + +Out: FreePool (Buf); - return EFI_NOT_FOUND; + return Status; } /** -- 2.38.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#97261): https://edk2.groups.io/g/devel/message/97261 Mute This Topic: https://groups.io/mt/95622329/21656 Group Owner: devel+ow...@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-