Skip to contentSkip to conversation

libexfat: fix return value of exfat_check_written_data()

PATCH Applied David Timber 2 participants 2 messages Aug 8

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libexfat: fix return value of exfat_check_written_data()

In exfat_check_written_data(), the return value of posix_memalign() is only used to check if the memory allocation failed and the errno is returned from the function. However, according to posix_memalign(3), errno shouldn't be set by posix_memalign() at all.

Overflows and invalid allocation lengths are already checked with assert() beforehand. Therefore, EINVAL should never be returned from posix_memalign(). So, always return -ENOMEM if allocation fails.

Note that currently, this patch doesn't really fix any issue because with all the current Linux libc implementations(glibc and musl), errno is set for ENOMEM cases in posix_memalign() anyway. This patch is only to fix potential undefined behaviour on other POSIX platforms in the future.

Fixes 78e398e37ba5 ("exfatprogs: Fix and improve verify written zeros (memory optimisations)")
---
 lib/libexfat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/libexfat.c +1 -1
index 8e23aca..6eda5dd 100644
@@ -1209,7 +1209,7 @@ int exfat_check_written_data(struct exfat_blk_dev *bd,
 	assert(aligned_len > 0 && aligned_len >= (off_t)len);
 
 	if (posix_memalign(&verify, sector, (size_t)aligned_len))
-		return -errno;
+		return -ENOMEM;
 	memset(verify, 0, (size_t)aligned_len);
 
 	if (buf == NULL) {
Signature · 1 line
2.55.0
maintainer Applied
#1 lore raw
Quoted 10 lines · David Timber

In exfat_check_written_data(), the return value of posix_memalign() is only used to check if the memory allocation failed and the errno is returned from the function. However, according to posix_memalign(3), errno shouldn't be set by posix_memalign() at all.

Overflows and invalid allocation lengths are already checked with assert() beforehand. Therefore, EINVAL should never be returned from posix_memalign(). So, always return -ENOMEM if allocation fails.

Quoted 8 lines

Note that currently, this patch doesn't really fix any issue because with all the current Linux libc implementations(glibc and musl), errno is set for ENOMEM cases in posix_memalign() anyway. This patch is only to fix potential undefined behaviour on other POSIX platforms in the future.

Fixes 78e398e37ba5 ("exfatprogs: Fix and improve verify written zeros (memory optimisations)")

Applied it to #exfat-next.
Thanks!