1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:07:45 +00:00

AK: Add trivial structure validation to SharedBuffer

If we're sharing buffers, we only want to share trivial structures
as anything else could potentially share internal pointers, which
most likely is going to cause problems due to different address
spaces.

Fix the GUI::SystemTheme structure, which was not trivial, which
is now caught at compile time.

Fixes #3650
This commit is contained in:
Tom 2020-09-30 20:00:59 -06:00 committed by Andreas Kling
parent 87f20f704c
commit 7399874479
16 changed files with 48 additions and 29 deletions

View file

@ -169,7 +169,7 @@ RefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRef
Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size, const Vector<RGBA32>& palette)
: m_size(size)
, m_data(shared_buffer->data())
, m_data(shared_buffer->data<void>())
, m_pitch(minimum_pitch(size.width(), format))
, m_format(format)
, m_shared_buffer(move(shared_buffer))
@ -255,7 +255,7 @@ RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
auto bitmap = Bitmap::create_with_shared_buffer(m_format, *buffer, m_size, palette_to_vector());
if (!bitmap)
return nullptr;
memcpy(buffer->data(), scanline(0), size_in_bytes());
memcpy(buffer->data<void>(), scanline(0), size_in_bytes());
return bitmap;
}

View file

@ -52,13 +52,13 @@ Palette::~Palette()
const SystemTheme& PaletteImpl::theme() const
{
return *(const SystemTheme*)m_theme_buffer->data();
return *m_theme_buffer->data<SystemTheme>();
}
Color PaletteImpl::color(ColorRole role) const
{
ASSERT((int)role < (int)ColorRole::__Count);
return theme().color[(int)role];
return Color::from_rgba(theme().color[(int)role]);
}
int PaletteImpl::metric(MetricRole role) const
@ -76,7 +76,7 @@ String PaletteImpl::path(PathRole role) const
NonnullRefPtr<PaletteImpl> PaletteImpl::clone() const
{
auto new_theme_buffer = SharedBuffer::create_with_size(m_theme_buffer->size());
memcpy(new_theme_buffer->data(), m_theme_buffer->data(), m_theme_buffer->size());
memcpy(new_theme_buffer->data<SystemTheme>(), &theme(), m_theme_buffer->size());
return adopt(*new PaletteImpl(*new_theme_buffer));
}
@ -85,7 +85,7 @@ void Palette::set_color(ColorRole role, Color color)
if (m_impl->ref_count() != 1)
m_impl = m_impl->clone();
auto& theme = const_cast<SystemTheme&>(impl().theme());
theme.color[(int)role] = color;
theme.color[(int)role] = color.value();
}
void Palette::set_metric(MetricRole role, int value)
@ -101,7 +101,8 @@ void Palette::set_path(PathRole role, String path)
if (m_impl->ref_count() != 1)
m_impl = m_impl->clone();
auto& theme = const_cast<SystemTheme&>(impl().theme());
theme.path[(int)role] = path;
memcpy(theme.path[(int)role], path.characters(), min(path.length() + 1, sizeof(theme.path[(int)role])));
theme.path[(int)role][sizeof(theme.path[(int)role]) - 1] = '\0';
}
PaletteImpl::~PaletteImpl()

View file

@ -27,6 +27,7 @@
#include <AK/SharedBuffer.h>
#include <LibCore/ConfigFile.h>
#include <LibGfx/SystemTheme.h>
#include <string.h>
namespace Gfx {
@ -49,7 +50,7 @@ int current_system_theme_buffer_id()
void set_system_theme(SharedBuffer& buffer)
{
theme_buffer = buffer;
theme_page = (SystemTheme*)theme_buffer->data();
theme_page = theme_buffer->data<SystemTheme>();
}
RefPtr<SharedBuffer> load_system_theme(const String& path)
@ -57,7 +58,7 @@ RefPtr<SharedBuffer> load_system_theme(const String& path)
auto file = Core::ConfigFile::open(path);
auto buffer = SharedBuffer::create_with_size(sizeof(SystemTheme));
auto* data = (SystemTheme*)buffer->data();
auto* data = buffer->data<SystemTheme>();
auto get_color = [&](auto& name) {
auto color_string = file->read_entry("Colors", name);
@ -100,7 +101,7 @@ RefPtr<SharedBuffer> load_system_theme(const String& path)
#undef __ENUMERATE_COLOR_ROLE
#define __ENUMERATE_COLOR_ROLE(role) \
data->color[(int)ColorRole::role] = get_color(#role);
data->color[(int)ColorRole::role] = get_color(#role).value();
ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
#undef __ENUMERATE_COLOR_ROLE
@ -111,8 +112,12 @@ RefPtr<SharedBuffer> load_system_theme(const String& path)
DO_METRIC(TitleButtonWidth);
DO_METRIC(TitleButtonHeight);
#define DO_PATH(x) \
data->path[(int)PathRole::x] = get_path(#x, (int)PathRole::x)
#define DO_PATH(x) \
do { \
auto path = get_path(#x, (int)PathRole::x); \
memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
} while (0)
DO_PATH(TitleButtonIcons);

View file

@ -143,9 +143,9 @@ enum class PathRole {
};
struct SystemTheme {
Color color[(int)ColorRole::__Count];
RGBA32 color[(int)ColorRole::__Count];
int metric[(int)MetricRole::__Count];
String path[(int)PathRole::__Count];
char path[(int)PathRole::__Count][256]; // TODO: PATH_MAX?
};
const SystemTheme& current_system_theme();