commit 63332d309204b50fed4231e8b88a7086a8939c88
parent 6adc3af08329d21c86fb2cfbe6a09b8477e20756
Author: Andrew Laack <andrew@laack.co>
Date: Mon, 28 Sep 2026 12:52:41 -0500
Unique pointer for openssl critical code.
Diffstat:
9 files changed, 76 insertions(+), 25 deletions(-)
diff --git a/Makefile b/Makefile
@@ -27,5 +27,10 @@ pure-test:
./test3.out
rm test3.out
+# NOTE: This is purposely not included in the make test command
+mem-leak-test:
+ ${STCOMMAND_P} tests/mem-leak-test.cpp ${COMMAND_S} -o test4.out
+ valgrind --tool=memcheck ./test4.out
+ rm test4.out
test: pure-test browser-test fetch-test
diff --git a/TODO.txt b/TODO.txt
@@ -95,3 +95,7 @@
---
- some sort of rpc thing to do language tranlsations
+
+---
+
+- finalize name + renaming
diff --git a/config.mk b/config.mk
@@ -21,3 +21,5 @@ TCOMMAND_S = ${BASE_FILES} ${TLIBS}
DCOMMAND_P = ${CC} ${DFLAGS}
DCOMMAND_S = ${BASE_FILES} ${LIBS}
+
+STCOMMAND_P = ${CC} ${CFLAGS}
diff --git a/include/shared.hpp b/include/shared.hpp
@@ -13,3 +13,5 @@ const int COLOR_PREFORMATTED = 201; // color used for preformatted text regions
// We guarantee responses can be at least the size limit, and at most 4096 additional bytes
const int RESPONSE_SIZE_LIMIT_MB = 5;
+
+const char openUnknownScheme [] = "xdg-open";
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -1,6 +1,5 @@
#include "../include/browser.hpp"
#include <cassert>
-#include <iostream>
#include <string>
#include <thread>
#include <unistd.h>
diff --git a/src/gemini-client.cpp b/src/gemini-client.cpp
@@ -23,29 +23,33 @@ Site* GeminiClient::getNetworkedSite(Link link, std::string crtPath, std::string
conn = host + ":" + std::to_string(link.getLinkDestination().get_port());
}
- SSL_CTX* ctx = SSL_CTX_new(TLS_client_method());
+ std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)> ctx(SSL_CTX_new(TLS_client_method()), SSL_CTX_free);
+ if (!ctx) {
+ return nullptr;
+ }
if(crtPath != "" && keyPath != "") {
- if (SSL_CTX_use_certificate_file(ctx, crtPath.c_str(), SSL_FILETYPE_PEM) <= 0 ||
- SSL_CTX_use_PrivateKey_file(ctx, keyPath.c_str(), SSL_FILETYPE_PEM) <= 0 ||
- !SSL_CTX_check_private_key(ctx)) {
- SSL_CTX_free(ctx);
+ if (SSL_CTX_use_certificate_file(ctx.get(), crtPath.c_str(), SSL_FILETYPE_PEM) <= 0 || SSL_CTX_use_PrivateKey_file(ctx.get(), keyPath.c_str(), SSL_FILETYPE_PEM) <= 0 || !SSL_CTX_check_private_key(ctx.get())) {
return nullptr;
}
}
- BIO* bio = BIO_new_ssl_connect(ctx);
+ std::unique_ptr<BIO, decltype(&BIO_free_all)> bio(BIO_new_ssl_connect(ctx.get()), BIO_free_all);
+
+ if (!bio) {
+ return nullptr;
+ }
SSL* ssl;
- BIO_get_ssl(bio, &ssl);
+ BIO_get_ssl(bio.get(), &ssl);
SSL_set_tlsext_host_name(ssl, host.c_str());
- BIO_set_conn_hostname(bio, conn.c_str());
- BIO_set_nbio(bio, 1);
+ BIO_set_conn_hostname(bio.get(), conn.c_str());
+ BIO_set_nbio(bio.get(), 1);
auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5);
- while (BIO_do_connect(bio) <= 0) {
+ while (BIO_do_connect(bio.get()) <= 0) {
int fd = -1;
@@ -53,23 +57,21 @@ Site* GeminiClient::getNetworkedSite(Link link, std::string crtPath, std::string
pollfd p{};
// yikes.
- if (!BIO_should_retry(bio) || BIO_get_fd(bio, &fd) < 0 || fd < 0 || ms <= 0 || (p = {fd, short(BIO_should_read(bio) ? POLLIN : POLLOUT), 0}, poll(&p, 1, int(ms)) <= 0)) {
- BIO_free_all(bio);
- SSL_CTX_free(ctx);
+ if (!BIO_should_retry(bio.get()) || BIO_get_fd(bio.get(), &fd) < 0 || fd < 0 || ms <= 0 || (p = {fd, short(BIO_should_read(bio.get()) ? POLLIN : POLLOUT), 0}, poll(&p, 1, int(ms)) <= 0)) {
return nullptr;
}
}
// timeout, 5 seconds
int fd = -1;
- if (BIO_get_fd(bio, &fd) >= 0 && fd >= 0) {
+ if (BIO_get_fd(bio.get(), &fd) >= 0 && fd >= 0) {
BIO_socket_nbio(fd, 0);
timeval tv{5, 0};
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
}
- BIO_write(bio, req.data(), (int)req.size());
+ BIO_write(bio.get(), req.data(), (int)req.size());
std::string response;
char buf[4096];
@@ -78,7 +80,7 @@ Site* GeminiClient::getNetworkedSite(Link link, std::string crtPath, std::string
int sizeRemaining = RESPONSE_SIZE_LIMIT_MB * 1024 * 1024;
bool truncated = false;
- while ((n = BIO_read(bio, buf, sizeof buf)) > 0) {
+ while ((n = BIO_read(bio.get(), buf, sizeof buf)) > 0) {
response.append(buf, n);
sizeRemaining -= n;
@@ -88,8 +90,7 @@ Site* GeminiClient::getNetworkedSite(Link link, std::string crtPath, std::string
}
}
- BIO_free_all(bio);
- SSL_CTX_free(ctx);
+ bio.reset();
std::size_t nl = response.find('\n');
if (nl == std::string::npos) {
diff --git a/src/utils.cpp b/src/utils.cpp
@@ -38,8 +38,8 @@ void openUrl(const std::string& url) {
posix_spawn_file_actions_adddup2(&fa, 1, 2);
pid_t pid;
- char* argv[] = {(char*)"xdg-open", (char*)url.c_str(), nullptr};
- if (posix_spawnp(&pid, "xdg-open", &fa, nullptr, argv, environ) == 0) {
+ char* argv[] = {(char*)openUnknownScheme, (char*)url.c_str(), nullptr};
+ if (posix_spawnp(&pid, openUnknownScheme, &fa, nullptr, argv, environ) == 0) {
waitpid(pid, nullptr, 0);
}
posix_spawn_file_actions_destroy(&fa);
diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp
@@ -81,10 +81,6 @@ TEST_CASE("Test encoding allows relative linking with : in parameter") {
REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://tlgs.one/search?https%3A%2F%2Ftest.com");
}
-
-
-
-
TEST_CASE("Sanitize characters to draw tests") {
Browser b {};
bool res = b.goToSite("file:///home/andrew/gitRepos/gemini-browser/tests/sites/line-return.gmi");
diff --git a/tests/mem-leak-test.cpp b/tests/mem-leak-test.cpp
@@ -0,0 +1,42 @@
+#include "../include/browser.hpp"
+#include <cstdlib>
+#include <thread>
+#include <vector>
+
+
+void traversal() {
+ Browser b{};
+ b.goToSite("gemini://laack.co", true);
+
+ for(int i = 0; i < 100; ++i) {
+ auto* lls = b.getLinkLines();
+ std::size_t count = lls->size();
+ delete lls;
+ if(count == 0) {
+ b.goBack();
+ } else if(rand() % 5 == 0) {
+ b.goForward();
+ } else if(rand() % 5 == 0) {
+ b.goBack();
+ } else {
+ b.followLinkNumber(rand() % count + 1);
+ }
+ }
+
+}
+
+// NOTE: When running this you probably want to run something like:
+// `watch pkill {browser}`
+// because it'll probably try to follow http links that are found.
+
+int main() {
+ std::vector<std::thread*> vec {};
+ for(int x = 0; x < 10; ++x) {
+ auto* t = new std::thread (&traversal);
+ vec.push_back(t);
+ }
+ for(auto& th : vec) {
+ th->join();
+ delete th;
+ }
+}