Cleanup/fix some BLI_string_utf8 not using size_t/off_t as expected.

This commit is contained in:
Bastien Montagne 2017-01-20 13:03:21 +01:00
parent 2666a222f6
commit 051526da62
5 changed files with 23 additions and 15 deletions

View File

@ -1408,7 +1408,8 @@ static ID *is_dupid(ListBase *lb, ID *id, const char *name)
static bool check_for_dupid(ListBase *lb, ID *id, char *name)
{
ID *idtest;
int nr = 0, a, left_len;
int nr = 0, a;
size_t left_len;
#define MAX_IN_USE 64
bool in_use[MAX_IN_USE];
/* to speed up finding unused numbers within [1 .. MAX_IN_USE - 1] */
@ -1442,7 +1443,7 @@ static bool check_for_dupid(ListBase *lb, ID *id, char *name)
/* Code above may have generated invalid utf-8 string, due to raw truncation.
* Ensure we get a valid one now! */
left_len -= BLI_utf8_invalid_strip(left, left_len);
left_len -= (size_t)BLI_utf8_invalid_strip(left, left_len);
for (idtest = lb->first; idtest; idtest = idtest->next) {
int nrtest;
@ -1484,7 +1485,7 @@ static bool check_for_dupid(ListBase *lb, ID *id, char *name)
* shave off the end chars until we have a unique name.
* Check the null terminators match as well so we don't get Cube.000 -> Cube.00 */
if (nr == 0 && name[left_len] == '\0') {
int len;
size_t len;
/* FIXME: this code will never be executed, because either nr will be
* at least 1, or name will not end at left_len! */
BLI_assert(0);

View File

@ -235,8 +235,9 @@ Text *BKE_text_add(Main *bmain, const char *name)
/* to a valid utf-8 sequences */
int txt_extended_ascii_as_utf8(char **str)
{
int bad_char, added = 0, i = 0;
int length = strlen(*str);
size_t bad_char, i = 0;
const size_t length = strlen(*str);
int added = 0;
while ((*str)[i]) {
if ((bad_char = BLI_utf8_invalid_byte(*str + i, length - i)) == -1)
@ -248,7 +249,7 @@ int txt_extended_ascii_as_utf8(char **str)
if (added != 0) {
char *newstr = MEM_mallocN(length + added + 1, "text_line");
int mi = 0;
size_t mi = 0;
i = 0;
while ((*str)[i]) {

View File

@ -36,8 +36,8 @@ extern "C" {
char *BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL();
size_t BLI_strncpy_utf8_rlen(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL();
char *BLI_strncat_utf8(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL();
int BLI_utf8_invalid_byte(const char *str, int length) ATTR_NONNULL();
int BLI_utf8_invalid_strip(char *str, int length) ATTR_NONNULL();
off_t BLI_utf8_invalid_byte(const char *str, size_t length) ATTR_NONNULL();
int BLI_utf8_invalid_strip(char *str, size_t length) ATTR_NONNULL();
int BLI_str_utf8_size(const char *p) ATTR_NONNULL(); /* warning, can return -1 on bad chars */
int BLI_str_utf8_size_safe(const char *p) ATTR_NONNULL();

View File

@ -74,7 +74,7 @@ static const size_t utf8_skip_data[256] = {
*
* \return the offset of the first invalid byte.
*/
int BLI_utf8_invalid_byte(const char *str, int length)
off_t BLI_utf8_invalid_byte(const char *str, size_t length)
{
const unsigned char *p, *perr, *pend = (const unsigned char *)str + length;
unsigned char c;
@ -161,18 +161,24 @@ int BLI_utf8_invalid_byte(const char *str, int length)
utf8_error:
return (int)((const char *)perr - (const char *)str);
return ((const char *)perr - (const char *)str);
}
int BLI_utf8_invalid_strip(char *str, int length)
/**
* Remove any invalid utf-8 byte (taking into account multi-bytes sequence of course).
*
* @return number of stripped bytes.
*/
int BLI_utf8_invalid_strip(char *str, size_t length)
{
int bad_char, tot = 0;
off_t bad_char;
int tot = 0;
BLI_assert(str[length] == '\0');
while ((bad_char = BLI_utf8_invalid_byte(str, length)) != -1) {
str += bad_char;
length -= (bad_char + 1);
length -= (size_t)(bad_char + 1);
if (length == 0) {
/* last character bad, strip it */
@ -182,7 +188,7 @@ int BLI_utf8_invalid_strip(char *str, int length)
}
else {
/* strip, keep looking */
memmove(str, str + 1, (size_t)length + 1); /* +1 for NULL char! */
memmove(str, str + 1, length + 1); /* +1 for NULL char! */
tot++;
}
}

View File

@ -2963,7 +2963,7 @@ static bool ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, const in
if (pbuf) {
if (ui_but_is_utf8(but)) {
buf_len -= BLI_utf8_invalid_strip(pbuf, buf_len);
buf_len -= BLI_utf8_invalid_strip(pbuf, (size_t)buf_len);
}
ui_textedit_insert_buf(but, data, pbuf, buf_len);