From e2f4c4db8d6cbe4694c24d599e16ee3889871bdd Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 7 Apr 2022 15:43:23 +1000 Subject: Cleanup: pass the buffer length into `txt_insert_buf` Also remove redundant NULL check. --- source/blender/blenkernel/BKE_text.h | 7 +++--- source/blender/blenkernel/intern/text.c | 29 ++++++++++------------ .../blender/editors/space_text/text_autocomplete.c | 3 ++- source/blender/editors/space_text/text_ops.c | 6 ++--- source/blender/makesrna/intern/rna_text_api.c | 4 +-- source/blender/python/intern/bpy_rna_text.c | 15 ++++++++--- 6 files changed, 36 insertions(+), 28 deletions(-) (limited to 'source/blender') diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index a5b71d42cdc..b05abb5a73c 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -45,8 +45,8 @@ struct Text *BKE_text_load_ex(struct Main *bmain, * \note Text data-blocks have no user by default, only the 'real user' flag. */ struct Text *BKE_text_load(struct Main *bmain, const char *file, const char *relpath); -void BKE_text_clear(struct Text *text); -void BKE_text_write(struct Text *text, const char *str); +void BKE_text_clear(struct Text *text) ATTR_NONNULL(1); +void BKE_text_write(struct Text *text, const char *str, int str_len) ATTR_NONNULL(1, 2); /** * \return codes: * - 0 if file on disk is the same or Text is in memory only. @@ -93,7 +93,8 @@ 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, size_t *r_buf_strlen); -void txt_insert_buf(struct Text *text, const char *in_buffer); +void txt_insert_buf(struct Text *text, const char *in_buffer, int in_buffer_len) + ATTR_NONNULL(1, 2); void txt_split_curline(struct Text *text); void txt_backspace_char(struct Text *text); void txt_backspace_word(struct Text *text); diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index 8759d7a0e5f..9d8a97c90b8 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -518,9 +518,9 @@ void BKE_text_clear(Text *text) /* called directly from rna */ txt_make_dirty(text); } -void BKE_text_write(Text *text, const char *str) /* called directly from rna */ +void BKE_text_write(Text *text, const char *str, int str_len) /* called directly from rna */ { - txt_insert_buf(text, str); + txt_insert_buf(text, str, str_len); txt_move_eof(text, 0); txt_make_dirty(text); } @@ -1536,33 +1536,30 @@ char *txt_sel_to_buf(Text *text, size_t *r_buf_strlen) return buf; } -void txt_insert_buf(Text *text, const char *in_buffer) +void txt_insert_buf(Text *text, const char *in_buffer, int in_buffer_len) { - int l = 0, len; + BLI_assert(in_buffer_len == strlen(in_buffer)); + + int l = 0; size_t i = 0, j; TextLine *add; char *buffer; - if (!in_buffer) { - return; - } - txt_delete_sel(text); - len = strlen(in_buffer); - buffer = BLI_strdupn(in_buffer, len); - len += txt_extended_ascii_as_utf8(&buffer); + buffer = BLI_strdupn(in_buffer, in_buffer_len); + in_buffer_len += txt_extended_ascii_as_utf8(&buffer); /* Read the first line (or as close as possible */ while (buffer[i] && buffer[i] != '\n') { - txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, len, &i)); + txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, in_buffer_len, &i)); } if (buffer[i] == '\n') { txt_split_curline(text); i++; - while (i < len) { + while (i < in_buffer_len) { l = 0; while (buffer[i] && buffer[i] != '\n') { @@ -1576,8 +1573,8 @@ void txt_insert_buf(Text *text, const char *in_buffer) i++; } else { - for (j = i - l; j < i && j < len;) { - txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, len, &j)); + for (j = i - l; j < i && j < in_buffer_len;) { + txt_add_raw_char(text, BLI_str_utf8_as_unicode_step(buffer, in_buffer_len, &j)); } break; } @@ -1875,7 +1872,7 @@ static void txt_convert_tab_to_spaces(Text *text) * to multiples of TXT_TABSIZE) */ const char *sb = &tab_to_spaces[text->curc % TXT_TABSIZE]; - txt_insert_buf(text, sb); + txt_insert_buf(text, sb, strlen(sb)); } static bool txt_add_char_intern(Text *text, unsigned int add, bool replace_tabs) diff --git a/source/blender/editors/space_text/text_autocomplete.c b/source/blender/editors/space_text/text_autocomplete.c index 55873740491..54735a4d481 100644 --- a/source/blender/editors/space_text/text_autocomplete.c +++ b/source/blender/editors/space_text/text_autocomplete.c @@ -267,7 +267,8 @@ static void confirm_suggestion(Text *text) // for (i = 0; i < skipleft; i++) // txt_move_left(text, 0); BLI_assert(memcmp(sel->name, &line[i], over) == 0); - txt_insert_buf(text, sel->name + over); + const char *buf = sel->name + over; + txt_insert_buf(text, buf, strlen(buf)); // for (i = 0; i < skipleft; i++) // txt_move_right(text, 0); diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index d4aac8dd57f..3f1483bbd03 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -921,7 +921,7 @@ static int text_paste_exec(bContext *C, wmOperator *op) buf = new_buf; } - txt_insert_buf(text, buf); + txt_insert_buf(text, buf, buf_len); text_update_edited(text); MEM_freeN(buf); @@ -3587,7 +3587,7 @@ static int text_find_and_replace(bContext *C, wmOperator *op, short mode) if (found) { if (mode == TEXT_REPLACE) { ED_text_undo_push_init(C); - txt_insert_buf(text, st->replacestr); + txt_insert_buf(text, st->replacestr, strlen(st->replacestr)); if (text->curl && text->curl->format) { MEM_freeN(text->curl->format); text->curl->format = NULL; @@ -3671,7 +3671,7 @@ static int text_replace_all(bContext *C) ED_text_undo_push_init(C); do { - txt_insert_buf(text, st->replacestr); + txt_insert_buf(text, st->replacestr, strlen(st->replacestr)); if (text->curl && text->curl->format) { MEM_freeN(text->curl->format); text->curl->format = NULL; diff --git a/source/blender/makesrna/intern/rna_text_api.c b/source/blender/makesrna/intern/rna_text_api.c index a5eadd1f36c..8377fbc7418 100644 --- a/source/blender/makesrna/intern/rna_text_api.c +++ b/source/blender/makesrna/intern/rna_text_api.c @@ -28,14 +28,14 @@ static void rna_Text_clear(Text *text) static void rna_Text_write(Text *text, const char *str) { - BKE_text_write(text, str); + BKE_text_write(text, str, strlen(str)); WM_main_add_notifier(NC_TEXT | NA_EDITED, text); } static void rna_Text_from_string(Text *text, const char *str) { BKE_text_clear(text); - BKE_text_write(text, str); + BKE_text_write(text, str, strlen(str)); } static void rna_Text_as_string(Text *text, int *r_result_len, const char **result) diff --git a/source/blender/python/intern/bpy_rna_text.c b/source/blender/python/intern/bpy_rna_text.c index 44568ad30a6..fedb914256a 100644 --- a/source/blender/python/intern/bpy_rna_text.c +++ b/source/blender/python/intern/bpy_rna_text.c @@ -6,6 +6,8 @@ * This file extends the text editor with C/Python API methods and attributes. */ +#define PY_SSIZE_T_CLEAN + #include #include "DNA_text_types.h" @@ -103,9 +105,16 @@ static PyObject *bpy_rna_region_from_string(PyObject *self, PyObject *args) /* Parse the region range. */ const char *buf; + Py_ssize_t buf_len; TextRegion region; - if (!PyArg_ParseTuple( - args, "s|((ii)(ii))", &buf, ®ion.curl, ®ion.curc, ®ion.sell, ®ion.selc)) { + if (!PyArg_ParseTuple(args, + "s#|((ii)(ii))", + &buf, + &buf_len, + ®ion.curl, + ®ion.curc, + ®ion.sell, + ®ion.selc)) { return NULL; } @@ -114,7 +123,7 @@ static PyObject *bpy_rna_region_from_string(PyObject *self, PyObject *args) } /* Set the selected text. */ - txt_insert_buf(text, buf); + txt_insert_buf(text, buf, buf_len); /* Update the text editor. */ WM_main_add_notifier(NC_TEXT | NA_EDITED, text); -- cgit v1.2.3