BLI_string: move DEBUG_STRSIZE check into a macro, check sizes after nil
- Avoid inline ifdef checks for DEBUG_STRSIZE - Add BLI_string_debug_size_after_nil to ensure strings to manipulate have the expected buffer size after the nil terminator. - Add checks to more string manipulation functions. Further changes are required for this to be enabled during regular development as the RNA currently allocates the strings length but passes in the buffer size as a limit which conflicts with DEBUG_STRSIZE.
This commit is contained in:
@@ -600,6 +600,22 @@ int BLI_string_find_split_words(const char *str,
|
||||
|
||||
/** \} */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name String Debugging
|
||||
* \{ */
|
||||
#ifdef DEBUG_STRSIZE
|
||||
# define BLI_string_debug_size(str, str_maxncpy) memset(str, 0xff, sizeof(*(str)) * str_maxncpy)
|
||||
/**
|
||||
* Fill `str` with a non-nil value after the trailing nil character,
|
||||
* use to ensure buffer sizes passed to string functions are correct.
|
||||
*/
|
||||
void BLI_string_debug_size_after_nil(char *str, size_t str_maxncpy);
|
||||
#else
|
||||
# define BLI_string_debug_size(str, str_maxncpy) (void)(0 ? ((str) + (str_maxncpy)) : 0)
|
||||
# define BLI_string_debug_size_after_nil(str, str_maxncpy) BLI_string_debug_size(str, str_maxncpy)
|
||||
#endif /* !DEBUG_STRSIZE */
|
||||
|
||||
/** \} */
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -45,8 +45,6 @@ static int BLI_path_unc_prefix_len(const char *path);
|
||||
static bool BLI_path_is_abs_win32(const char *path);
|
||||
#endif /* WIN32 */
|
||||
|
||||
// #define DEBUG_STRSIZE
|
||||
|
||||
int BLI_path_sequence_decode(const char *string,
|
||||
char *head,
|
||||
const size_t head_maxncpy,
|
||||
@@ -54,14 +52,12 @@ int BLI_path_sequence_decode(const char *string,
|
||||
const size_t tail_maxncpy,
|
||||
ushort *r_digits_len)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
if (head) {
|
||||
memset(head, 0xff, sizeof(*head) * head_maxncpy);
|
||||
BLI_string_debug_size(head, head_maxncpy);
|
||||
}
|
||||
if (tail) {
|
||||
memset(tail, 0xff, sizeof(*tail) * tail_maxncpy);
|
||||
BLI_string_debug_size(tail, tail_maxncpy);
|
||||
}
|
||||
#endif
|
||||
|
||||
uint nums = 0, nume = 0;
|
||||
int i;
|
||||
@@ -126,9 +122,8 @@ void BLI_path_sequence_encode(char *string,
|
||||
ushort numlen,
|
||||
int pic)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(string, 0xff, sizeof(*string) * string_maxncpy);
|
||||
#endif
|
||||
BLI_string_debug_size(string, string_maxncpy);
|
||||
|
||||
BLI_snprintf(string, string_maxncpy, "%s%.*d%s", head, numlen, MAX2(0, pic), tail);
|
||||
}
|
||||
|
||||
@@ -591,6 +586,8 @@ void BLI_path_normalize_unc_16(wchar_t *path_16)
|
||||
|
||||
void BLI_path_rel(char *file, const char *relfile)
|
||||
{
|
||||
BLI_string_debug_size_after_nil(file, FILE_MAX);
|
||||
|
||||
const char *lslash;
|
||||
char temp[FILE_MAX];
|
||||
char res[FILE_MAX];
|
||||
@@ -726,9 +723,8 @@ void BLI_path_rel(char *file, const char *relfile)
|
||||
|
||||
bool BLI_path_suffix(char *string, size_t maxlen, const char *suffix, const char *sep)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(string, 0xff, sizeof(*string) * maxlen);
|
||||
#endif
|
||||
BLI_string_debug_size_after_nil(string, maxlen);
|
||||
|
||||
const size_t suffix_len = strlen(suffix);
|
||||
const size_t sep_len = strlen(sep);
|
||||
char *extension = (char *)BLI_path_extension_or_end(string);
|
||||
@@ -854,6 +850,8 @@ static void ensure_digits(char *path, int digits)
|
||||
|
||||
bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits)
|
||||
{
|
||||
BLI_string_debug_size_after_nil(path, path_maxncpy);
|
||||
|
||||
int ch_sta, ch_end;
|
||||
|
||||
if (digits) {
|
||||
@@ -872,6 +870,8 @@ bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits)
|
||||
|
||||
bool BLI_path_frame_range(char *path, int sta, int end, int digits)
|
||||
{
|
||||
BLI_string_debug_size_after_nil(path, FILE_MAX);
|
||||
|
||||
int ch_sta, ch_end;
|
||||
|
||||
if (digits) {
|
||||
@@ -927,6 +927,7 @@ bool BLI_path_frame_get(const char *path, int *r_frame, int *r_digits_len)
|
||||
|
||||
void BLI_path_frame_strip(char *path, char *r_ext, const size_t ext_maxlen)
|
||||
{
|
||||
BLI_string_debug_size(r_ext, ext_maxlen);
|
||||
*r_ext = '\0';
|
||||
if (*path == '\0') {
|
||||
return;
|
||||
@@ -960,6 +961,8 @@ bool BLI_path_frame_check_chars(const char *path)
|
||||
|
||||
void BLI_path_to_display_name(char *display_name, int maxlen, const char *name)
|
||||
{
|
||||
BLI_string_debug_size(display_name, maxlen);
|
||||
|
||||
/* Strip leading underscores and spaces. */
|
||||
int strip_offset = 0;
|
||||
while (ELEM(name[strip_offset], '_', ' ')) {
|
||||
@@ -997,6 +1000,8 @@ void BLI_path_to_display_name(char *display_name, int maxlen, const char *name)
|
||||
|
||||
bool BLI_path_abs(char *path, const char *basepath)
|
||||
{
|
||||
BLI_string_debug_size_after_nil(path, FILE_MAX);
|
||||
|
||||
const bool wasrelative = BLI_path_is_rel(path);
|
||||
char tmp[FILE_MAX];
|
||||
char base[FILE_MAX];
|
||||
@@ -1113,9 +1118,7 @@ bool BLI_path_is_abs_from_cwd(const char *path)
|
||||
|
||||
bool BLI_path_abs_from_cwd(char *path, const size_t maxlen)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(path, 0xff, sizeof(*path) * maxlen);
|
||||
#endif
|
||||
BLI_string_debug_size_after_nil(path, maxlen);
|
||||
|
||||
if (!BLI_path_is_abs_from_cwd(path)) {
|
||||
char cwd[FILE_MAX];
|
||||
@@ -1189,9 +1192,8 @@ bool BLI_path_program_extensions_add_win32(char *program_name, const size_t maxl
|
||||
|
||||
bool BLI_path_program_search(char *program_filepath, const size_t maxlen, const char *program_name)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(program_filepath, 0xff, sizeof(*program_filepath) * maxlen);
|
||||
#endif
|
||||
BLI_string_debug_size(program_filepath, maxlen);
|
||||
|
||||
const char *path;
|
||||
bool retval = false;
|
||||
|
||||
@@ -1400,9 +1402,8 @@ bool BLI_path_extension_glob_validate(char *ext_fnmatch)
|
||||
|
||||
bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(path, 0xff, sizeof(*path) * maxlen);
|
||||
#endif
|
||||
BLI_string_debug_size_after_nil(path, maxlen);
|
||||
|
||||
char *path_ext = (char *)BLI_path_extension_or_end(path);
|
||||
const size_t ext_len = strlen(ext);
|
||||
if ((path_ext - path) + ext_len >= maxlen) {
|
||||
@@ -1425,9 +1426,8 @@ bool BLI_path_extension_strip(char *path)
|
||||
|
||||
bool BLI_path_extension_ensure(char *path, size_t maxlen, const char *ext)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(path, 0xff, sizeof(*path) * maxlen);
|
||||
#endif
|
||||
BLI_string_debug_size_after_nil(path, maxlen);
|
||||
|
||||
/* First check the extension is already there.
|
||||
* If `path_ext` is the end of the string this is simply checking if `ext` is also empty. */
|
||||
const char *path_ext = BLI_path_extension_or_end(path);
|
||||
@@ -1459,9 +1459,8 @@ bool BLI_path_extension_ensure(char *path, size_t maxlen, const char *ext)
|
||||
|
||||
bool BLI_path_filename_ensure(char *filepath, size_t maxlen, const char *filename)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(filepath, 0xff, sizeof(*filepath) * maxlen);
|
||||
#endif
|
||||
BLI_string_debug_size_after_nil(filepath, maxlen);
|
||||
|
||||
char *c = (char *)BLI_path_slash_rfind(filepath);
|
||||
if (!c || ((c - filepath) < maxlen - (strlen(filename) + 1))) {
|
||||
strcpy(c ? &c[1] : filepath, filename);
|
||||
@@ -1480,10 +1479,9 @@ static size_t path_split_dir_file_offset(const char *string)
|
||||
void BLI_path_split_dir_file(
|
||||
const char *filepath, char *dir, const size_t dirlen, char *file, const size_t filelen)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dir, 0xff, sizeof(*dir) * dirlen);
|
||||
memset(file, 0xff, sizeof(*file) * filelen);
|
||||
#endif
|
||||
BLI_string_debug_size(dir, dirlen);
|
||||
BLI_string_debug_size(file, filelen);
|
||||
|
||||
const size_t lslash = path_split_dir_file_offset(filepath);
|
||||
if (lslash) { /* +1 to include the slash and the last char. */
|
||||
BLI_strncpy(dir, filepath, MIN2(dirlen, lslash + 1));
|
||||
@@ -1496,9 +1494,7 @@ void BLI_path_split_dir_file(
|
||||
|
||||
void BLI_path_split_dir_part(const char *filepath, char *dir, const size_t dirlen)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dir, 0xff, sizeof(*dir) * dirlen);
|
||||
#endif
|
||||
BLI_string_debug_size(dir, dirlen);
|
||||
const size_t lslash = path_split_dir_file_offset(filepath);
|
||||
if (lslash) { /* +1 to include the slash and the last char. */
|
||||
BLI_strncpy(dir, filepath, MIN2(dirlen, lslash + 1));
|
||||
@@ -1510,9 +1506,7 @@ void BLI_path_split_dir_part(const char *filepath, char *dir, const size_t dirle
|
||||
|
||||
void BLI_path_split_file_part(const char *filepath, char *file, const size_t filelen)
|
||||
{
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(file, 0xff, sizeof(*file) * filelen);
|
||||
#endif
|
||||
BLI_string_debug_size(file, filelen);
|
||||
const size_t lslash = path_split_dir_file_offset(filepath);
|
||||
BLI_strncpy(file, filepath + lslash, filelen);
|
||||
}
|
||||
@@ -1562,8 +1556,9 @@ const char *BLI_path_extension(const char *filepath)
|
||||
|
||||
size_t BLI_path_append(char *__restrict dst, const size_t maxlen, const char *__restrict file)
|
||||
{
|
||||
size_t dirlen = BLI_strnlen(dst, maxlen);
|
||||
BLI_string_debug_size_after_nil(dst, maxlen);
|
||||
|
||||
size_t dirlen = BLI_strnlen(dst, maxlen);
|
||||
/* Inline #BLI_path_slash_ensure. */
|
||||
if ((dirlen > 0) && !BLI_path_slash_is_native_compat(dst[dirlen - 1])) {
|
||||
dst[dirlen++] = SEP;
|
||||
@@ -1596,9 +1591,8 @@ size_t BLI_path_join_array(char *__restrict dst,
|
||||
const int path_array_num)
|
||||
{
|
||||
BLI_assert(path_array_num > 0);
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * dst_len);
|
||||
#endif
|
||||
BLI_string_debug_size(dst, dst_len);
|
||||
|
||||
if (UNLIKELY(dst_len == 0)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -1845,6 +1839,8 @@ const char *BLI_path_slash_rfind(const char *string)
|
||||
|
||||
int BLI_path_slash_ensure(char *string, size_t string_maxlen)
|
||||
{
|
||||
BLI_string_debug_size_after_nil(string, string_maxlen);
|
||||
|
||||
int len = strlen(string);
|
||||
BLI_assert(len < string_maxlen);
|
||||
if (len == 0 || !BLI_path_slash_is_native_compat(string[len - 1])) {
|
||||
|
||||
@@ -24,8 +24,6 @@
|
||||
# pragma GCC diagnostic error "-Wsign-conversion"
|
||||
#endif
|
||||
|
||||
// #define DEBUG_STRSIZE
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name String Duplicate/Copy
|
||||
* \{ */
|
||||
@@ -63,12 +61,10 @@ char *BLI_strdupcat(const char *__restrict str1, const char *__restrict str2)
|
||||
|
||||
char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
|
||||
{
|
||||
size_t srclen = BLI_strnlen(src, maxncpy - 1);
|
||||
BLI_assert(maxncpy != 0);
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
||||
#endif
|
||||
BLI_assert(maxncpy != 0);
|
||||
size_t srclen = BLI_strnlen(src, maxncpy - 1);
|
||||
|
||||
memcpy(dst, src, srclen);
|
||||
dst[srclen] = '\0';
|
||||
@@ -80,12 +76,9 @@ char *BLI_strncpy_ensure_pad(char *__restrict dst,
|
||||
const char pad,
|
||||
size_t maxncpy)
|
||||
{
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
BLI_assert(maxncpy != 0);
|
||||
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
||||
#endif
|
||||
|
||||
if (src[0] == '\0') {
|
||||
dst[0] = '\0';
|
||||
}
|
||||
@@ -119,13 +112,11 @@ char *BLI_strncpy_ensure_pad(char *__restrict dst,
|
||||
|
||||
size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
|
||||
{
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
|
||||
size_t srclen = BLI_strnlen(src, maxncpy - 1);
|
||||
BLI_assert(maxncpy != 0);
|
||||
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
||||
#endif
|
||||
|
||||
memcpy(dst, src, srclen);
|
||||
dst[srclen] = '\0';
|
||||
return srclen;
|
||||
@@ -144,6 +135,8 @@ size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src)
|
||||
|
||||
char *BLI_strncat(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
|
||||
{
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
|
||||
size_t len = BLI_strnlen(dst, maxncpy);
|
||||
if (len < maxncpy) {
|
||||
BLI_strncpy(dst + len, src, maxncpy - len);
|
||||
@@ -162,6 +155,8 @@ size_t BLI_vsnprintf(char *__restrict buffer,
|
||||
const char *__restrict format,
|
||||
va_list arg)
|
||||
{
|
||||
BLI_string_debug_size(buffer, maxncpy);
|
||||
|
||||
size_t n;
|
||||
|
||||
BLI_assert(buffer != NULL);
|
||||
@@ -185,6 +180,8 @@ size_t BLI_vsnprintf_rlen(char *__restrict buffer,
|
||||
const char *__restrict format,
|
||||
va_list arg)
|
||||
{
|
||||
BLI_string_debug_size(buffer, maxncpy);
|
||||
|
||||
size_t n;
|
||||
|
||||
BLI_assert(buffer != NULL);
|
||||
@@ -206,13 +203,11 @@ size_t BLI_vsnprintf_rlen(char *__restrict buffer,
|
||||
|
||||
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...)
|
||||
{
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
|
||||
size_t n;
|
||||
va_list arg;
|
||||
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
||||
#endif
|
||||
|
||||
va_start(arg, format);
|
||||
n = BLI_vsnprintf(dst, maxncpy, format, arg);
|
||||
va_end(arg);
|
||||
@@ -222,13 +217,11 @@ size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict
|
||||
|
||||
size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...)
|
||||
{
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
|
||||
size_t n;
|
||||
va_list arg;
|
||||
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
||||
#endif
|
||||
|
||||
va_start(arg, format);
|
||||
n = BLI_vsnprintf_rlen(dst, maxncpy, format, arg);
|
||||
va_end(arg);
|
||||
@@ -273,8 +266,8 @@ int BLI_sprintf(char *__restrict str, const char *__restrict format, ...)
|
||||
|
||||
size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, const size_t dst_maxncpy)
|
||||
{
|
||||
|
||||
BLI_assert(dst_maxncpy != 0);
|
||||
BLI_string_debug_size(dst, dst_maxncpy);
|
||||
|
||||
size_t len = 0;
|
||||
for (; (len < dst_maxncpy) && (*src != '\0'); dst++, src++, len++) {
|
||||
@@ -329,6 +322,8 @@ size_t BLI_str_unescape_ex(char *__restrict dst,
|
||||
const size_t dst_maxncpy,
|
||||
bool *r_is_complete)
|
||||
{
|
||||
BLI_string_debug_size(dst, dst_maxncpy);
|
||||
|
||||
size_t len = 0;
|
||||
bool is_complete = true;
|
||||
const size_t max_strlen = dst_maxncpy - 1; /* Account for trailing zero byte. */
|
||||
@@ -350,6 +345,8 @@ size_t BLI_str_unescape_ex(char *__restrict dst,
|
||||
|
||||
size_t BLI_str_unescape(char *__restrict dst, const char *__restrict src, const size_t src_maxncpy)
|
||||
{
|
||||
BLI_string_debug_size(dst, src_maxncpy); /* `dst` must be at least as big as `src`. */
|
||||
|
||||
size_t len = 0;
|
||||
for (const char *src_end = src + src_maxncpy; (src < src_end) && *src; src++) {
|
||||
char c = *src;
|
||||
@@ -450,6 +447,8 @@ bool BLI_str_quoted_substr(const char *__restrict str,
|
||||
char *result,
|
||||
size_t result_maxlen)
|
||||
{
|
||||
BLI_string_debug_size(result, result_maxlen);
|
||||
|
||||
int start_match_ofs, end_match_ofs;
|
||||
if (!BLI_str_quoted_substr_range(str, prefix, &start_match_ofs, &end_match_ofs)) {
|
||||
return false;
|
||||
@@ -539,6 +538,8 @@ bool BLI_str_replace_table_exact(char *string,
|
||||
const char *replace_table[][2],
|
||||
int replace_table_len)
|
||||
{
|
||||
BLI_string_debug_size_after_nil(string, string_len);
|
||||
|
||||
for (int i = 0; i < replace_table_len; i++) {
|
||||
if (STREQ(string, replace_table[i][0])) {
|
||||
BLI_strncpy(string, replace_table[i][1], string_len);
|
||||
@@ -1137,6 +1138,8 @@ static size_t BLI_str_format_int_grouped_ex(char *src, char *dst, int num_len)
|
||||
|
||||
size_t BLI_str_format_int_grouped(char dst[BLI_STR_FORMAT_INT32_GROUPED_SIZE], int num)
|
||||
{
|
||||
BLI_string_debug_size(dst, BLI_STR_FORMAT_INT32_GROUPED_SIZE);
|
||||
|
||||
char src[BLI_STR_FORMAT_INT32_GROUPED_SIZE];
|
||||
const int num_len = BLI_snprintf(src, sizeof(src), "%d", num);
|
||||
|
||||
@@ -1145,6 +1148,8 @@ size_t BLI_str_format_int_grouped(char dst[BLI_STR_FORMAT_INT32_GROUPED_SIZE], i
|
||||
|
||||
size_t BLI_str_format_uint64_grouped(char dst[BLI_STR_FORMAT_UINT64_GROUPED_SIZE], uint64_t num)
|
||||
{
|
||||
BLI_string_debug_size(dst, BLI_STR_FORMAT_UINT64_GROUPED_SIZE);
|
||||
|
||||
char src[BLI_STR_FORMAT_UINT64_GROUPED_SIZE];
|
||||
const int num_len = BLI_snprintf(src, sizeof(src), "%" PRIu64 "", num);
|
||||
|
||||
@@ -1155,6 +1160,8 @@ void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE],
|
||||
long long int bytes,
|
||||
const bool base_10)
|
||||
{
|
||||
BLI_string_debug_size(dst, BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE);
|
||||
|
||||
double bytes_converted = bytes;
|
||||
int order = 0;
|
||||
int decimals;
|
||||
@@ -1182,6 +1189,8 @@ void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE],
|
||||
void BLI_str_format_decimal_unit(char dst[BLI_STR_FORMAT_INT32_DECIMAL_UNIT_SIZE],
|
||||
int number_to_format)
|
||||
{
|
||||
BLI_string_debug_size(dst, BLI_STR_FORMAT_INT32_DECIMAL_UNIT_SIZE);
|
||||
|
||||
float number_to_format_converted = number_to_format;
|
||||
int order = 0;
|
||||
const float base = 1000;
|
||||
@@ -1204,6 +1213,8 @@ void BLI_str_format_decimal_unit(char dst[BLI_STR_FORMAT_INT32_DECIMAL_UNIT_SIZE
|
||||
void BLI_str_format_integer_unit(char dst[BLI_STR_FORMAT_INT32_INTEGER_UNIT_SIZE],
|
||||
const int number_to_format)
|
||||
{
|
||||
BLI_string_debug_size(dst, BLI_STR_FORMAT_INT32_INTEGER_UNIT_SIZE);
|
||||
|
||||
float number_to_format_converted = number_to_format;
|
||||
int order = 0;
|
||||
const float base = 1000;
|
||||
@@ -1233,3 +1244,21 @@ void BLI_str_format_integer_unit(char dst[BLI_STR_FORMAT_INT32_INTEGER_UNIT_SIZE
|
||||
}
|
||||
|
||||
/** \} */
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name String Debugging
|
||||
* \{ */
|
||||
|
||||
#ifdef DEBUG_STRSIZE
|
||||
void BLI_string_debug_size_after_nil(char *str, size_t str_maxlen)
|
||||
{
|
||||
/* Step over the nil, into the character afterwards. */
|
||||
size_t str_tail = BLI_strnlen(str, str_maxlen) + 2;
|
||||
if (str_tail < str_maxlen) {
|
||||
BLI_string_debug_size(str + str_tail, str_maxlen - str_tail);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* DEBUG_STRSIZE */
|
||||
|
||||
/** \} */
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include "BLI_utildefines.h"
|
||||
|
||||
#include "BLI_string.h" /* #BLI_string_debug_size. */
|
||||
#include "BLI_string_utf8.h" /* own include */
|
||||
#ifdef WIN32
|
||||
# include "utfconv.h"
|
||||
@@ -25,8 +26,6 @@
|
||||
# pragma GCC diagnostic error "-Wsign-conversion"
|
||||
#endif
|
||||
|
||||
// #define DEBUG_STRSIZE
|
||||
|
||||
/**
|
||||
* Array copied from GLIB's `gutf8.c`.
|
||||
* \note last two values (0xfe and 0xff) are forbidden in UTF-8,
|
||||
@@ -235,14 +234,10 @@ int BLI_str_utf8_invalid_strip(char *str, size_t length)
|
||||
|
||||
char *BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t maxncpy)
|
||||
{
|
||||
char *r_dst = dst;
|
||||
|
||||
BLI_assert(maxncpy != 0);
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
||||
#endif
|
||||
|
||||
char *r_dst = dst;
|
||||
/* NOTE: currently we don't attempt to deal with invalid utf8 chars. */
|
||||
BLI_STR_UTF8_CPY(dst, src, maxncpy);
|
||||
|
||||
@@ -251,14 +246,10 @@ char *BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t
|
||||
|
||||
size_t BLI_strncpy_utf8_rlen(char *__restrict dst, const char *__restrict src, size_t maxncpy)
|
||||
{
|
||||
char *r_dst = dst;
|
||||
|
||||
BLI_assert(maxncpy != 0);
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
||||
#endif
|
||||
|
||||
char *r_dst = dst;
|
||||
/* NOTE: currently we don't attempt to deal with invalid utf8 chars. */
|
||||
BLI_STR_UTF8_CPY(dst, src, maxncpy);
|
||||
|
||||
@@ -275,10 +266,9 @@ size_t BLI_strncpy_wchar_as_utf8(char *__restrict dst,
|
||||
const size_t maxncpy)
|
||||
{
|
||||
BLI_assert(maxncpy != 0);
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
|
||||
size_t len = 0;
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
||||
#endif
|
||||
while (*src && len < maxncpy) {
|
||||
len += BLI_str_utf8_from_unicode((uint)*src++, dst + len, maxncpy - len);
|
||||
}
|
||||
@@ -350,6 +340,7 @@ size_t BLI_strncpy_wchar_from_utf8(wchar_t *__restrict dst_w,
|
||||
const size_t maxncpy)
|
||||
{
|
||||
#ifdef WIN32
|
||||
BLI_string_debug_size(dst_w, maxncpy);
|
||||
conv_utf_8_to_16(src_c, dst_w, maxncpy);
|
||||
/* NOTE: it would be more efficient to calculate the length as part of #conv_utf_8_to_16. */
|
||||
return wcslen(dst_w);
|
||||
@@ -802,6 +793,8 @@ size_t BLI_str_utf8_from_unicode_len(const uint c)
|
||||
size_t BLI_str_utf8_from_unicode(uint c, char *outbuf, const size_t outbuf_len)
|
||||
|
||||
{
|
||||
BLI_string_debug_size(outbuf, outbuf_len);
|
||||
|
||||
/* If this gets modified, also update the copy in g_string_insert_unichar() */
|
||||
uint len = 0;
|
||||
uint first;
|
||||
@@ -827,15 +820,12 @@ size_t BLI_str_utf8_as_utf32(char32_t *__restrict dst_w,
|
||||
const char *__restrict src_c,
|
||||
const size_t maxncpy)
|
||||
{
|
||||
BLI_assert(maxncpy != 0);
|
||||
BLI_string_debug_size(dst_w, maxncpy);
|
||||
|
||||
const size_t maxlen = maxncpy - 1;
|
||||
size_t len = 0;
|
||||
|
||||
BLI_assert(maxncpy != 0);
|
||||
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst_w, 0xff, sizeof(*dst_w) * maxncpy);
|
||||
#endif
|
||||
|
||||
const size_t src_c_len = strlen(src_c);
|
||||
const char *src_c_end = src_c + src_c_len;
|
||||
size_t index = 0;
|
||||
@@ -863,10 +853,9 @@ size_t BLI_str_utf32_as_utf8(char *__restrict dst,
|
||||
const size_t maxncpy)
|
||||
{
|
||||
BLI_assert(maxncpy != 0);
|
||||
BLI_string_debug_size(dst, maxncpy);
|
||||
|
||||
size_t len = 0;
|
||||
#ifdef DEBUG_STRSIZE
|
||||
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
||||
#endif
|
||||
while (*src && len < maxncpy) {
|
||||
len += BLI_str_utf8_from_unicode((uint)*src++, dst + len, maxncpy - len);
|
||||
}
|
||||
|
||||
@@ -80,6 +80,9 @@ void BLI_string_split_suffix(const char *string,
|
||||
char *r_body,
|
||||
char *r_suf)
|
||||
{
|
||||
BLI_string_debug_size(r_body, string_maxlen);
|
||||
BLI_string_debug_size(r_suf, string_maxlen);
|
||||
|
||||
size_t len = BLI_strnlen(string, string_maxlen);
|
||||
size_t i;
|
||||
|
||||
@@ -101,6 +104,9 @@ void BLI_string_split_prefix(const char *string,
|
||||
char *r_pre,
|
||||
char *r_body)
|
||||
{
|
||||
BLI_string_debug_size(r_pre, string_maxlen);
|
||||
BLI_string_debug_size(r_body, string_maxlen);
|
||||
|
||||
size_t len = BLI_strnlen(string, string_maxlen);
|
||||
size_t i;
|
||||
|
||||
@@ -123,6 +129,8 @@ size_t BLI_string_flip_side_name(char *r_name,
|
||||
const bool strip_number,
|
||||
const size_t name_len)
|
||||
{
|
||||
BLI_string_debug_size(r_name, name_len);
|
||||
|
||||
size_t len;
|
||||
char *prefix = alloca(name_len); /* The part before the facing */
|
||||
char *suffix = alloca(name_len); /* The part after the facing */
|
||||
@@ -246,6 +254,8 @@ bool BLI_uniquename_cb(UniquenameCheckCallback unique_check,
|
||||
char *name,
|
||||
size_t name_len)
|
||||
{
|
||||
BLI_string_debug_size_after_nil(name, name_len);
|
||||
|
||||
if (name[0] == '\0') {
|
||||
BLI_strncpy(name, defname, name_len);
|
||||
}
|
||||
@@ -373,6 +383,8 @@ size_t BLI_string_join_array(char *result,
|
||||
size_t BLI_string_join_array_by_sep_char(
|
||||
char *result, size_t result_len, char sep, const char *strings[], uint strings_len)
|
||||
{
|
||||
BLI_string_debug_size(result, result_len);
|
||||
|
||||
char *c = result;
|
||||
char *c_end = &result[result_len - 1];
|
||||
for (uint i = 0; i < strings_len; i++) {
|
||||
|
||||
Reference in New Issue
Block a user