fix [#28658] python can assign non utf8 and crash because of string lenth limits.
add BLI_strncpy_utf8() which which ensures there are no partially copied UTF8 characters, limited by the buffer size.
This commit is contained in:
@@ -144,6 +144,7 @@ void BLI_ascii_strtoupper(char *str, int len);
|
||||
|
||||
|
||||
/* string_utf8.c - may move these into their own header some day - campbell */
|
||||
char *BLI_strncpy_utf8(char *dst, const char *src, size_t maxncpy);
|
||||
int BLI_utf8_invalid_byte(const char *str, int length);
|
||||
int BLI_utf8_invalid_strip(char *str, int length);
|
||||
|
||||
|
@@ -141,3 +141,43 @@ int BLI_utf8_invalid_strip(char *str, int length)
|
||||
|
||||
return tot;
|
||||
}
|
||||
|
||||
|
||||
/* compatible with BLI_strncpy, but esnure no partial utf8 chars */
|
||||
|
||||
/* array copied from glib's glib's gutf8.c,
|
||||
* note: this looks to be at odd's with 'trailingBytesForUTF8',
|
||||
* need to find out what gives here! - campbell */
|
||||
static const size_t utf8_skip_data[256] = {
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
||||
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
|
||||
};
|
||||
|
||||
char *BLI_strncpy_utf8(char *dst, const char *src, size_t maxncpy)
|
||||
{
|
||||
char *dst_r= dst;
|
||||
size_t utf8_size;
|
||||
|
||||
/* note: currently we dont attempt to deal with invalid utf8 chars */
|
||||
|
||||
while(*src != '\0' && (utf8_size= utf8_skip_data[*src]) < maxncpy) {
|
||||
maxncpy -= utf8_size;
|
||||
switch(utf8_size) {
|
||||
case 6: *dst ++ = *src ++;
|
||||
case 5: *dst ++ = *src ++;
|
||||
case 4: *dst ++ = *src ++;
|
||||
case 3: *dst ++ = *src ++;
|
||||
case 2: *dst ++ = *src ++;
|
||||
case 1: *dst ++ = *src ++;
|
||||
}
|
||||
}
|
||||
*dst= '\0';
|
||||
return dst_r;
|
||||
}
|
||||
|
||||
|
@@ -522,11 +522,14 @@ static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *pr
|
||||
fprintf(f, " %s(ptr, value);\n", manualfunc);
|
||||
}
|
||||
else {
|
||||
const PropertySubType subtype= prop->subtype;
|
||||
const char *string_copy_func= (subtype==PROP_FILEPATH || subtype==PROP_DIRPATH || subtype==PROP_FILENAME) ? "BLI_strncpy" : "BLI_strncpy_utf8";
|
||||
|
||||
rna_print_data_get(f, dp);
|
||||
if(sprop->maxlength)
|
||||
fprintf(f, " BLI_strncpy(value, data->%s, %d);\n", dp->dnaname, sprop->maxlength);
|
||||
fprintf(f, " %s(value, data->%s, %d);\n", string_copy_func, dp->dnaname, sprop->maxlength);
|
||||
else
|
||||
fprintf(f, " BLI_strncpy(value, data->%s, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
|
||||
fprintf(f, " %s(value, data->%s, sizeof(data->%s));\n", string_copy_func, dp->dnaname, dp->dnaname);
|
||||
}
|
||||
fprintf(f, "}\n\n");
|
||||
break;
|
||||
@@ -734,11 +737,14 @@ static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *pr
|
||||
fprintf(f, " %s(ptr, value);\n", manualfunc);
|
||||
}
|
||||
else {
|
||||
const PropertySubType subtype= prop->subtype;
|
||||
const char *string_copy_func= (subtype==PROP_FILEPATH || subtype==PROP_DIRPATH || subtype==PROP_FILENAME) ? "BLI_strncpy" : "BLI_strncpy_utf8";
|
||||
|
||||
rna_print_data_get(f, dp);
|
||||
if(sprop->maxlength)
|
||||
fprintf(f, " BLI_strncpy(data->%s, value, %d);\n", dp->dnaname, sprop->maxlength);
|
||||
fprintf(f, " %s(data->%s, value, %d);\n", string_copy_func, dp->dnaname, sprop->maxlength);
|
||||
else
|
||||
fprintf(f, " BLI_strncpy(data->%s, value, sizeof(data->%s));\n", dp->dnaname, dp->dnaname);
|
||||
fprintf(f, " %s(data->%s, value, sizeof(data->%s));\n", string_copy_func, dp->dnaname, dp->dnaname);
|
||||
}
|
||||
fprintf(f, "}\n\n");
|
||||
break;
|
||||
|
@@ -99,7 +99,7 @@ int rna_ID_name_length(PointerRNA *ptr)
|
||||
void rna_ID_name_set(PointerRNA *ptr, const char *value)
|
||||
{
|
||||
ID *id= (ID*)ptr->data;
|
||||
BLI_strncpy(id->name+2, value, sizeof(id->name)-2);
|
||||
BLI_strncpy_utf8(id->name+2, value, sizeof(id->name)-2);
|
||||
test_idbutton(id->name+2);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user