fix: possible memory issues

This commit is contained in:
javalsai 2025-07-04 04:47:32 +02:00
parent 5a6471dd00
commit a316afecb2
Signed by: javalsai
SSH Key Fingerprint: SHA256:3G83yKhBUWVABVX/vPWH88xnK4+ptMtHkZGCRXD4Mk8

View File

@ -109,6 +109,7 @@ static char* fmt_time(const char* fmt) {
}
char* trunc_gethostname(const size_t MAXSIZE, const char* const ELLIPSIS) {
if(utf8len(ELLIPSIS) > MAXSIZE) return NULL;
size_t alloc_size = MAXSIZE + 1;
char* buf = malloc(alloc_size);
if (!buf) return NULL;
@ -116,7 +117,7 @@ char* trunc_gethostname(const size_t MAXSIZE, const char* const ELLIPSIS) {
if (gethostname(buf, alloc_size) == 0) return buf;
if (errno == ENAMETOOLONG) {
buf[alloc_size] = '\0';
if (utf8len(buf) > MAXSIZE) {
if (utf8len(buf) > MAXSIZE - utf8len(ELLIPSIS)) {
strcpy(&buf[MAXSIZE - utf8len(ELLIPSIS)], ELLIPSIS);
return buf;
}