String alloc shouldn't convert to std::string
.
Some checks failed
CI / ubuntu-latest-html-tests (push) Has been cancelled
CI / alpine-mbedtls-3_6_0 (push) Has been cancelled
CI / ubuntu-latest-no-tls (push) Has been cancelled
CI / ubuntu-latest-mbedtls2 (push) Has been cancelled
CI / ubuntu-latest-openssl-3 (push) Has been cancelled
CI / ubuntu-latest-with-old-std (push) Has been cancelled
CI / ubuntu-20-04-openssl-1-1 (push) Has been cancelled
CI / macOS-13-openssl-1-1 (push) Has been cancelled
CI / macOS-13-openssl-3 (push) Has been cancelled
CI / freebsd-14-openssl-3 (push) Has been cancelled
CI / windows-mbedtls (push) Has been cancelled
Some checks failed
CI / ubuntu-latest-html-tests (push) Has been cancelled
CI / alpine-mbedtls-3_6_0 (push) Has been cancelled
CI / ubuntu-latest-no-tls (push) Has been cancelled
CI / ubuntu-latest-mbedtls2 (push) Has been cancelled
CI / ubuntu-latest-openssl-3 (push) Has been cancelled
CI / ubuntu-latest-with-old-std (push) Has been cancelled
CI / ubuntu-20-04-openssl-1-1 (push) Has been cancelled
CI / macOS-13-openssl-1-1 (push) Has been cancelled
CI / macOS-13-openssl-3 (push) Has been cancelled
CI / freebsd-14-openssl-3 (push) Has been cancelled
CI / windows-mbedtls (push) Has been cancelled
As I work through making code use more C++ RAII and such, most of the work is handling strings, especially temporaries. As member variables which manage string memory get turned into `std::string`, some use cases might wind up leaking memory. (One was found in this change.) By using a non-convertible-to-string result, such accidents should be avoided.
This commit is contained in:
20
dlib/dlib.cc
20
dlib/dlib.cc
@ -74,32 +74,32 @@ void dFree (void *mem)
|
|||||||
*- strings (char *) ----------------------------------------------------------
|
*- strings (char *) ----------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *dStrdup(const char *s)
|
CharPtrNoStringConversion dStrdup(const char *s)
|
||||||
{
|
{
|
||||||
if (s) {
|
if (s) {
|
||||||
int len = strlen(s)+1;
|
int len = strlen(s)+1;
|
||||||
char *ns = dNew(char, len);
|
char *ns = dNew(char, len);
|
||||||
memcpy(ns, s, len);
|
memcpy(ns, s, len);
|
||||||
return ns;
|
return { ns };
|
||||||
}
|
}
|
||||||
return NULL;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
char *dStrndup(const char *s, size_t sz)
|
CharPtrNoStringConversion dStrndup(const char *s, size_t sz)
|
||||||
{
|
{
|
||||||
if (s) {
|
if (s) {
|
||||||
char *ns = dNew(char, sz+1);
|
char *ns = dNew(char, sz+1);
|
||||||
memcpy(ns, s, sz);
|
memcpy(ns, s, sz);
|
||||||
ns[sz] = 0;
|
ns[sz] = 0;
|
||||||
return ns;
|
return { ns };
|
||||||
}
|
}
|
||||||
return NULL;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Concatenate a NULL-terminated list of strings
|
* Concatenate a NULL-terminated list of strings
|
||||||
*/
|
*/
|
||||||
char *dStrconcat(const char *s1, ...)
|
CharPtrNoStringConversion dStrconcat(const char *s1, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
char *s, *ns = NULL;
|
char *s, *ns = NULL;
|
||||||
@ -113,7 +113,7 @@ char *dStrconcat(const char *s1, ...)
|
|||||||
ns = dstr->str;
|
ns = dstr->str;
|
||||||
dStr_free(dstr, 0);
|
dStr_free(dstr, 0);
|
||||||
}
|
}
|
||||||
return ns;
|
return { ns };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -146,11 +146,11 @@ void dStrshred(char *s)
|
|||||||
/**
|
/**
|
||||||
* Return a new string of length 'len' filled with 'c' characters
|
* Return a new string of length 'len' filled with 'c' characters
|
||||||
*/
|
*/
|
||||||
char *dStrnfill(size_t len, char c)
|
CharPtrNoStringConversion dStrnfill(size_t len, char c)
|
||||||
{
|
{
|
||||||
char *ret = dNew(char, len+1);
|
char *ret = dNew(char, len+1);
|
||||||
for (ret[len] = 0; len > 0; ret[--len] = c);
|
for (ret[len] = 0; len > 0; ret[--len] = c);
|
||||||
return ret;
|
return { ret };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
15
dlib/dlib.hh
15
dlib/dlib.hh
@ -11,6 +11,13 @@
|
|||||||
|
|
||||||
#include "d_size.h"
|
#include "d_size.h"
|
||||||
|
|
||||||
|
struct CharPtrNoStringConversion
|
||||||
|
{
|
||||||
|
char *ptr= nullptr;
|
||||||
|
|
||||||
|
operator char *() const noexcept { return ptr; }
|
||||||
|
};
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
@ -84,11 +91,11 @@ void dFree (void *mem);
|
|||||||
/*
|
/*
|
||||||
*- C strings -----------------------------------------------------------------
|
*- C strings -----------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
char *dStrdup(const char *s);
|
CharPtrNoStringConversion dStrdup(const char *s);
|
||||||
char *dStrndup(const char *s, size_t sz);
|
CharPtrNoStringConversion dStrndup(const char *s, size_t sz);
|
||||||
char *dStrconcat(const char *s1, ...);
|
CharPtrNoStringConversion dStrconcat(const char *s1, ...);
|
||||||
char *dStrstrip(char *s);
|
char *dStrstrip(char *s);
|
||||||
char *dStrnfill(size_t len, char c);
|
CharPtrNoStringConversion dStrnfill(size_t len, char c);
|
||||||
char *dStrsep(char **orig, const char *delim);
|
char *dStrsep(char **orig, const char *delim);
|
||||||
void dStrshred(char *s);
|
void dStrshred(char *s);
|
||||||
char *dStriAsciiStr(const char *haystack, const char *needle);
|
char *dStriAsciiStr(const char *haystack, const char *needle);
|
||||||
|
@ -1329,7 +1329,7 @@ static std::string Cookies_get(char *url_host, char *url_path,
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (disabled)
|
if (disabled)
|
||||||
return dStrdup("");
|
return "";
|
||||||
|
|
||||||
matching_cookies = dList_new(8);
|
matching_cookies = dList_new(8);
|
||||||
|
|
||||||
|
@ -274,7 +274,7 @@ static unsigned char *datauri_get_data(char *url, size_t *p_sz)
|
|||||||
data = (unsigned char *)a_Url_decode_hex_str(p, p_sz);
|
data = (unsigned char *)a_Url_decode_hex_str(p, p_sz);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
data = (unsigned char *)dStrdup("");
|
data = (unsigned char *)dStrdup("").ptr;
|
||||||
*p_sz = 0;
|
*p_sz = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -866,9 +866,9 @@ FltkEntryResource::FltkEntryResource (FltkPlatform *platform, int size,
|
|||||||
{
|
{
|
||||||
this->size = size;
|
this->size = size;
|
||||||
this->password = password;
|
this->password = password;
|
||||||
this->label = label ? dStrdup(label) : NULL;
|
this->label = dStrdup(label);
|
||||||
this->label_w = 0;
|
this->label_w = 0;
|
||||||
this->placeholder = placeholder ? dStrdup(placeholder) : NULL;
|
this->placeholder = dStrdup(placeholder);
|
||||||
|
|
||||||
initText = NULL;
|
initText = NULL;
|
||||||
editable = false;
|
editable = false;
|
||||||
@ -1052,7 +1052,7 @@ FltkMultiLineTextResource::FltkMultiLineTextResource (FltkPlatform *platform,
|
|||||||
MSG_WARN("numRows = %d is set to 1.\n", numRows);
|
MSG_WARN("numRows = %d is set to 1.\n", numRows);
|
||||||
numRows = 1;
|
numRows = 1;
|
||||||
}
|
}
|
||||||
this->placeholder = placeholder ? dStrdup(placeholder) : NULL;
|
this->placeholder = dStrdup(placeholder);
|
||||||
|
|
||||||
init (platform);
|
init (platform);
|
||||||
}
|
}
|
||||||
|
@ -371,7 +371,7 @@ TrieBuilder::~TrieBuilder ()
|
|||||||
void TrieBuilder::insert (const char *key, const char *value)
|
void TrieBuilder::insert (const char *key, const char *value)
|
||||||
{
|
{
|
||||||
dataList->increase ();
|
dataList->increase ();
|
||||||
dataList->getLastRef ()->key = (unsigned char *) dStrdup(key);
|
dataList->getLastRef ()->key = (unsigned char *) dStrdup(key).ptr;
|
||||||
dataList->getLastRef ()->value = dataZone->strdup (value);
|
dataList->getLastRef ()->value = dataZone->strdup (value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,7 +290,7 @@ void ConstString::intoStringBuffer(misc::StringBuffer *sb) const
|
|||||||
// String
|
// String
|
||||||
// ------------
|
// ------------
|
||||||
|
|
||||||
String::String (const char *str): ConstString (str ? dStrdup(str) : NULL)
|
String::String (const char *str): ConstString (dStrdup(str))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -861,7 +861,7 @@ void Html_tag_open_option(DilloHtml *html, const char *tag, int tagsize)
|
|||||||
bool enabled = (a_Html_get_attr(html, tag, tagsize, "disabled") == NULL);
|
bool enabled = (a_Html_get_attr(html, tag, tagsize, "disabled") == NULL);
|
||||||
|
|
||||||
auto option =
|
auto option =
|
||||||
std::make_unique< DilloHtmlOption >(value ? dStrdup( value.value().c_str() ) : nullptr, label ? dStrdup( label.value().c_str() ) : nullptr, selected, enabled );
|
std::make_unique< DilloHtmlOption >(value ? dStrdup( value.value().c_str() ).ptr : nullptr, label ? dStrdup( label.value().c_str() ).ptr : nullptr, selected, enabled );
|
||||||
|
|
||||||
input->select->addOpt( std::move( option ) );
|
input->select->addOpt( std::move( option ) );
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user