fix: clang nullptr dereference warning in utils.cc (#1797)
We know what strings are passed into tr_strip_positional_args(). Instead of using dynamic allocation, use a fixed-size array that's big enough to fit the strings that we have.
This commit is contained in:
parent
90605d4bd3
commit
098c7508e3
|
@ -7,9 +7,12 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef HAVE_MEMMEM
|
#ifdef HAVE_MEMMEM
|
||||||
|
#ifndef _GNU_SOURCE
|
||||||
#define _GNU_SOURCE /* glibc's string.h needs this to pick up memmem */
|
#define _GNU_SOURCE /* glibc's string.h needs this to pick up memmem */
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <array> // std::array
|
||||||
#include <ctype.h> /* isdigit(), tolower() */
|
#include <ctype.h> /* isdigit(), tolower() */
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <float.h> /* DBL_DIG */
|
#include <float.h> /* DBL_DIG */
|
||||||
|
@ -203,23 +206,14 @@ void* tr_memdup(void const* src, size_t byteCount)
|
||||||
|
|
||||||
char const* tr_strip_positional_args(char const* str)
|
char const* tr_strip_positional_args(char const* str)
|
||||||
{
|
{
|
||||||
char* out;
|
static auto buf = std::array<char, 512>{};
|
||||||
static size_t bufsize = 0;
|
|
||||||
static char* buf = NULL;
|
|
||||||
char const* in = str;
|
char const* in = str;
|
||||||
size_t const len = str != NULL ? strlen(str) : 0;
|
size_t pos = 0;
|
||||||
|
|
||||||
if (buf == NULL || bufsize < len)
|
for (; str && *str && pos + 1 < buf.size(); ++str)
|
||||||
{
|
{
|
||||||
bufsize = len * 2 + 1;
|
buf[pos++] = *str;
|
||||||
buf = tr_renew(char, buf, bufsize);
|
|
||||||
}
|
|
||||||
|
|
||||||
out = buf;
|
|
||||||
|
|
||||||
for (; !tr_str_is_empty(str); ++str)
|
|
||||||
{
|
|
||||||
*out++ = *str;
|
|
||||||
|
|
||||||
if (*str == '%' && isdigit(str[1]))
|
if (*str == '%' && isdigit(str[1]))
|
||||||
{
|
{
|
||||||
|
@ -242,8 +236,9 @@ char const* tr_strip_positional_args(char const* str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = '\0';
|
buf[pos] = '\0';
|
||||||
return (in == NULL || strcmp(buf, in) != 0) ? buf : in;
|
|
||||||
|
return in && !strcmp(buf.data(), in) ? in : buf.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue