[Base/Posix] mapped_memory portability and correctness fixes
Replace Linux-specific fstat64/ftruncate64 with portable POSIX equivalents. Fix mmap error check to use MAP_FAILED instead of null pointer comparison. Remove unconditional ftruncate after mmap that could corrupt files. Add zero-length guard.
This commit is contained in:
@@ -46,20 +46,26 @@ class PosixMappedMemory : public MappedMemory {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct stat file_stat;
|
||||||
|
if (fstat(file_descriptor, &file_stat)) {
|
||||||
|
close(file_descriptor);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
uint64_t file_size = uint64_t(file_stat.st_size);
|
||||||
|
|
||||||
size_t map_length = length;
|
size_t map_length = length;
|
||||||
if (!length) {
|
if (!length) {
|
||||||
struct stat64 file_stat;
|
map_length = size_t(file_size);
|
||||||
if (fstat64(file_descriptor, &file_stat)) {
|
}
|
||||||
close(file_descriptor);
|
|
||||||
return nullptr;
|
if (!map_length) {
|
||||||
}
|
close(file_descriptor);
|
||||||
map_length = size_t(file_stat.st_size);
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* data =
|
void* data =
|
||||||
mmap(0, map_length, protection, MAP_SHARED, file_descriptor, offset);
|
mmap(0, map_length, protection, MAP_SHARED, file_descriptor, offset);
|
||||||
ftruncate(file_descriptor, map_length);
|
if (data == MAP_FAILED) {
|
||||||
if (!data) {
|
|
||||||
close(file_descriptor);
|
close(file_descriptor);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -75,7 +81,7 @@ class PosixMappedMemory : public MappedMemory {
|
|||||||
}
|
}
|
||||||
if (file_descriptor_ >= 0) {
|
if (file_descriptor_ >= 0) {
|
||||||
if (truncate_size) {
|
if (truncate_size) {
|
||||||
ftruncate64(file_descriptor_, off64_t(truncate_size));
|
ftruncate(file_descriptor_, truncate_size);
|
||||||
}
|
}
|
||||||
close(file_descriptor_);
|
close(file_descriptor_);
|
||||||
file_descriptor_ = -1;
|
file_descriptor_ = -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user