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 <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 };
}
/**

View File

@ -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);