Skip to content

Commit

Permalink
StringView: Rename characters() to characters_without_null_terminatio…
Browse files Browse the repository at this point in the history
…n().

This should make you think twice before trying to use the const char* from
a StringView as if it's a null-terminated string.
  • Loading branch information
awesomekling committed Jul 8, 2019
1 parent 567551b commit 0e75aba
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 46 deletions.
2 changes: 1 addition & 1 deletion AK/AKString.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class String {
if (view.m_impl)
m_impl = *view.m_impl;
else
m_impl = StringImpl::create(view.characters(), view.length());
m_impl = StringImpl::create(view.characters_without_null_termination(), view.length());
}

String(const String& other)
Expand Down
2 changes: 1 addition & 1 deletion AK/LogStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const LogStream& operator<<(const LogStream& stream, const String& value)

const LogStream& operator<<(const LogStream& stream, const StringView& value)
{
stream.write(value.characters(), value.length());
stream.write(value.characters_without_null_termination(), value.length());
return stream;
}

Expand Down
6 changes: 3 additions & 3 deletions AK/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ bool String::starts_with(const StringView& str) const
return false;
if (str.length() > length())
return false;
return !memcmp(characters(), str.characters(), str.length());
return !memcmp(characters(), str.characters_without_null_termination(), str.length());
}

bool String::ends_with(const StringView& str) const
Expand All @@ -209,7 +209,7 @@ bool String::ends_with(const StringView& str) const
return false;
if (str.length() > length())
return false;
return !memcmp(characters() + (length() - str.length()), str.characters(), str.length());
return !memcmp(characters() + (length() - str.length()), str.characters_without_null_termination(), str.length());
}

String String::repeated(char ch, int count)
Expand Down Expand Up @@ -239,7 +239,7 @@ bool String::match_helper(const StringView& mask) const
return false;

const char* string_ptr = characters();
const char* mask_ptr = mask.characters();
const char* mask_ptr = mask.characters_without_null_termination();
const char* mask_end = mask_ptr + mask.length();

// Match string against mask directly unless we hit a *
Expand Down
2 changes: 1 addition & 1 deletion AK/StringBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void StringBuilder::append(const StringView& str)
if (str.is_empty())
return;
will_append(str.length());
memcpy(m_buffer.pointer() + m_length, str.characters(), str.length());
memcpy(m_buffer.pointer() + m_length, str.characters_without_null_termination(), str.length());
m_length += str.length();
}

Expand Down
12 changes: 6 additions & 6 deletions AK/StringView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Vector<StringView> StringView::split_view(const char separator) const
Vector<StringView> v;
ssize_t substart = 0;
for (ssize_t i = 0; i < length(); ++i) {
char ch = characters()[i];
char ch = characters_without_null_termination()[i];
if (ch == separator) {
ssize_t sublen = i - substart;
if (sublen != 0)
Expand All @@ -35,7 +35,7 @@ Vector<StringView> StringView::split_view(const char separator) const
ssize_t taillen = length() - substart;
if (taillen != 0)
v.append(substring_view(substart, taillen));
if (characters()[length() - 1] == separator)
if (characters_without_null_termination()[length() - 1] == separator)
v.append(String::empty());
return v;
}
Expand All @@ -50,7 +50,7 @@ StringView StringView::substring_view(int start, int length) const

StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
{
const char* remaining_characters = substring.characters();
const char* remaining_characters = substring.characters_without_null_termination();
ASSERT(remaining_characters >= m_characters);
ASSERT(remaining_characters <= m_characters + m_length);
int remaining_length = m_length - (remaining_characters - m_characters);
Expand All @@ -59,7 +59,7 @@ StringView StringView::substring_view_starting_from_substring(const StringView&

StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
{
const char* remaining_characters = substring.characters() + substring.length();
const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
ASSERT(remaining_characters >= m_characters);
ASSERT(remaining_characters <= m_characters + m_length);
int remaining_length = m_length - (remaining_characters - m_characters);
Expand All @@ -70,12 +70,12 @@ unsigned StringView::to_uint(bool& ok) const
{
unsigned value = 0;
for (ssize_t i = 0; i < length(); ++i) {
if (characters()[i] < '0' || characters()[i] > '9') {
if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
ok = false;
return 0;
}
value = value * 10;
value += characters()[i] - '0';
value += characters_without_null_termination()[i] - '0';
}
ok = true;
return value;
Expand Down
2 changes: 1 addition & 1 deletion AK/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class StringView {

bool is_null() const { return !m_characters; }
bool is_empty() const { return m_length == 0; }
const char* characters() const { return m_characters; }
const char* characters_without_null_termination() const { return m_characters; }
int length() const { return m_length; }
char operator[](int index) const { return m_characters[index]; }

Expand Down
2 changes: 1 addition & 1 deletion Applications/PaintBrush/ToolboxWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ToolboxWidget::ToolboxWidget(GWidget* parent)
button->set_checkable(true);
button->set_exclusive(true);

button->set_icon(load_png(String::format("/res/icons/paintbrush/%s.png", icon_name.characters())));
button->set_icon(load_png(String::format("/res/icons/paintbrush/%s.png", String(icon_name).characters())));

button->on_checked = [button](auto checked) {
if (checked)
Expand Down
10 changes: 5 additions & 5 deletions Kernel/FileSystem/Ext2FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ KResult Ext2FSInode::add_child(InodeIdentifier child_id, const StringView& name,
ASSERT(is_directory());

//#ifdef EXT2_DEBUG
dbgprintf("Ext2FS: Adding inode %u with name '%s' and mode %o to directory %u\n", child_id.index(), name.characters(), mode, index());
dbg() << "Ext2FSInode::add_child(): Adding inode " << child_id.index() << " with name '" << name << " and mode " << mode << " to directory " << index();
//#endif

Vector<FS::DirectoryEntry> entries;
Expand All @@ -775,15 +775,15 @@ KResult Ext2FSInode::add_child(InodeIdentifier child_id, const StringView& name,
return true;
});
if (name_already_exists) {
kprintf("Ext2FS: Name '%s' already exists in directory inode %u\n", name.characters(), index());
dbg() << "Ext2FSInode::add_child(): Name '" << name << "' already exists in inode " << index();
return KResult(-EEXIST);
}

auto child_inode = fs().get_inode(child_id);
if (child_inode)
child_inode->increment_link_count();

entries.append({ name.characters(), name.length(), child_id, to_ext2_file_type(mode) });
entries.append({ name.characters_without_null_termination(), name.length(), child_id, to_ext2_file_type(mode) });
bool success = write_directory(entries);
if (success)
m_lookup_cache.set(name, child_id.index());
Expand All @@ -794,7 +794,7 @@ KResult Ext2FSInode::remove_child(const StringView& name)
{
LOCKER(m_lock);
#ifdef EXT2_DEBUG
dbgprintf("Ext2FSInode::remove_child(%s) in inode %u\n", name.characters(), index());
dbg() << "Ext2FSInode::remove_child(" << name << ") in inode " << index();
#endif
ASSERT(is_directory());

Expand All @@ -807,7 +807,7 @@ KResult Ext2FSInode::remove_child(const StringView& name)
InodeIdentifier child_id { fsid(), child_inode_index };

//#ifdef EXT2_DEBUG
dbgprintf("Ext2FS: Removing '%s' in directory %u\n", name.characters(), index());
dbg() << "Ext2FSInode::remove_child(): Removing '" << name << "' in directory " << index();
//#endif

Vector<FS::DirectoryEntry> entries;
Expand Down
7 changes: 7 additions & 0 deletions Kernel/FileSystem/InodeIdentifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ class InodeIdentifier {
u32 m_fsid { 0 };
u32 m_index { 0 };
};

inline const LogStream& operator<<(const LogStream& stream, const InodeIdentifier& value)
{
stream << value.fsid() << ':' << value.index();
return stream;
}

16 changes: 8 additions & 8 deletions Kernel/FileSystem/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ bool VFS::mount(NonnullRefPtr<FS>&& file_system, StringView path)
{
auto result = resolve_path(path, root_custody());
if (result.is_error()) {
kprintf("VFS: mount can't resolve mount point '%s'\n", path.characters());
dbg() << "VFS: mount can't resolve mount point '" << path << "'";
return false;
}
auto& inode = result.value()->inode();
kprintf("VFS: mounting %s{%p} at %s (inode: %u)\n", file_system->class_name(), file_system.ptr(), path.characters(), inode.index());
dbg() << "VFS: Mounting " << file_system->class_name() << " at " << path << " (inode: " << inode.identifier() << ")";
// FIXME: check that this is not already a mount point
auto mount = make<Mount>(*result.value(), move(file_system));
m_mounts.append(move(mount));
Expand Down Expand Up @@ -221,7 +221,7 @@ KResult VFS::mknod(StringView path, mode_t mode, dev_t dev, Custody& base)
return KResult(-EACCES);

FileSystemPath p(path);
dbgprintf("VFS::mknod: '%s' mode=%o dev=%u in %u:%u\n", p.basename().characters(), mode, dev, parent_inode.fsid(), parent_inode.index());
dbg() << "VFS::mknod: '" << p.basename() << "' mode=" << mode << " dev=" << dev << " in " << parent_inode.identifier();
int error;
auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, dev, error);
if (!new_file)
Expand All @@ -243,7 +243,7 @@ KResultOr<NonnullRefPtr<FileDescription>> VFS::create(StringView path, int optio
if (!parent_inode.metadata().may_write(current->process()))
return KResult(-EACCES);
FileSystemPath p(path);
dbgprintf("VFS::create_file: '%s' in %u:%u\n", p.basename().characters(), parent_inode.fsid(), parent_inode.index());
dbg() << "VFS::create: '" << p.basename() << "' in " << parent_inode.identifier();
int error;
auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), mode, 0, 0, error);
if (!new_file)
Expand All @@ -269,7 +269,7 @@ KResult VFS::mkdir(StringView path, mode_t mode, Custody& base)
return KResult(-EACCES);

FileSystemPath p(path);
dbgprintf("VFS::mkdir: '%s' in %u:%u\n", p.basename().characters(), parent_inode.fsid(), parent_inode.index());
dbg() << "VFS::mkdir: '" << p.basename() << "' in " << parent_inode.identifier();
int error;
auto new_dir = parent_inode.fs().create_directory(parent_inode.identifier(), p.basename(), mode, error);
if (new_dir)
Expand Down Expand Up @@ -423,7 +423,7 @@ KResult VFS::chown(Inode& inode, uid_t a_uid, gid_t a_gid)
new_gid = a_gid;
}

dbgprintf("VFS::chown(): inode %u:%u <- uid:%d, gid:%d\n", inode.fsid(), inode.index(), new_uid, new_gid);
dbg() << "VFS::chown(): inode " << inode.identifier() << " <- uid:" << new_uid << " gid:" << new_gid;
return inode.chown(new_uid, new_gid);
}

Expand Down Expand Up @@ -511,12 +511,12 @@ KResult VFS::symlink(StringView target, StringView linkpath, Custody& base)
return KResult(-EACCES);

FileSystemPath p(linkpath);
dbgprintf("VFS::symlink: '%s' (-> '%s') in %u:%u\n", p.basename().characters(), target.characters(), parent_inode.fsid(), parent_inode.index());
dbg() << "VFS::symlink: '" << p.basename() << "' (-> '" << target << "') in " << parent_inode.identifier();
int error;
auto new_file = parent_inode.fs().create_inode(parent_inode.identifier(), p.basename(), 0120644, 0, 0, error);
if (!new_file)
return KResult(error);
ssize_t nwritten = new_file->write_bytes(0, target.length(), (const u8*)target.characters(), nullptr);
ssize_t nwritten = new_file->write_bytes(0, target.length(), (const u8*)target.characters_without_null_termination(), nullptr);
if (nwritten < 0)
return KResult(nwritten);
return KSuccess;
Expand Down
6 changes: 5 additions & 1 deletion Kernel/Net/NetworkAdapter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <AK/HashTable.h>
#include <AK/StringBuilder.h>
#include <Kernel/Lock.h>
#include <Kernel/Net/EtherType.h>
#include <Kernel/Net/EthernetFrameHeader.h>
Expand Down Expand Up @@ -100,7 +101,10 @@ void NetworkAdapter::set_ipv4_address(const IPv4Address& address)
void NetworkAdapter::set_interface_name(const StringView& basename)
{
// FIXME: Find a unique name for this interface, starting with $basename.
m_name = String::format("%s0", basename.characters());
StringBuilder builder;
builder.append(basename);
builder.append('0');
m_name = builder.to_string();
}

bool PacketQueueAlarm::is_ringing() const
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibCore/CIODevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CIODevice : public CObject {
ByteBuffer read_all();

bool write(const u8*, int size);
bool write(const AK::StringView& v) { return write((const u8*)v.characters(), v.length()); }
bool write(const StringView& v) { return write((const u8*)v.characters_without_null_termination(), v.length()); }

// FIXME: I would like this to be const but currently it needs to call populate_read_buffer().
bool can_read_line();
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGUI/GClipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void GClipboard::set_data(const StringView& data)
return;
}
if (!data.is_empty())
memcpy(shared_buffer->data(), data.characters(), data.length() + 1);
memcpy(shared_buffer->data(), data.characters_without_null_termination(), data.length() + 1);
else
((u8*)shared_buffer->data())[0] = '\0';
shared_buffer->seal();
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGUI/GDesktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool GDesktop::set_wallpaper(const StringView& path)
WSAPI_ClientMessage message;
message.type = WSAPI_ClientMessage::Type::SetWallpaper;
ASSERT(path.length() < (int)sizeof(message.text));
strncpy(message.text, path.characters(), path.length());
strncpy(message.text, path.characters_without_null_termination(), path.length());
message.text_length = path.length();
auto response = GEventLoop::current().sync_request(message, WSAPI_ServerMessage::Type::DidSetWallpaper);
return response.value;
Expand Down
4 changes: 2 additions & 2 deletions Libraries/LibGUI/GIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void GIconImpl::set_bitmap_for_size(int size, RefPtr<GraphicsBitmap>&& bitmap)

GIcon GIcon::default_icon(const StringView& name)
{
auto bitmap16 = GraphicsBitmap::load_from_file(String::format("/res/icons/16x16/%s.png", name.characters()));
auto bitmap32 = GraphicsBitmap::load_from_file(String::format("/res/icons/32x32/%s.png", name.characters()));
auto bitmap16 = GraphicsBitmap::load_from_file(String::format("/res/icons/16x16/%s.png", String(name).characters()));
auto bitmap32 = GraphicsBitmap::load_from_file(String::format("/res/icons/32x32/%s.png", String(name).characters()));
return GIcon(move(bitmap16), move(bitmap32));
}
8 changes: 4 additions & 4 deletions Libraries/LibGUI/GTextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void GTextEditor::create_actions()

void GTextEditor::set_text(const StringView& text)
{
if (is_single_line() && text.length() == m_lines[0]->length() && !memcmp(text.characters(), m_lines[0]->characters(), text.length()))
if (is_single_line() && text.length() == m_lines[0]->length() && !memcmp(text.characters_without_null_termination(), m_lines[0]->characters(), text.length()))
return;

m_selection.clear();
Expand Down Expand Up @@ -783,14 +783,14 @@ void GTextEditor::Line::clear()

void GTextEditor::Line::set_text(const StringView& text)
{
if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
if (text.length() == length() && !memcmp(text.characters_without_null_termination(), characters(), length()))
return;
if (text.is_empty()) {
clear();
return;
}
m_text.resize(text.length() + 1);
memcpy(m_text.data(), text.characters(), text.length() + 1);
memcpy(m_text.data(), text.characters_without_null_termination(), text.length() + 1);
}

int GTextEditor::Line::width(const Font& font) const
Expand Down Expand Up @@ -844,7 +844,7 @@ void GTextEditor::Line::truncate(int length)

bool GTextEditor::write_to_file(const StringView& path)
{
int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
int fd = open(String(path).characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
perror("open");
return false;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGUI/GWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ void GWindow::set_icon_path(const StringView& path)
message.type = WSAPI_ClientMessage::Type::SetWindowIcon;
message.window_id = m_window_id;
ASSERT(path.length() < (int)sizeof(message.text));
strcpy(message.text, path.characters());
strcpy(message.text, String(path).characters());
message.text_length = path.length();
GEventLoop::post_message_to_server(message);
}
Expand Down
4 changes: 2 additions & 2 deletions SharedGraphics/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ RefPtr<Font> Font::load_from_file(const StringView& path)

bool Font::write_to_file(const StringView& path)
{
int fd = creat(path.characters(), 0644);
int fd = creat(String(path).characters(), 0644);
if (fd < 0) {
perror("open");
return false;
Expand Down Expand Up @@ -169,7 +169,7 @@ int Font::width(const StringView& string) const

int width = 0;
for (int i = 0; i < string.length(); ++i)
width += glyph_width(string.characters()[i]) + 1;
width += glyph_width(string.characters_without_null_termination()[i]) + 1;

return width - 1;
}
2 changes: 1 addition & 1 deletion SharedGraphics/GraphicsBitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ GraphicsBitmap::~GraphicsBitmap()
void GraphicsBitmap::set_mmap_name(const StringView& name)
{
ASSERT(m_needs_munmap);
::set_mmap_name(m_data, size_in_bytes(), name.characters());
::set_mmap_name(m_data, size_in_bytes(), String(name).characters());
}

void GraphicsBitmap::fill(Color color)
Expand Down
6 changes: 3 additions & 3 deletions SharedGraphics/Painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ void Painter::draw_text(const Rect& rect, const StringView& text, const Font& fo
int new_width = font.width("...");
if (new_width < text_width) {
for (int i = 0; i < final_text.length(); ++i) {
int glyph_width = font.glyph_width(final_text.characters()[i]);
int glyph_width = font.glyph_width(final_text.characters_without_null_termination()[i]);
// NOTE: Glyph spacing should not be added after the last glyph on the line,
// but since we are here because the last glyph does not actually fit on the line,
// we don't have to worry about spacing.
Expand All @@ -582,7 +582,7 @@ void Painter::draw_text(const Rect& rect, const StringView& text, const Font& fo
new_width += glyph_width + glyph_spacing;
}
StringBuilder builder;
builder.append(StringView(final_text.characters(), new_length));
builder.append(StringView(final_text.characters_without_null_termination(), new_length));
builder.append("...");
elided_text = builder.to_string();
final_text = elided_text;
Expand All @@ -609,7 +609,7 @@ void Painter::draw_text(const Rect& rect, const StringView& text, const Font& fo

int space_width = font.glyph_width(' ') + font.glyph_spacing();
for (ssize_t i = 0; i < final_text.length(); ++i) {
char ch = final_text.characters()[i];
char ch = final_text.characters_without_null_termination()[i];
if (ch == ' ') {
point.move_by(space_width, 0);
continue;
Expand Down
Loading

0 comments on commit 0e75aba

Please sign in to comment.