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 8aaefc8a6025cb57987308182c0461e999a04c97
Author: Szymon Janc <[email protected]>
AuthorDate: Mon Oct 19 17:50:00 2026 +0200

    newt/cli: Fix panic in man and apropos without argument
    
    "newt man" and "newt apropos" used args[0] without checking that an
    argument was given, causing an index out of range panic. Print usage
    with an error instead.
    
    Assisted-by: Claude:claude-opus-5-5
---
 newt/cli/man_cmds.go    |  9 +++++++++
 newt/cli_errors_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/newt/cli/man_cmds.go b/newt/cli/man_cmds.go
index eb59c310..6160f13a 100644
--- a/newt/cli/man_cmds.go
+++ b/newt/cli/man_cmds.go
@@ -22,6 +22,7 @@ package cli
 import (
        "github.com/spf13/cobra"
        "mynewt.apache.org/newt/newt/man"
+       "mynewt.apache.org/newt/util"
 )
 
 func manBuildRunCmd(cmd *cobra.Command, args []string) {
@@ -33,6 +34,10 @@ func manBuildRunCmd(cmd *cobra.Command, args []string) {
 }
 
 func manRunCmd(cmd *cobra.Command, args []string) {
+       if len(args) != 1 {
+               NewtUsage(cmd, util.NewNewtError("Exactly one argument 
required"))
+       }
+
        proj := TryGetProject()
        err := man.RunMan(proj, args)
        if err != nil {
@@ -41,6 +46,10 @@ func manRunCmd(cmd *cobra.Command, args []string) {
 }
 
 func aproposRunCmd(cmd *cobra.Command, args []string) {
+       if len(args) != 1 {
+               NewtUsage(cmd, util.NewNewtError("Exactly one argument 
required"))
+       }
+
        proj := TryGetProject()
        err := man.RunApropos(proj, args)
        if err != nil {
diff --git a/newt/cli_errors_test.go b/newt/cli_errors_test.go
new file mode 100644
index 00000000..5b81baf2
--- /dev/null
+++ b/newt/cli_errors_test.go
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package main
+
+import (
+       "path/filepath"
+       "strings"
+       "testing"
+)
+
+// cliErrorCheck runs newt and verifies that it fails gracefully, i.e.,
+// with an error message containing msg rather than with a panic.
+func cliErrorCheck(t *testing.T, dir string, env []string, msg string,
+       args ...string) {
+
+       t.Helper()
+
+       out, err := newtCombinedOutput(dir, env, args...)
+       if err == nil {
+               t.Errorf("newt %q succeeded\n%s", args, out)
+       }
+       if strings.Contains(out, "panic") || strings.Contains(out, "goroutine 
") {
+               t.Errorf("newt %q panicked\n%s", args, out)
+       }
+       if !strings.Contains(out, msg) {
+               t.Errorf("newt %q: missing %q in output\n%s", args, msg, out)
+       }
+}
+
+// "newt man" and "newt apropos" require an argument.
+func TestCliManNoArgs(t *testing.T) {
+       projDir := filepath.Join("testdata", "syscfg_collision")
+
+       cliErrorCheck(t, projDir, nil, "Exactly one argument required", "man")
+       cliErrorCheck(t, projDir, nil, "Exactly one argument required",
+               "apropos")
+}

Reply via email to