diff --git a/components/fatfs/test/test_fatfs_common.c b/components/fatfs/test/test_fatfs_common.c index 9804b13a79..953b8d4ad5 100644 --- a/components/fatfs/test/test_fatfs_common.c +++ b/components/fatfs/test/test_fatfs_common.c @@ -261,7 +261,7 @@ void test_fatfs_truncate_file(const char* filename) TEST_ASSERT_EQUAL(errno, EPERM); TEST_ASSERT_EQUAL(-1, truncate(filename, -1)); - TEST_ASSERT_EQUAL(errno, EPERM); + TEST_ASSERT_EQUAL(errno, EINVAL); // Truncating should succeed @@ -294,7 +294,7 @@ void test_fatfs_truncate_file(const char* filename) TEST_ASSERT_EQUAL(EPERM, errno); TEST_ASSERT_EQUAL(-1, truncate(filename, -1)); - TEST_ASSERT_EQUAL(EPERM, errno); + TEST_ASSERT_EQUAL(EINVAL, errno); // Truncating a truncated file should succeed diff --git a/components/fatfs/vfs/vfs_fat.c b/components/fatfs/vfs/vfs_fat.c index f24173037f..26b4f0662a 100644 --- a/components/fatfs/vfs/vfs_fat.c +++ b/components/fatfs/vfs/vfs_fat.c @@ -882,12 +882,18 @@ static int vfs_fat_access(void* ctx, const char *path, int amode) static int vfs_fat_truncate(void* ctx, const char *path, off_t length) { FRESULT res; - FIL* file; + FIL* file = NULL; int ret = 0; vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ctx; + if (length < 0) { + errno = EINVAL; + ret = -1; + goto out; + } + _lock_acquire(&fat_ctx->lock); prepend_drive_to_path(fat_ctx, &path, NULL);