Isolate a few more string leakage cases.

This commit is contained in:
2025-09-27 18:18:36 -04:00
parent c8cfc0f038
commit 8356c017e1
4 changed files with 34 additions and 9 deletions

View File

@ -27,6 +27,8 @@
#include <ctype.h> #include <ctype.h>
#include <time.h> #include <time.h>
#include <Alepha/AutoRAII.h>
#include "dlib.hh" #include "dlib.hh"
static bool dLib_show_msg = TRUE; static bool dLib_show_msg = TRUE;
@ -902,17 +904,27 @@ void dLib_show_messages(bool show)
/** /**
* Return the current working directory in a new string * Return the current working directory in a new string
*/ */
char *dGetcwd (void) std::optional< std::string >
dGetcwd_string()
{
auto owned= Alepha::AutoRAII< char * >{ dGetcwd, dFree };
const char *const ptr= owned;
if( not ptr ) return std::nullopt;
return ptr;
}
CharPtrNoStringConversion dGetcwd (void)
{ {
size_t size = 128; size_t size = 128;
while (1) { while (1) {
char *buffer = dNew(char, size); char *buffer = dNew(char, size);
if (getcwd (buffer, size) == buffer) if (getcwd (buffer, size) == buffer)
return buffer; return { buffer };
dFree (buffer); dFree (buffer);
if (errno != ERANGE) if (errno != ERANGE)
return 0; return {};
size *= 2; size *= 2;
} }
} }
@ -920,7 +932,17 @@ char *dGetcwd (void)
/** /**
* Return the home directory in a static string (don't free) * Return the home directory in a static string (don't free)
*/ */
char *dGethomedir (void) std::optional< std::string >
dGethomedir_string()
{
auto owned= Alepha::AutoRAII< char * >{ dGethomedir, dFree };
const char *const ptr= owned;
if( not ptr ) return std::nullopt;
return ptr;
}
CharPtrNoStringConversion dGethomedir (void)
{ {
static char *homedir = NULL; static char *homedir = NULL;
@ -935,7 +957,7 @@ char *dGethomedir (void)
homedir = dStrdup("/"); homedir = dStrdup("/");
} }
} }
return homedir; return { homedir };
} }
/** /**

View File

@ -190,8 +190,11 @@ void dLib_show_messages(bool show);
/* /*
*- Misc utility functions ---------------------------------------------------- *- Misc utility functions ----------------------------------------------------
*/ */
char *dGetcwd(void); CharPtrNoStringConversion dGetcwd();
char *dGethomedir(void); std::optional< std::string > dGetcwd_string();
CharPtrNoStringConversion dGethomedir();
std::optional< std::string > dGethomedir_string();
std::optional< std::string > dGetline_string(FILE *stream); std::optional< std::string > dGetline_string(FILE *stream);
char *dGetline_unsafe(FILE *stream); char *dGetline_unsafe(FILE *stream);
std::optional< std::string > dGetline(FILE *stream); std::optional< std::string > dGetline(FILE *stream);

View File

@ -54,7 +54,7 @@ char *SharedKey = NULL;
*/ */
void cleanup(void) void cleanup(void)
{ {
std::string fname = dGethomedir() + "/"s + dotDILLO_DPID_COMM_KEYS; std::string fname = dGethomedir_string().value() + "/"s + dotDILLO_DPID_COMM_KEYS;
unlink(fname.c_str()); unlink(fname.c_str());
} }

View File

@ -1069,7 +1069,7 @@ void StyleEngine::init () {
void StyleEngine::buildUserStyle () { void StyleEngine::buildUserStyle () {
using namespace std::literals::string_literals; using namespace std::literals::string_literals;
std::string filename = dGethomedir() + "/.flenser/style.css"s; std::string filename = dGethomedir_string().value() + "/.flenser/style.css"s;
const std::string style= a_Misc_file2dstr(filename.c_str()); const std::string style= a_Misc_file2dstr(filename.c_str());
if (not style.empty()) { if (not style.empty()) {