Skip to content

Commit

Permalink
fix: improve string copy safety
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Oct 23, 2024
1 parent 59799e3 commit 6a207a7
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 61 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ target_sources(${PROJECT_NAME} PRIVATE
"src/app.cpp"
"src/desktop.cpp"

"src/icon.cpp"
"src/string.cpp"
"src/memory.cpp"


"src/icon.cpp"
"src/stash.cpp"
"src/script.cpp"
"src/scheme.cpp"
Expand Down
8 changes: 8 additions & 0 deletions private/utils/string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <string>

namespace bindings
{
char *alloc(const std::string &);
} // namespace bindings
14 changes: 7 additions & 7 deletions private/utils/wrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace bindings
using type = T;

template <typename U>
static auto convert(U &&data)
static auto convert(U &data)
{
return data;
};
Expand All @@ -24,7 +24,7 @@ namespace bindings
using type = const char *;

template <typename U>
static auto convert(U &&data)
static auto convert(U &data)
{
return data.c_str();
};
Expand All @@ -36,10 +36,10 @@ namespace bindings
using type = saucer_icon *;

template <typename U>
static auto convert(U &&data)
static auto convert(U &data)
{
// ! User is responsible for freeing this!
return saucer_icon::make(std::forward<U>(data));
return saucer_icon::make(std::move(data));
};
};

Expand All @@ -49,10 +49,10 @@ namespace bindings
using type = saucer_navigation *;

template <typename U>
static auto convert(U &&data)
static auto convert(U &data)
{
// ! User is responsible for freeing this!
return saucer_navigation::make(std::forward<U>(data));
return saucer_navigation::make(std::move(data));
};
};

Expand All @@ -73,7 +73,7 @@ namespace bindings
return [handle, callback]<typename... Ts>(Ts &&...args)
{
auto *converted = wrap<T>::convert(callback);
return std::invoke(converted, handle, wrap<Ts>::convert(std::forward<Ts>(args))...);
return std::invoke(converted, handle, wrap<Ts>::convert(args)...);
};
};
} // namespace bindings
11 changes: 2 additions & 9 deletions src/navigation.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "navigation.h"
#include "navigation.hpp"

#include "memory.h"

#include <cstring>
#include "utils/string.hpp"

extern "C"
{
Expand All @@ -14,12 +12,7 @@ extern "C"

char *saucer_navigation_url(saucer_navigation *handle)
{
auto url = handle->value().url();

auto *rtn = static_cast<char *>(saucer_memory_alloc(url.capacity()));
strncpy(rtn, url.data(), url.capacity());

return rtn;
return bindings::alloc(handle->value().url());
}

bool saucer_navigation_new_window(saucer_navigation *handle)
Expand Down
26 changes: 6 additions & 20 deletions src/scheme.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "scheme.h"
#include "scheme.hpp"

#include "memory.h"
#include "stash.hpp"

#include <ranges>
#include <cstring>
#include "memory.h"
#include "utils/string.hpp"

extern "C"
{
Expand Down Expand Up @@ -37,22 +36,12 @@ extern "C"

char *saucer_scheme_request_url(saucer_scheme_request *handle)
{
auto url = handle->value()->url();

auto *rtn = static_cast<char *>(saucer_memory_alloc(url.capacity()));
strncpy(rtn, url.data(), url.capacity());

return rtn;
return bindings::alloc(handle->value()->url());
}

char *saucer_scheme_request_method(saucer_scheme_request *handle)
{
auto method = handle->value()->method();

auto *rtn = static_cast<char *>(saucer_memory_alloc(method.capacity()));
strncpy(rtn, method.data(), method.capacity());

return rtn;
return bindings::alloc(handle->value()->method());
}

saucer_stash *saucer_scheme_request_content(saucer_scheme_request *handle)
Expand All @@ -73,11 +62,8 @@ extern "C"
const auto &[header, value] = *it;
const auto index = std::distance(data.begin(), it);

(*headers)[index] = static_cast<char *>(saucer_memory_alloc(header.capacity()));
(*values)[index] = static_cast<char *>(saucer_memory_alloc(value.capacity()));

strncpy((*headers)[index], header.data(), header.capacity());
strncpy((*values)[index], value.data(), value.capacity());
(*headers)[index] = bindings::alloc(header);
(*values)[index] = bindings::alloc(value);
}
}
}
14 changes: 14 additions & 0 deletions src/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "utils/string.hpp"

#include "memory.h"

char *bindings::alloc(const std::string &value)
{
const auto size = value.size();
char *const rtn = static_cast<char *>(saucer_memory_alloc(size + 1));

value.copy(rtn, size);
rtn[size + 1] = '\0';

return rtn;
}
18 changes: 3 additions & 15 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
#include "scheme.hpp"
#include "preferences.hpp"

#include "memory.h"

#include "utils/wrap.hpp"
#include "utils/string.hpp"
#include "utils/handle.hpp"

#include <utility>
#include <cstring>

struct saucer_embedded_file : bindings::handle<saucer_embedded_file, saucer::embedded_file>
{
Expand Down Expand Up @@ -52,12 +50,7 @@ extern "C"

char *saucer_webview_page_title(saucer_handle *handle)
{
auto title = handle->page_title();

auto *rtn = static_cast<char *>(saucer_memory_alloc(title.capacity()));
strncpy(rtn, title.data(), title.capacity());

return rtn;
return bindings::alloc(handle->page_title());
}

bool saucer_webview_dev_tools(saucer_handle *handle)
Expand All @@ -67,12 +60,7 @@ extern "C"

char *saucer_webview_url(saucer_handle *handle)
{
auto url = handle->url();

auto *rtn = static_cast<char *>(saucer_memory_alloc(url.capacity()));
strncpy(rtn, url.data(), url.capacity());

return rtn;
return bindings::alloc(handle->url());
}

bool saucer_webview_context_menu(saucer_handle *handle)
Expand Down
10 changes: 2 additions & 8 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
#include "icon.hpp"
#include "webview.hpp"

#include "memory.h"
#include "utils/wrap.hpp"
#include "utils/string.hpp"

#include <utility>
#include <cstring>

#include <saucer/window.hpp>

Expand Down Expand Up @@ -50,12 +49,7 @@ extern "C"

char *saucer_window_title(saucer_handle *handle)
{
auto title = handle->title();

auto *rtn = static_cast<char *>(saucer_memory_alloc(title.capacity()));
strncpy(rtn, title.data(), title.capacity());

return rtn;
return bindings::alloc(handle->title());
}

void saucer_window_size(saucer_handle *handle, int *width, int *height)
Expand Down

0 comments on commit 6a207a7

Please sign in to comment.