This is an automated email from the ASF dual-hosted git repository.

sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git

commit 60535eaa364d5ec451ad518e9932e1f4f62b3ac0
Author: Szymon Janc <[email protected]>
AuthorDate: Mon Oct 19 17:51:19 2026 +0200

    newt/cli: Fix panic in complete with aliased program name
    
    "newt complete" looked for the name of the last parsed command in
    COMP_LINE. When newt was invoked under another name, e.g. through an
    alias ("COMP_LINE='n ' newt complete"), the root command name "newt"
    was not found, and the -1 index led to a slice bounds panic.
    
    Skip the first word of the command line, which is the program name,
    and only search for the last command name after it.
    
    Assisted-by: Claude:claude-opus-5-5
---
 newt/cli/complete_cmd.go | 14 +++++++++++---
 newt/cli_errors_test.go  | 25 +++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/newt/cli/complete_cmd.go b/newt/cli/complete_cmd.go
index ae3d39e6..1b2ef51e 100644
--- a/newt/cli/complete_cmd.go
+++ b/newt/cli/complete_cmd.go
@@ -143,9 +143,17 @@ func completeRunCmd(cmd *cobra.Command, args []string) {
        found_args := strings.Split(found_cmd.CommandPath(), " ")
        last_arg := found_args[len(found_args)-1]
 
-       /* what is remaining after the last parsed argument */
-       ind := strings.Index(cmd_line, last_arg)
-       ind += len(last_arg)
+       /* what is remaining after the last parsed argument; the first word
+        * is the program name, which may differ from the root command name
+        * (e.g., an alias or a path) */
+       ind := len(args[0])
+       if found_cmd != root_cmd {
+               i := strings.Index(cmd_line[ind:], last_arg)
+               if i < 0 {
+                       return
+               }
+               ind += i + len(last_arg)
+       }
        extra_str := cmd_line[ind:]
 
        if len(extra_str) == 0 {
diff --git a/newt/cli_errors_test.go b/newt/cli_errors_test.go
index 5172af73..f64d249a 100644
--- a/newt/cli_errors_test.go
+++ b/newt/cli_errors_test.go
@@ -70,3 +70,28 @@ func TestCliNoProject(t *testing.T) {
        cliErrorCheck(t, dir, nil, "No project file found", "pkg", "new", "foo")
        cliErrorCheck(t, dir, nil, "No project file found", "docs", "build")
 }
+
+// "newt complete" must handle command lines whose program name differs
+// from "newt", e.g., when newt is invoked through an alias.
+func TestCliCompleteAlias(t *testing.T) {
+       projDir := filepath.Join("testdata", "syscfg_collision")
+
+       for _, tc := range []struct {
+               line     string
+               expected string
+       }{
+               {"n ", "newt "},
+               {"n", "newt"},
+               {"n tar", "newt tar"},
+               {"n target cr", "newt target cr"},
+               {"/usr/bin/newt target cr", "newt target cr"},
+       } {
+               got := newtOutput(t, projDir, []string{"COMP_LINE=" + tc.line},
+                       "complete")
+               exp := newtOutput(t, projDir, []string{"COMP_LINE=" + 
tc.expected},
+                       "complete")
+               if got != exp {
+                       t.Errorf("COMP_LINE=%q: expected %q, got %q", tc.line, 
exp, got)
+               }
+       }
+}

Reply via email to