Cleanup: use size_t for BLF text API functions

Also minor cleanup to txt_sel_to_buf:

- Use memcpy instead of strncpy as the strings don't contain nil bytes.
- Replace while loops with for loops.
This commit is contained in:
Campbell Barton 2022-03-14 14:25:33 +11:00
parent a5571fd0e8
commit 541ba68991
Notes: blender-bot 2023-02-13 22:20:49 +01:00
Referenced by commit 8ecaa2d624, Fix T96510: ASAN failure when selecting text in Text Editor
Referenced by issue #96510, ASAN failure when selecting text in Text Editor
5 changed files with 25 additions and 33 deletions

View File

@ -57,7 +57,7 @@ void BKE_text_write(struct Text *text, const char *str);
int BKE_text_file_modified_check(struct Text *text);
void BKE_text_file_modified_ignore(struct Text *text);
char *txt_to_buf(struct Text *text, int *r_buf_strlen)
char *txt_to_buf(struct Text *text, size_t *r_buf_strlen)
ATTR_NONNULL(1, 2) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL;
void txt_clean_text(struct Text *text);
void txt_order_cursors(struct Text *text, bool reverse);
@ -92,7 +92,7 @@ void txt_sel_all(struct Text *text);
void txt_sel_clear(struct Text *text);
void txt_sel_line(struct Text *text);
void txt_sel_set(struct Text *text, int startl, int startc, int endl, int endc);
char *txt_sel_to_buf(struct Text *text, int *r_buf_strlen);
char *txt_sel_to_buf(struct Text *text, size_t *r_buf_strlen);
void txt_insert_buf(struct Text *text, const char *in_buffer);
void txt_split_curline(struct Text *text);
void txt_backspace_char(struct Text *text);
@ -138,12 +138,12 @@ enum {
/**
* Create a buffer, the only requirement is #txt_from_buf_for_undo can decode it.
*/
char *txt_to_buf_for_undo(struct Text *text, int *r_buf_len)
char *txt_to_buf_for_undo(struct Text *text, size_t *r_buf_len)
ATTR_NONNULL(1, 2) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL;
/**
* Decode a buffer from #txt_to_buf_for_undo.
*/
void txt_from_buf_for_undo(struct Text *text, const char *buf, int buf_len) ATTR_NONNULL(1, 2);
void txt_from_buf_for_undo(struct Text *text, const char *buf, size_t buf_len) ATTR_NONNULL(1, 2);
#ifdef __cplusplus
}

View File

@ -1349,7 +1349,7 @@ void txt_sel_set(Text *text, int startl, int startc, int endl, int endc)
* - Are not null terminated.
* \{ */
char *txt_to_buf_for_undo(Text *text, int *r_buf_len)
char *txt_to_buf_for_undo(Text *text, size_t *r_buf_len)
{
int buf_len = 0;
LISTBASE_FOREACH (const TextLine *, l, &text->lines) {
@ -1366,7 +1366,7 @@ char *txt_to_buf_for_undo(Text *text, int *r_buf_len)
return buf;
}
void txt_from_buf_for_undo(Text *text, const char *buf, int buf_len)
void txt_from_buf_for_undo(Text *text, const char *buf, size_t buf_len)
{
const char *buf_end = buf + buf_len;
const char *buf_step = buf;
@ -1434,10 +1434,10 @@ void txt_from_buf_for_undo(Text *text, const char *buf, int buf_len)
/** \name Cut and Paste Functions
* \{ */
char *txt_to_buf(Text *text, int *r_buf_strlen)
char *txt_to_buf(Text *text, size_t *r_buf_strlen)
{
/* Identical to #txt_to_buf_for_undo except that the string is nil terminated. */
int buf_len = 0;
size_t buf_len = 0;
LISTBASE_FOREACH (const TextLine *, l, &text->lines) {
buf_len += l->len + 1;
}
@ -1453,10 +1453,10 @@ char *txt_to_buf(Text *text, int *r_buf_strlen)
return buf;
}
char *txt_sel_to_buf(Text *text, int *r_buf_strlen)
char *txt_sel_to_buf(Text *text, size_t *r_buf_strlen)
{
char *buf;
int length = 0;
size_t length = 0;
TextLine *tmp, *linef, *linel;
int charf, charl;
@ -1500,42 +1500,32 @@ char *txt_sel_to_buf(Text *text, int *r_buf_strlen)
if (linef == linel) {
length = charl - charf;
buf = MEM_mallocN(length + 1, "sel buffer");
BLI_strncpy(buf, linef->line + charf, length + 1);
memcpy(buf, linef->line + charf, length + 1);
}
else {
length += linef->len - charf;
length += charl;
length++; /* For the '\n' */
/* Add 1 for the '\n' */
length = (linef->len - charf) + charl + 1;
tmp = linef->next;
while (tmp && tmp != linel) {
for (tmp = linef->next; tmp && tmp != linel; tmp = tmp->next) {
length += tmp->len + 1;
tmp = tmp->next;
}
buf = MEM_mallocN(length + 1, "sel buffer");
strncpy(buf, linef->line + charf, linef->len - charf);
memcpy(buf, linef->line + charf, linef->len - charf);
length = linef->len - charf;
buf[length++] = '\n';
tmp = linef->next;
while (tmp && tmp != linel) {
strncpy(buf + length, tmp->line, tmp->len);
for (tmp = linef->next; tmp && tmp != linel; tmp = tmp->next) {
memcpy(buf + length, tmp->line, tmp->len);
length += tmp->len;
buf[length++] = '\n';
tmp = tmp->next;
}
strncpy(buf + length, linel->line, charl);
length += charl;
buf[length] = 0;
memcpy(buf + length, linel->line, charl);
length += charl;
buf[length] = '\0';
}
if (r_buf_strlen) {

View File

@ -59,7 +59,7 @@ typedef struct TextState {
static void text_state_encode(TextState *state, Text *text, BArrayStore *buffer_store)
{
int buf_len = 0;
size_t buf_len = 0;
uchar *buf = (uchar *)txt_to_buf_for_undo(text, &buf_len);
state->buf_array_state = BLI_array_store_state_add(buffer_store, buf, buf_len, NULL);
MEM_freeN(buf);

View File

@ -40,7 +40,9 @@ static void rna_Text_from_string(Text *text, const char *str)
static void rna_Text_as_string(Text *text, int *r_result_len, const char **result)
{
*result = txt_to_buf(text, r_result_len);
size_t result_len;
*result = txt_to_buf(text, &result_len);
*r_result_len = result_len;
}
static void rna_Text_select_set(Text *text, int startl, int startc, int endl, int endc)

View File

@ -102,7 +102,7 @@ static bool python_script_exec(
fn_dummy_py = PyC_UnicodeFromByte(fn_dummy);
int buf_len_dummy;
size_t buf_len_dummy;
buf = txt_to_buf(text, &buf_len_dummy);
text->compiled = Py_CompileStringObject(buf, fn_dummy_py, Py_file_input, NULL, -1);
MEM_freeN(buf);