summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-20 12:27:59 -0500
committerpommicket <pommicket@gmail.com>2022-12-20 12:27:59 -0500
commit927f3eba4140be1526d51af9ecfc88b72b57dee2 (patch)
treeff87b432950dfbd8c4c72f634106a8095dc2b526 /buffer.c
parentfd40216803159858d03289e4c9bf37ec1f8332e7 (diff)
fix potential memory bug
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c6
1 files changed, 3 insertions, 3 deletions
diff --git a/buffer.c b/buffer.c
index 6e7ba7e..bf8047c 100644
--- a/buffer.c
+++ b/buffer.c
@@ -1388,7 +1388,8 @@ BufferPos buffer_insert_text_at_pos(TextBuffer *buffer, BufferPos pos, String32
// create a copy of str. we need to do this to remove carriage returns and newlines in the case of line buffers
char32_t str_copy[256];
- if (str.len > arr_count(str_copy)) {
+ bool str_is_allocated = str.len > arr_count(str_copy);
+ if (str_is_allocated) {
char32_t *new_str = buffer_calloc(buffer, str.len, sizeof *new_str);
memcpy(new_str, str.str, str.len * sizeof *str.str);
str.str = new_str;
@@ -1484,9 +1485,8 @@ BufferPos buffer_insert_text_at_pos(TextBuffer *buffer, BufferPos pos, String32
buffer_lines_modified(buffer, pos.line, line_idx);
BufferPos b = {.line = line_idx, .index = index};
- if (str.len > arr_count(str_copy)) {
+ if (str_is_allocated)
free(str.str);
- }
return b;
}