Revert "Use std::make_unique from c++14 #360"

This reverts commit 8dd8f55088.

The make_unique function in util/memory.hh is not at all like
std::make_unique. The former is just a plain wrapper for the unique_ptr
constructor and makes the resulting unique_ptr take ownership of the
instance identified by the given pointer.

std::make_unique on the otherhand allocates a new instance of a given
type and makes creates a unique_ptr that has ownership of that instance.

So what the patch did was to allocate a char pointer, that did not
point at the string we're interested in, but instead points at the
pointer that points to that string. This means that the string that was
meant to be freed by the unique_ptr upon leaving its scope leaks.

Additionally, the check in open_selection() wasn't adapted to handle
the extra layer of indirection added by this change, so it always
assumed that there was some selection, and tried to open it, completely
breaking mouse usage.
This commit is contained in:
Björn Steinbrink 2018-02-12 14:42:04 +01:00
parent a4af9e7e41
commit 9aaf970fba
2 changed files with 15 additions and 14 deletions

View File

@ -4,7 +4,7 @@ GTK = gtk+-3.0
VTE = vte-2.91
TERMINFO = ${PREFIX}/share/terminfo
CXXFLAGS := -std=c++14 -O3 \
CXXFLAGS := -std=c++11 -O3 \
-Wall -Wextra -pedantic \
-Winit-self \
-Wshadow \
@ -32,7 +32,7 @@ endif
LDFLAGS := -s -Wl,--as-needed ${LDFLAGS}
LDLIBS := ${shell pkg-config --libs ${GTK} ${VTE}}
termite: termite.cc url_regex.hh util/clamp.hh util/maybe.hh
termite: termite.cc url_regex.hh util/clamp.hh util/maybe.hh util/memory.hh
${CXX} ${CXXFLAGS} ${LDFLAGS} $< ${LDLIBS} -o $@
install: termite termite.desktop termite.terminfo

View File

@ -42,6 +42,7 @@
#include "url_regex.hh"
#include "util/clamp.hh"
#include "util/maybe.hh"
#include "util/memory.hh"
using namespace std::placeholders;
@ -296,18 +297,18 @@ static void launch_in_directory(VteTerminal *vte) {
g_printerr("no directory uri set\n");
return;
}
auto dir = std::make_unique<gchar*>(g_filename_from_uri(uri, nullptr, nullptr));
auto dir = make_unique(g_filename_from_uri(uri, nullptr, nullptr), g_free);
char term[] = "termite"; // maybe this should be argv[0]
char *cmd[] = {term, nullptr};
g_spawn_async(*dir.get(), cmd, nullptr, G_SPAWN_SEARCH_PATH, nullptr, nullptr, nullptr, nullptr);
g_spawn_async(dir.get(), cmd, nullptr, G_SPAWN_SEARCH_PATH, nullptr, nullptr, nullptr, nullptr);
}
static void find_urls(VteTerminal *vte, search_panel_info *panel_info) {
GRegex *regex = g_regex_new(url_regex, G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY, nullptr);
GArray *attributes = g_array_new(FALSE, FALSE, sizeof(VteCharAttributes));
auto content = std::make_unique<gchar*>(vte_terminal_get_text(vte, nullptr, nullptr, attributes));
auto content = make_unique(vte_terminal_get_text(vte, nullptr, nullptr, attributes), g_free);
for (char *s_ptr = *content.get(), *saveptr; ; s_ptr = nullptr) {
for (char *s_ptr = content.get(), *saveptr; ; s_ptr = nullptr) {
const char *token = strtok_r(s_ptr, "\n", &saveptr);
if (!token) {
break;
@ -322,7 +323,7 @@ static void find_urls(VteTerminal *vte, search_panel_info *panel_info) {
g_match_info_fetch_pos(info, 0, &pos, nullptr);
const long first_row = g_array_index(attributes, VteCharAttributes, 0).row;
const auto attr = g_array_index(attributes, VteCharAttributes, token + pos - *content.get());
const auto attr = g_array_index(attributes, VteCharAttributes, token + pos - content.get());
panel_info->url_list.emplace_back(g_match_info_fetch(info, 0),
attr.column,
@ -569,9 +570,9 @@ static void open_selection(char *browser, VteTerminal *vte) {
}
if (browser) {
auto selection = std::make_unique<gchar*>(vte_terminal_get_selection(vte));
auto selection = make_unique(vte_terminal_get_selection(vte), g_free);
if (selection && *selection) {
launch_browser(browser, *selection.get());
launch_browser(browser, selection.get());
}
} else {
g_printerr("no browser to open url\n");
@ -1182,21 +1183,21 @@ gboolean position_overlay_cb(GtkBin *overlay, GtkWidget *widget, GdkRectangle *a
gboolean button_press_cb(VteTerminal *vte, GdkEventButton *event, const config_info *info) {
if (info->clickable_url && event->type == GDK_BUTTON_PRESS) {
#if VTE_CHECK_VERSION (0, 49, 1)
auto match = std::make_unique<gchar*>(vte_terminal_hyperlink_check_event(vte, (GdkEvent*)event));
auto match = make_unique(vte_terminal_hyperlink_check_event(vte, (GdkEvent*)event), g_free);
if (!match) {
match = std::make_unique<gchar*>(check_match(vte, event));
match = make_unique(check_match(vte, event), g_free);
}
#else
auto match = std::make_unique<gchar*>(check_match(vte, event));
auto match = make_unique(check_match(vte, event), g_free);
#endif
if (!match)
return FALSE;
if (event->button == 1) {
launch_browser(info->browser, *match.get());
launch_browser(info->browser, match.get());
} else if (event->button == 3) {
GtkClipboard *clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gtk_clipboard_set_text(clipboard, *match.get(), -1);
gtk_clipboard_set_text(clipboard, match.get(), -1);
}
return TRUE;