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

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:
2025-09-08 13:24:56 -04:00
parent 97baa28f57
commit 1bf12c858f
8 changed files with 29 additions and 22 deletions

View File

@ -74,32 +74,32 @@ void dFree (void *mem)
*- strings (char *) ----------------------------------------------------------
*/
char *dStrdup(const char *s)
CharPtrNoStringConversion dStrdup(const char *s)
{
if (s) {
int len = strlen(s)+1;
char *ns = dNew(char, 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) {
char *ns = dNew(char, sz+1);
memcpy(ns, s, sz);
ns[sz] = 0;
return ns;
return { ns };
}
return NULL;
return {};
}
/**
* Concatenate a NULL-terminated list of strings
*/
char *dStrconcat(const char *s1, ...)
CharPtrNoStringConversion dStrconcat(const char *s1, ...)
{
va_list args;
char *s, *ns = NULL;
@ -113,7 +113,7 @@ char *dStrconcat(const char *s1, ...)
ns = dstr->str;
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
*/
char *dStrnfill(size_t len, char c)
CharPtrNoStringConversion dStrnfill(size_t len, char c)
{
char *ret = dNew(char, len+1);
for (ret[len] = 0; len > 0; ret[--len] = c);
return ret;
return { ret };
}
/**

View File

@ -11,6 +11,13 @@
#include "d_size.h"
struct CharPtrNoStringConversion
{
char *ptr= nullptr;
operator char *() const noexcept { return ptr; }
};
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@ -84,11 +91,11 @@ void dFree (void *mem);
/*
*- C strings -----------------------------------------------------------------
*/
char *dStrdup(const char *s);
char *dStrndup(const char *s, size_t sz);
char *dStrconcat(const char *s1, ...);
CharPtrNoStringConversion dStrdup(const char *s);
CharPtrNoStringConversion dStrndup(const char *s, size_t sz);
CharPtrNoStringConversion dStrconcat(const char *s1, ...);
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);
void dStrshred(char *s);
char *dStriAsciiStr(const char *haystack, const char *needle);