Эх сурвалжийг харах

Merge branch 'bugfix/return_EINVAL_if_truncate_length_minus_zero' into 'master'

fatfs: return EINVAL if truncate length is less than 0

Closes IDFCI-390

See merge request espressif/esp-idf!11980
Angus Gratton 5 жил өмнө
parent
commit
a0eab085ad

+ 2 - 2
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

+ 7 - 1
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);