[PATCH] libexfat: fix return value of exfat_check_written_data()

David Timber <[email protected]>
Newsgroups dev.linux.lists.exfat
Message-ID <[email protected]>
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)")
Signed-off-by: David Timber <[email protected]>
---
 lib/libexfat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/libexfat.c b/lib/libexfat.c
index 8e23aca..6eda5dd 100644
--- a/lib/libexfat.c
+++ b/lib/libexfat.c
@@ -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) {
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.