Skip to content

Commit

Permalink
Fix crash when running copy -bik and also clean up cmd_copy()
Browse files Browse the repository at this point in the history
Reproducer:

* Start `dte`
* Insert a few characters
* Run `copy -bik`

Result:

> src/block-iter.c:356: **BUG** in block_iter_get_bytes():
> 'pos < len && blk->node.next == bi->head'
>
> Stack trace:
>   #0 0x721e6a90bdd8 in __sanitizer_print_stack_trace asan_stack.cpp:87
>   #1 0x5fe2730bb194 in print_stack_trace src/util/debug.c:67
>   #2 0x5fe2730bb72f in bug src/util/debug.c:86
>   #3 0x5fe272fe0d43 in block_iter_get_bytes src/block-iter.c:356
>   #4 0x5fe27300516b in cmd_copy src/commands.c:509
>   #5 0x5fe272fdcc8a in command_func_call src/command/run.h:74
>   #6 0x5fe272fdd29d in handle_binding src/bind.c:51
>   #7 0x5fe27304acef in handle_input_single src/mode.c:80
>   #8 0x5fe27304b08a in handle_input_recursive src/mode.c:89
>   #9 0x5fe27304b311 in handle_input src/mode.c:106
>   #10 0x5fe273023a01 in main_loop src/editor.c:338
>   #11 0x5fe273049b0f in main src/main.c:663
>   #12 0x721e69e34e07  (/usr/lib/libc.so.6+0x25e07)
>   #13 0x721e69e34ecb in __libc_start_main (/usr/lib/libc.so.6+0x25ecb)
>   #14 0x5fe272fdcb24 in _start (dte/dte+0x16bb24)

Thanks to Pedro Navarro for the bug report!
  • Loading branch information
craigbarnes committed Jan 12, 2025
1 parent 702f2fd commit 93d7ea6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
34 changes: 16 additions & 18 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,19 @@ static bool cmd_copy(EditorState *e, const CommandArgs *a)
return true;
}

View *view = e->view;
const BlockIter save = view->cursor;
const View *view = e->view;
BlockIter bi;
size_t size;
bool line_copy;
if (view->selection) {
size = prepare_selection(view);
SelectionInfo info = init_selection(view);
size = info.eo - info.so;
bi = info.si;
line_copy = (view->selection == SELECT_LINES);
} else {
block_iter_bol(&view->cursor);
BlockIter tmp = view->cursor;
bi = view->cursor;
block_iter_bol(&bi);
BlockIter tmp = bi;
size = block_iter_eat_line(&tmp);
line_copy = true;
}
Expand All @@ -495,29 +498,24 @@ static bool cmd_copy(EditorState *e, const CommandArgs *a)
return true;
}

if (internal) {
copy(&e->clipboard, view, size, line_copy);
}

char *buf = block_iter_get_bytes(&bi, size);
if (osc52) {
if (internal) {
view->cursor = save;
if (view->selection) {
size = prepare_selection(view);
}
}
char *buf = block_iter_get_bytes(&view->cursor, size);
if (!term_osc52_copy(&term->obuf, buf, size, clipboard, primary)) {
error_msg_errno(e->err, "OSC 52 copy failed");
}
}

if (internal) {
// Clipboard takes ownership of `buf`
record_copy(&e->clipboard, buf, size, line_copy);
} else {
free(buf);
}

if (!has_flag(a, 'k')) {
unselect(view);
unselect(e->view);
}

view->cursor = save;
// TODO: return false if term_osc52_copy() failed?
return true;
}
Expand Down
9 changes: 1 addition & 8 deletions src/copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ void record_copy(Clipboard *clip, char *buf, size_t len, bool is_lines)
clip->is_lines = is_lines;
}

void copy(Clipboard *clip, View *view, size_t len, bool is_lines)
void cut(Clipboard *clip, View *view, size_t len, bool is_lines)
{
if (len) {
char *buf = block_iter_get_bytes(&view->cursor, len);
record_copy(clip, buf, len, is_lines);
}
}

void cut(Clipboard *clip, View *view, size_t len, bool is_lines)
{
if (len) {
copy(clip, view, len, is_lines);
buffer_delete_bytes(view, len);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ typedef enum {
} PasteLinesType;

void record_copy(Clipboard *clip, char *buf, size_t len, bool is_lines);
void copy(Clipboard *clip, View *view, size_t len, bool is_lines);
void cut(Clipboard *clip, View *view, size_t len, bool is_lines);
void paste(Clipboard *clip, View *view, PasteLinesType type, bool move_after);

Expand Down

0 comments on commit 93d7ea6

Please sign in to comment.