libexfat: fix return value of exfat_check_written_data()
Subsystems (2)
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.
--- lib/libexfat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/libexfat.c +1 -1
@@ -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
Quoted 10 lines · David Timber
In
exfat_check_written_data(), the return value ofposix_memalign()is only used to check if the memory allocation failed and the errno is returned from the function. However, according toposix_memalign(3), errno shouldn't be set byposix_memalign()at all.Overflows and invalid allocation lengths are already checked with
assert()beforehand. Therefore, EINVAL should never be returned fromposix_memalign(). So, always return-ENOMEMif 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)")Signed-off-by David Timber <[email protected]>
Applied it to #exfat-next.
Thanks!