diff options
| author | Albert Cervin <albert@acervin.com> | 2024-01-25 10:25:21 +0100 |
|---|---|---|
| committer | Albert Cervin <albert@acervin.com> | 2024-01-25 10:25:21 +0100 |
| commit | 9433096a73d6af7cac7b05b087a740b2d070f463 (patch) | |
| tree | 49a314125d4b70453f2acd0b2d5efb0b38f3c7ed /src | |
| parent | b924543c0edac842c86661c3c05ab6b023cd9bdf (diff) | |
| download | dged-9433096a73d6af7cac7b05b087a740b2d070f463.tar.gz dged-9433096a73d6af7cac7b05b087a740b2d070f463.tar.xz dged-9433096a73d6af7cac7b05b087a740b2d070f463.zip | |
Fix save empty buffer crash
It was assuming it could access the "last line".
Diffstat (limited to 'src')
| -rw-r--r-- | src/dged/buffer.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/src/dged/buffer.c b/src/dged/buffer.c index 8b1f69c..78b89c8 100644 --- a/src/dged/buffer.c +++ b/src/dged/buffer.c @@ -366,10 +366,13 @@ void buffer_to_file(struct buffer *buffer) { } uint32_t nlines = text_num_lines(buffer->text); - struct text_chunk lastline = text_get_line(buffer->text, nlines - 1); - uint32_t nlines_to_write = lastline.nbytes == 0 ? nlines - 1 : nlines; + uint32_t nlines_to_write = nlines; + if (nlines > 0) { + struct text_chunk lastline = text_get_line(buffer->text, nlines - 1); + nlines_to_write = lastline.nbytes == 0 ? nlines - 1 : nlines; + text_for_each_line(buffer->text, 0, nlines_to_write, write_line, file); + } - text_for_each_line(buffer->text, 0, nlines_to_write, write_line, file); minibuffer_echo_timeout(4, "wrote %d lines to %s", nlines_to_write, buffer->filename); fclose(file); |
