SGX IPFS: Fix a segfault and support seeking beyond the end of files while using SEEK_CUR/SEEK_END (#1916)

The current implementation throws a segmentation fault when padding
files using a large range, because the writing operation overflows the
source buffer, which was a single char.

IPFS previously assumed that the offset for the seek operation was related
to the start of the file (SEEK_SET). It now correctly checks the parameter
'whence' and computes the offset for SEEK_CUR (middle of the file) and
SEEK_END (end of the file).
This commit is contained in:
Jämes Ménétrey
2023-01-30 01:24:12 +01:00
committed by GitHub
parent 45c003e6e4
commit 0435acdd43
2 changed files with 89 additions and 18 deletions

View File

@ -18,7 +18,7 @@
#define WORLD_OFFSET 7
#define NAME_REPLACMENT "James"
#define NAME_REPLACMENT_LEN (sizeof(NAME_REPLACMENT) - 1)
#define ADDITIONAL_SPACE 10
#define ADDITIONAL_SPACE 1 * 1024 * 1024
int
main(int argc, char **argv)
@ -100,7 +100,7 @@ main(int argc, char **argv)
printf("[Test] Reading at specified offset passed.\n");
// Test: allocate more space to the file (posix_fallocate)
printf("Allocate more space to the file..\n");
printf("Allocate more space to the file (posix_fallocate)..\n");
posix_fallocate(fileno(file), ftell(file), ADDITIONAL_SPACE);
printf("File current offset: %ld\n", ftell(file));
printf("Moving to the end..\n");
@ -110,8 +110,8 @@ main(int argc, char **argv)
printf("[Test] Allocation or more space passed.\n");
// Test: allocate more space to the file (ftruncate)
printf("Extend the file size of 10 bytes using ftruncate..\n");
ftruncate(fileno(file), ftell(file) + 10);
printf("Allocate more space to the file (ftruncate)..\n");
ftruncate(fileno(file), ftell(file) + ADDITIONAL_SPACE);
assert(ftell(file) == strlen(text) + ADDITIONAL_SPACE);
printf("File current offset: %ld\n", ftell(file));
printf("Moving to the end..\n");
@ -120,6 +120,31 @@ main(int argc, char **argv)
assert(ftell(file) == strlen(text) + 2 * ADDITIONAL_SPACE);
printf("[Test] Extension of the file size passed.\n");
// Test: allocate more space to the file (fseek)
printf("Allocate more space to the file (fseek) from the start..\n");
printf("File current offset: %ld\n", ftell(file));
fseek(file, 3 * ADDITIONAL_SPACE, SEEK_SET);
printf("File current offset: %ld\n", ftell(file));
assert(ftell(file) == 3 * ADDITIONAL_SPACE);
printf("[Test] Extension of the file size passed.\n");
// Test: allocate more space to the file (fseek)
printf("Allocate more space to the file (fseek) from the end..\n");
printf("File current offset: %ld\n", ftell(file));
fseek(file, ADDITIONAL_SPACE, SEEK_END);
printf("File current offset: %ld\n", ftell(file));
assert(ftell(file) == 4 * ADDITIONAL_SPACE);
printf("[Test] Extension of the file size passed.\n");
// Test: allocate more space to the file (fseek)
printf("Allocate more space to the file (fseek) from the middle..\n");
fseek(file, 3 * ADDITIONAL_SPACE, SEEK_SET);
printf("File current offset: %ld\n", ftell(file));
fseek(file, 2 * ADDITIONAL_SPACE, SEEK_CUR);
printf("File current offset: %ld\n", ftell(file));
assert(ftell(file) == 5 * ADDITIONAL_SPACE);
printf("[Test] Extension of the file size passed.\n");
// Display some debug information
printf("Getting the size of the file on disk..\n");
struct stat st;