Skip to content

Commit b3d1a05

Browse files
asyntsawesomekling
authored andcommitted
Refactor: Expose const_cast by removing ByteBuffer::warp(const void*, size_t)
This function did a const_cast internally which made the call side look "safe". This method is removed completely and call sites are replaced with ByteBuffer::wrap(const_cast<void*>(data), size) which makes the behaviour obvious.
1 parent ac9f6fd commit b3d1a05

File tree

15 files changed

+42
-36
lines changed

15 files changed

+42
-36
lines changed

AK/ByteBuffer.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class ByteBufferImpl : public RefCounted<ByteBufferImpl> {
4242
static NonnullRefPtr<ByteBufferImpl> create_zeroed(size_t);
4343
static NonnullRefPtr<ByteBufferImpl> copy(const void*, size_t);
4444
static NonnullRefPtr<ByteBufferImpl> wrap(void*, size_t);
45-
static NonnullRefPtr<ByteBufferImpl> wrap(const void*, size_t);
4645
static NonnullRefPtr<ByteBufferImpl> adopt(void*, size_t);
4746

4847
~ByteBufferImpl() { clear(); }
@@ -135,7 +134,9 @@ class ByteBuffer {
135134
static ByteBuffer create_uninitialized(size_t size) { return ByteBuffer(ByteBufferImpl::create_uninitialized(size)); }
136135
static ByteBuffer create_zeroed(size_t size) { return ByteBuffer(ByteBufferImpl::create_zeroed(size)); }
137136
static ByteBuffer copy(const void* data, size_t size) { return ByteBuffer(ByteBufferImpl::copy(data, size)); }
138-
static ByteBuffer wrap(const void* data, size_t size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); }
137+
// The const version of this method was removed because it was misleading, suggesting copy on write
138+
// functionality. If you really need the old behaviour, call ByteBuffer::wrap(const_cast<void*>(data), size)
139+
// manually. Writing to such a byte buffer invokes undefined behaviour.
139140
static ByteBuffer wrap(void* data, size_t size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); }
140141
static ByteBuffer adopt(void* data, size_t size) { return ByteBuffer(ByteBufferImpl::adopt(data, size)); }
141142

@@ -193,7 +194,7 @@ class ByteBuffer {
193194
// I cannot hand you a slice I don't have
194195
ASSERT(offset + size <= this->size());
195196

196-
return wrap(offset_pointer(offset), size);
197+
return wrap(const_cast<u8*>(offset_pointer(offset)), size);
197198
}
198199

199200
ByteBuffer slice(size_t offset, size_t size) const
@@ -302,11 +303,6 @@ inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::wrap(void* data, size_t siz
302303
return ::adopt(*new ByteBufferImpl(data, size, Wrap));
303304
}
304305

305-
inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::wrap(const void* data, size_t size)
306-
{
307-
return ::adopt(*new ByteBufferImpl(const_cast<void*>(data), size, Wrap));
308-
}
309-
310306
inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::adopt(void* data, size_t size)
311307
{
312308
return ::adopt(*new ByteBufferImpl(data, size, Adopt));

Kernel/FileSystem/ProcFS.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ Optional<KBuffer> procfs$pid_vm(InodeIdentifier identifier)
315315
region_object.add("cow_pages", region.cow_pages());
316316
region_object.add("name", region.name());
317317
region_object.add("vmobject", region.vmobject().class_name());
318-
318+
319319
StringBuilder pagemap_builder;
320320
for (size_t i = 0; i < region.page_count(); ++i) {
321321
auto* page = region.physical_page(i);
@@ -736,8 +736,7 @@ Optional<KBuffer> procfs$cpuinfo(InodeIdentifier)
736736
KBufferBuilder builder;
737737
JsonArraySerializer array { builder };
738738
Processor::for_each(
739-
[&](Processor& proc) -> IterationDecision
740-
{
739+
[&](Processor& proc) -> IterationDecision {
741740
auto& info = proc.info();
742741
auto obj = array.add_object();
743742
JsonArray features;
@@ -1399,7 +1398,7 @@ ssize_t ProcFSInode::write_bytes(off_t offset, ssize_t size, const u8* buffer, F
13991398
ASSERT(is_persistent_inode(identifier()));
14001399
// FIXME: Being able to write into ProcFS at a non-zero offset seems like something we should maybe support..
14011400
ASSERT(offset == 0);
1402-
bool success = (*write_callback)(identifier(), ByteBuffer::wrap(buffer, size));
1401+
bool success = (*write_callback)(identifier(), ByteBuffer::wrap(const_cast<u8*>(buffer), size));
14031402
ASSERT(success);
14041403
return 0;
14051404
}

Libraries/LibELF/Image.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Image {
6666
{
6767
}
6868

69-
~Symbol() {}
69+
~Symbol() { }
7070

7171
StringView name() const { return m_image.table_string(m_sym.st_name); }
7272
unsigned section_index() const { return m_sym.st_shndx; }
@@ -92,7 +92,7 @@ class Image {
9292
, m_program_header_index(program_header_index)
9393
{
9494
}
95-
~ProgramHeader() {}
95+
~ProgramHeader() { }
9696

9797
unsigned index() const { return m_program_header_index; }
9898
u32 type() const { return m_program_header.p_type; }
@@ -122,7 +122,7 @@ class Image {
122122
, m_section_index(sectionIndex)
123123
{
124124
}
125-
~Section() {}
125+
~Section() { }
126126

127127
StringView name() const { return m_image.section_header_table_string(m_section_header.sh_name); }
128128
unsigned type() const { return m_section_header.sh_type; }
@@ -132,7 +132,7 @@ class Image {
132132
unsigned entry_count() const { return !entry_size() ? 0 : size() / entry_size(); }
133133
u32 address() const { return m_section_header.sh_addr; }
134134
const char* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); }
135-
ByteBuffer wrapping_byte_buffer() { return ByteBuffer::wrap(reinterpret_cast<const u8*>(raw_data()), size()); }
135+
ByteBuffer wrapping_byte_buffer() { return ByteBuffer::wrap(const_cast<char*>(raw_data()), size()); }
136136
bool is_undefined() const { return m_section_index == SHN_UNDEF; }
137137
const RelocationSection relocations() const;
138138
u32 flags() const { return m_section_header.sh_flags; }
@@ -166,7 +166,7 @@ class Image {
166166
{
167167
}
168168

169-
~Relocation() {}
169+
~Relocation() { }
170170

171171
unsigned offset() const { return m_rel.r_offset; }
172172
unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }

Libraries/LibGemini/GeminiJob.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ void GeminiJob::read_while_data_available(Function<IterationDecision()> read)
9595

9696
void GeminiJob::set_certificate(String certificate, String private_key)
9797
{
98-
if (!m_socket->add_client_key(ByteBuffer::wrap(certificate.characters(), certificate.length()), ByteBuffer::wrap(private_key.characters(), private_key.length()))) {
98+
if (!m_socket->add_client_key(
99+
ByteBuffer::wrap(const_cast<char*>(certificate.characters()), certificate.length()),
100+
ByteBuffer::wrap(const_cast<char*>(private_key.characters()), private_key.length()))) {
99101
dbg() << "LibGemini: Failed to set a client certificate";
100102
// FIXME: Do something about this failure
101103
ASSERT_NOT_REACHED();

Libraries/LibGfx/BMPLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ static bool decode_bmp_pixel_data(BMPLoadingContext& context)
11041104
return false;
11051105
}
11061106

1107-
auto buffer = ByteBuffer::wrap(context.data + context.data_offset, context.data_size);
1107+
auto buffer = ByteBuffer::wrap(const_cast<u8*>(context.data + context.data_offset), context.data_size);
11081108

11091109
if (context.dib.info.compression == Compression::RLE4 || context.dib.info.compression == Compression::RLE8
11101110
|| context.dib.info.compression == Compression::RLE24) {

Libraries/LibGfx/GIFLoader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ bool load_gif_frame_descriptors(GIFLoadingContext& context)
345345
if (context.data_size < 32)
346346
return false;
347347

348-
auto buffer = ByteBuffer::wrap(context.data, context.data_size);
348+
auto buffer = ByteBuffer::wrap(const_cast<u8*>(context.data), context.data_size);
349349
BufferStream stream(buffer);
350350

351351
Optional<GIFFormat> format = decode_gif_header(stream);
@@ -546,7 +546,7 @@ GIFImageDecoderPlugin::GIFImageDecoderPlugin(const u8* data, size_t size)
546546
m_context->data_size = size;
547547
}
548548

549-
GIFImageDecoderPlugin::~GIFImageDecoderPlugin() {}
549+
GIFImageDecoderPlugin::~GIFImageDecoderPlugin() { }
550550

551551
IntSize GIFImageDecoderPlugin::size()
552552
{
@@ -591,7 +591,7 @@ bool GIFImageDecoderPlugin::set_nonvolatile()
591591

592592
bool GIFImageDecoderPlugin::sniff()
593593
{
594-
auto buffer = ByteBuffer::wrap(m_context->data, m_context->data_size);
594+
auto buffer = ByteBuffer::wrap(const_cast<u8*>(m_context->data), m_context->data_size);
595595
BufferStream stream(buffer);
596596
return decode_gif_header(stream).has_value();
597597
}

Libraries/LibGfx/ICOLoader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static size_t find_largest_image(const ICOLoadingContext& context)
170170
size_t max_area = 0;
171171
size_t index = 0;
172172
size_t largest_index = 0;
173-
for(const auto& desc : context.images) {
173+
for (const auto& desc : context.images) {
174174
if (desc.width * desc.height > max_area) {
175175
max_area = desc.width * desc.height;
176176
largest_index = index;
@@ -182,7 +182,7 @@ static size_t find_largest_image(const ICOLoadingContext& context)
182182

183183
static bool load_ico_directory(ICOLoadingContext& context)
184184
{
185-
auto buffer = ByteBuffer::wrap(context.data, context.data_size);
185+
auto buffer = ByteBuffer::wrap(const_cast<u8*>(context.data), context.data_size);
186186
auto stream = BufferStream(buffer);
187187
auto image_count = decode_ico_header(stream);
188188
if (!image_count.has_value() || image_count.value() == 0) {
@@ -280,7 +280,7 @@ static bool load_ico_bmp(ICOLoadingContext& context, ImageDescriptor& desc)
280280
}
281281

282282
// Mask is 1bpp, and each row must be 4-byte aligned
283-
size_t mask_row_len = align_up_to(align_up_to(desc.width, 8)/8, 4);
283+
size_t mask_row_len = align_up_to(align_up_to(desc.width, 8) / 8, 4);
284284
size_t required_len = desc.height * (desc.width * sizeof(BMP_ARGB) + mask_row_len);
285285
size_t available_len = desc.size - sizeof(info);
286286
if (required_len > available_len) {
@@ -408,7 +408,7 @@ bool ICOImageDecoderPlugin::set_nonvolatile()
408408

409409
bool ICOImageDecoderPlugin::sniff()
410410
{
411-
auto buffer = ByteBuffer::wrap(m_context->data, m_context->data_size);
411+
auto buffer = ByteBuffer::wrap(const_cast<u8*>(m_context->data), m_context->data_size);
412412
BufferStream stream(buffer);
413413
return decode_ico_header(stream).has_value();
414414
}

Libraries/LibGfx/JPGLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ static bool scan_huffman_stream(BufferStream& stream, JPGLoadingContext& context
11461146

11471147
static bool decode_jpg(JPGLoadingContext& context)
11481148
{
1149-
ByteBuffer buffer = ByteBuffer::wrap(context.data, context.data_size);
1149+
ByteBuffer buffer = ByteBuffer::wrap(const_cast<u8*>(context.data), context.data_size);
11501150
BufferStream stream(buffer);
11511151
if (!parse_header(stream, context))
11521152
return false;

Libraries/LibGfx/PNGLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class Streamer {
166166
{
167167
if (m_size_remaining < count)
168168
return false;
169-
buffer = ByteBuffer::wrap(m_data_ptr, count);
169+
buffer = ByteBuffer::wrap(const_cast<u8*>(m_data_ptr), count);
170170
m_data_ptr += count;
171171
m_size_remaining -= count;
172172
return true;

Libraries/LibHTTP/HttpsJob.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ void HttpsJob::shutdown()
8888

8989
void HttpsJob::set_certificate(String certificate, String private_key)
9090
{
91-
if (!m_socket->add_client_key(ByteBuffer::wrap(certificate.characters(), certificate.length()), ByteBuffer::wrap(private_key.characters(), private_key.length()))) {
91+
if (!m_socket->add_client_key(
92+
ByteBuffer::wrap(const_cast<char*>(certificate.characters()), certificate.length()),
93+
ByteBuffer::wrap(const_cast<char*>(private_key.characters()), private_key.length()))) {
94+
9295
dbg() << "LibHTTP: Failed to set a client certificate";
9396
// FIXME: Do something about this failure
9497
ASSERT_NOT_REACHED();

0 commit comments

Comments
 (0)