Isolate a few more string leakage cases.
This commit is contained in:
32
dlib/dlib.cc
32
dlib/dlib.cc
@ -27,6 +27,8 @@
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <Alepha/AutoRAII.h>
|
||||
|
||||
#include "dlib.hh"
|
||||
|
||||
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
|
||||
*/
|
||||
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;
|
||||
|
||||
while (1) {
|
||||
char *buffer = dNew(char, size);
|
||||
if (getcwd (buffer, size) == buffer)
|
||||
return buffer;
|
||||
return { buffer };
|
||||
dFree (buffer);
|
||||
if (errno != ERANGE)
|
||||
return 0;
|
||||
return {};
|
||||
size *= 2;
|
||||
}
|
||||
}
|
||||
@ -920,7 +932,17 @@ char *dGetcwd (void)
|
||||
/**
|
||||
* 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;
|
||||
|
||||
@ -935,7 +957,7 @@ char *dGethomedir (void)
|
||||
homedir = dStrdup("/");
|
||||
}
|
||||
}
|
||||
return homedir;
|
||||
return { homedir };
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -190,8 +190,11 @@ void dLib_show_messages(bool show);
|
||||
/*
|
||||
*- Misc utility functions ----------------------------------------------------
|
||||
*/
|
||||
char *dGetcwd(void);
|
||||
char *dGethomedir(void);
|
||||
CharPtrNoStringConversion dGetcwd();
|
||||
std::optional< std::string > dGetcwd_string();
|
||||
CharPtrNoStringConversion dGethomedir();
|
||||
std::optional< std::string > dGethomedir_string();
|
||||
|
||||
std::optional< std::string > dGetline_string(FILE *stream);
|
||||
char *dGetline_unsafe(FILE *stream);
|
||||
std::optional< std::string > dGetline(FILE *stream);
|
||||
|
@ -54,7 +54,7 @@ char *SharedKey = NULL;
|
||||
*/
|
||||
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());
|
||||
}
|
||||
|
||||
|
@ -1069,7 +1069,7 @@ void StyleEngine::init () {
|
||||
|
||||
void StyleEngine::buildUserStyle () {
|
||||
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());
|
||||
if (not style.empty()) {
|
||||
|
Reference in New Issue
Block a user