Skip to content

Panic when using command -r in an abbr --function #11324

Open

Description

@kboshold

If you execute a commandline -r in an abbr --function, abbr still tries to replace the abbr text at the old offset. This leads to an index out of bounds error.

Possible solutions:

  1. Prohibit commandline -r in abbr --function
  2. Read out the current command line before executing abbr --function and then only work with this command line. (In this case abbr should offer an option to replace the complete line and not just the abbreviation itself)
  3. If the commandline changes when the abbr --function is executed, the replacement of the abbreviation is no longer executed

Environment:

fish --version
# fish, version 4.0.1

uname -a
# Linux XXX 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

echo $TERM
# xterm-256color

Start a new fish session using:

sh -c 'env HOME=$(mktemp -d) XDG_CONFIG_HOME= XDG_DATA_DIRS= RUST_BACKTRACE=full fish'

Execute the following code to obtain the malfunctioning setup:

function _test
    set -l current_line (commandline)
    commandline -r ""
    echo "test"
end

abbr -a @test --position anywhere --function _test

Now that the required environment has been set up, we can execute the previously added abbr:

test @test 

After executing the command (press enter or space at the end), the following error is displayed:

thread 'main' panicked at src/editable_line.rs:208:29:
index out of bounds
stack backtrace:
   0:     0x561531855bdc - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h1d1b4b5fa6f43fbc
   1:     0x561531614423 - core::fmt::write::ha0383308c4b73ef5
   2:     0x56153185736e - std::io::Write::write_fmt::h3d049babadfff437
   3:     0x561531866b4f - std::panicking::default_hook::hf34f99039c48f075
   4:     0x56153183b976 - fish::panic::panic_handler::{{closure}}::h345d771d51b8741b
   5:     0x5615318676c8 - std::panicking::rust_panic_with_hook::h0da62165dfee2b9c
   6:     0x561531856088 - std::panicking::begin_panic_handler::{{closure}}::h3aa93158d08ca3fc
   7:     0x561531855fe9 - std::sys::backtrace::__rust_end_short_backtrace::h59b242cd399f0746
   8:     0x5615318670bc - rust_begin_unwind
   9:     0x5615315f052f - core::panicking::panic_fmt::h9f331d0a33e30d27
  10:     0x5615315f03aa - core::option::expect_failed::hfd5b4101262fba60
  11:     0x56153174a988 - fish::editable_line::EditableLine::push_edit::h3072887e51bc4cfc
  12:     0x561531779622 - fish::reader::Reader::expand_abbreviation_at_cursor::hd6125625d3aa6bbf
  13:     0x561531777721 - fish::reader::Reader::handle_execute::h7eeba566d324c4a1
  14:     0x561531774565 - fish::reader::Reader::handle_readline_command::h15e5cdc6b2b644dc
  15:     0x56153176fa93 - fish::reader::Reader::handle_char_event::h5c47ee2ad2b92d58
  16:     0x56153176d0a8 - fish::reader::Reader::readline::h3ef3ff6e203880d5
  17:     0x5615317696a5 - fish::reader::reader_read::ha19f86a9dbbf9de8
  18:     0x561531606e48 - fish::throwing_main::h3a6d227385f13466
  19:     0x56153183b63f - fish::panic::panic_handler::hdec78391ac6e535e
  20:     0x561531600d9f - fish::main::h734af118209e6845
  21:     0x56153183b453 - std::sys::backtrace::__rust_begin_short_backtrace::h49a18dfa8f7ba2bf
  22:     0x56153160b380 - main
  23:     0x7efc2b4301fe - __libc_start_call_main
  24:     0x7efc2b4302b9 - __libc_start_main_alias_2
  25:     0x5615315fe7e5 - _start
  26:                0x0 - <unknown>
fish crashed, please report a bug. Debug PID 84539 or press Enter to exit

Activity

  1. changed the title [-]Panic for `command -r` on using `abbr --function`[/-] [+]Panic when using `command -r` in an `abbr --function`[/+] on Mar 26, 2025
  2. kboshold commented on Mar 26, 2025

    @kboshold
    Author

    It is possible to workaround the issue. This solution is only possible when the commandline -r text is equal or longer the existing text (commandline -b).

    In this case you have to echo the correct replacement to not generate a invalid output.

    Example

    Not working (This will remove 3 characters)

    function _yank_line
        set -l current_line (commandline -b)
        set -l new_line (string replace $argv[1] "" $current_line)
        set -l new_line (string trim $new_line | string escape | string replace -a '"' '\\"')
        set -l new_line "echo $new_line | yank"
    
        commandline -r $new_line
    end
    
    abbr -a @yl --position anywhere --function _yank_line
    
    # Input:
    # echo test @yl 
    
    # Output:
    # echo "echost" | yank 

    Solution

    function _yank_line
        set -l current_line (commandline -b)
        set -l new_line (string replace $argv[1] "" $current_line)
        set -l new_line (string trim $new_line | string escape | string replace -a '"' '\\"')
        set -l new_line "echo $new_line | yank"
    
        set -l pos (string match -n -r $argv[1] $current_line | string split " ")
        set -l replacement (string sub -s $pos[1] -l $pos[2] $new_line)
    
        commandline -r $new_line
        echo $replacement
    end
    
    abbr -a @yl --position anywhere --function _yank_line
    
    # Input:
    # echo test @yl 
    
    # Output
    # echo "echo test" | yank 
  3. added
    regressionSomething that used to work, but was broken, especially between releases
    on Mar 26, 2025
  4. added this to the fish 4.0.2 milestone on Mar 26, 2025
  5. faho commented on Mar 26, 2025

    @faho
    Member

    (there's very likely a nicer fix than my inelegant one, but I had it ready and I believe it works so I pushed it.

    not cherry-picking it to 4.0.2 in case there's something better)

  6. added a commit that references this issue on Mar 26, 2025
    89b62a5
  7. faho commented on Mar 27, 2025

    @faho
    Member

    Yeah, nah, that's not enough.

    This can't only be triggered if the commandline is empty - even commandline -r f causes it as long as "f" is shorter than the abbreviation key.

    I think we really need to check if the ranges are in-bounds.

  8. reopened this on Mar 27, 2025
  9. self-assigned this
    on Mar 27, 2025
  10. krobelus commented on Mar 27, 2025

    @krobelus
    Contributor

    commandline -r and commandline -f inside the abbr --function callback should probably be an error.
    I plan to make it silently do nothing (which is historical behavior), then I don't have to come up with an error message.

    If we ever want to support this, we can implement some kind of three-way merging heuristic.

  11. kboshold commented on Mar 27, 2025

    @kboshold
    Author

    @krobelus but is there still a way to implement what I had above?

    I would like to be able to replace the complete line without having to use regular expression.

  12. krobelus commented on Mar 27, 2025

    @krobelus
    Contributor

    you can use a binding: bind ctrl-g,y,l _yank_line

    BTW this specific one already exists, it's bound to ctrl-x.

    The abbreviation framework could support something similar in future

    If the commandline changes when the abbr --function is executed, the replacement of the abbreviation is no longer executed

    This doesn't sound totally unreasonable.
    But note that today --function arguments are tried one by one until one succeeds.
    What should we do if the first one modifies the commandline but fails? Also, should cursor movement count as modification too?
    Maybe _yank_line outputting nothing should be interpreted as "don't expand the abbrevation".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

regressionSomething that used to work, but was broken, especially between releases

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions