чување cf7013a777526e312c3484d12f3d5452ebfe1ef8
родитељ 1777bf0e2de396216f7ccf68fedb4928db004015
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Wed, 23 Feb 2022 15:04:04 +0100
sled.c(create_file,insert_line): Fix buffer initialization
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | sled.c | | | 46 | +++++++++++++++++----------------------------- |
измењених датотека: 1, додавања: 17(+), брисања: 29(-)
diff --git a/sled.c b/sled.c
@@ -208,12 +208,13 @@ create_file(struct DrawState* state)
if (!newchunk)
return LOAD_ERR_CANT_ALLOC;
for (size_t row = 0; row < ALLOC_DELTA; row++)
+ {
init_bufferline(newchunk + row);
- newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
- if (!newtext)
- return LOAD_ERR_CANT_ALLOC;
- newchunk->length = 0;
- newchunk->text = newtext;
+ newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
+ if (!newtext)
+ return LOAD_ERR_CANT_ALLOC;
+ newchunk[row].text = newtext;
+ }
state->buffer = newchunk;
state->buffer_size = ALLOC_DELTA;
state->rows_count = 1;
@@ -292,19 +293,7 @@ load_file(struct DrawState* state, long* lineno, long* col)
*state->action = 0;
if (state->rows_count == 0)
- {
- line = state->buffer;
- if (!line->text)
- {
- uint32_t* newtext = NULL;
- newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
- if (!newtext)
- return LOAD_ERR_CANT_ALLOC;
- line->length = 0;
- line->text = newtext;
- }
state->rows_count++;
- }
return LOAD_ERR_NONE;
}
@@ -1233,8 +1222,7 @@ insert_line(struct DrawState* state)
struct BufferLine* line = NULL;
uint32_t* newtext = NULL;
- line = state->buffer + state->rows_count;
- if (line == state->buffer + state->buffer_size)
+ if (state->rows_count == state->buffer_size)
{
state->buffer_size += ALLOC_DELTA;
newchunk = realloc(state->buffer,
@@ -1247,18 +1235,18 @@ insert_line(struct DrawState* state)
state->buffer = newchunk;
for (size_t row = state->buffer_size - ALLOC_DELTA;
row < state->buffer_size; row++)
- init_bufferline(line);
- line = state->buffer + state->buffer_size - ALLOC_DELTA;
- }
- if (!line->text)
- {
- newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
- if (!newtext)
{
- strcpy(state->error, errors[ERR_CANT_ALLOC]);
- return 0;
+ line = state->buffer + row;
+ init_bufferline(line);
+ newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
+ if (!newtext)
+ {
+ strcpy(state->error, errors[ERR_CANT_ALLOC]);
+ return 0;
+ }
+ line->text = newtext;
}
- line->text = newtext;
+ line = state->buffer + state->rows_count;
}
state->rows_count++;
for (size_t i = state->rows_count - 1; i > state->row + 1; i--)