libluna: Remove make_array() and destroy_array()

Placement new on arrays is a bit unreliable and could cause out-of-bounds data accesses.
This commit is contained in:
apio 2023-08-02 14:47:58 +02:00
parent b1fb6dee8a
commit df77fc8de8
Signed by: apio
GPG Key ID: B8A7D06E42258954
5 changed files with 8 additions and 21 deletions

View File

@ -754,8 +754,8 @@ Result<u64> ATADevice::read(u8* buf, usize offset, usize size) const
ScopedKMutexLock<100> lock(m_drive->channel()->lock()); ScopedKMutexLock<100> lock(m_drive->channel()->lock());
// FIXME: Don't always allocate this if we don't need it. // FIXME: Don't always allocate this if we don't need it.
auto* temp = TRY(make_array<u8>(block_size)); auto* temp = (u8*)TRY(malloc_impl(block_size));
auto guard = make_scope_guard([temp] { delete[] temp; }); auto guard = make_scope_guard([temp] { free_impl(temp); });
if (offset % block_size) if (offset % block_size)
{ {

View File

@ -41,8 +41,8 @@ namespace GPT
u32 partition_index = 1; u32 partition_index = 1;
auto* table = TRY(make_array<PartitionEntry>(header.num_partitions)); auto* table = (PartitionEntry*)TRY(calloc_impl(header.num_partitions, sizeof(PartitionEntry)));
auto guard = make_scope_guard([table] { delete[] table; }); auto guard = make_scope_guard([table] { free_impl(table); });
nread = TRY(device->read((u8*)table, partition_table_start, sizeof(PartitionEntry) * header.num_partitions)); nread = TRY(device->read((u8*)table, partition_table_start, sizeof(PartitionEntry) * header.num_partitions));
check(nread == sizeof(PartitionEntry) * header.num_partitions); check(nread == sizeof(PartitionEntry) * header.num_partitions);

View File

@ -91,8 +91,8 @@ namespace Ext2
const usize inode_size = m_metadata.size; const usize inode_size = m_metadata.size;
const usize block_size = m_fs->m_block_size; const usize block_size = m_fs->m_block_size;
u8* const buf = TRY(make_array<u8>(block_size)); u8* const buf = (u8*)TRY(calloc_impl(block_size, 1));
auto guard = make_scope_guard([buf] { delete[] buf; }); auto guard = make_scope_guard([buf] { free_impl(buf); });
m_entries.clear(); m_entries.clear();

View File

@ -10,19 +10,7 @@ template <typename T, class... Args> [[nodiscard]] Result<T*> make(Args... args)
return result; return result;
} }
template <typename T> [[nodiscard]] Result<T*> make_array(usize count)
{
T* const result = (T*)TRY(calloc_impl(count, sizeof(T)));
new (result) T[count];
return result;
}
template <typename T> void destroy(T* item) template <typename T> void destroy(T* item)
{ {
delete item; delete item;
} }
template <typename T> void destroy_array(T* item)
{
delete[] item;
}

View File

@ -82,12 +82,11 @@ namespace os::FileSystem
TRY(stat(path, st, false)); TRY(stat(path, st, false));
if (!S_ISLNK(st.st_mode)) return String {}; if (!S_ISLNK(st.st_mode)) return String {};
char* buf = TRY(make_array<char>(st.st_size + 1)); char* buf = (char*)TRY(calloc_impl(st.st_size + 1, 1));
auto guard = make_scope_guard([buf] { delete[] buf; }); auto guard = make_scope_guard([buf] { free_impl(buf); });
usize nread = TRY( usize nread = TRY(
Result<usize>::from_syscall(syscall(SYS_readlinkat, path.dirfd(), path.name().chars(), buf, st.st_size))); Result<usize>::from_syscall(syscall(SYS_readlinkat, path.dirfd(), path.name().chars(), buf, st.st_size)));
buf[nread] = '\0';
guard.deactivate(); guard.deactivate();
return String { buf, nread }; return String { buf, nread };