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-newtmgr.git

commit e3ca36d9da240147333ffa8d50bd858d26cf0514
Author: Szymon Janc <[email protected]>
AuthorDate: Thu Oct 22 01:06:16 2026 +0200

    nmxact/xact: Fix Status() of results without responses
    
    FsDownloadResult, FsUploadResult and CoreLoadResult returned the rc of
    the last response without checking that there is one.  FsUploadCmd
    does not send any request for empty data, so calling Status() on the
    result of uploading an empty file panicked with an index out of range.
    
    Return NMP_ERR_EUNKNOWN for an empty result, as ImageUploadResult and
    LogShowFullResult already do.
    
    Assisted-by: Claude:claude-opus-5-5
---
 nmxact/xact/fs.go      | 14 ++++++++++----
 nmxact/xact/fs_test.go | 28 ++++++++++++++++++++++++++++
 nmxact/xact/image.go   |  7 +++++--
 3 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/nmxact/xact/fs.go b/nmxact/xact/fs.go
index be12879..7c0d581 100644
--- a/nmxact/xact/fs.go
+++ b/nmxact/xact/fs.go
@@ -53,8 +53,11 @@ func newFsDownloadResult() *FsDownloadResult {
 }
 
 func (r *FsDownloadResult) Status() int {
-       rsp := r.Rsps[len(r.Rsps)-1]
-       return rsp.Rc
+       if len(r.Rsps) > 0 {
+               return r.Rsps[len(r.Rsps)-1].Rc
+       } else {
+               return nmp.NMP_ERR_EUNKNOWN
+       }
 }
 
 func (c *FsDownloadCmd) Run(s sesn.Sesn) (Result, error) {
@@ -119,8 +122,11 @@ func newFsUploadResult() *FsUploadResult {
 }
 
 func (r *FsUploadResult) Status() int {
-       rsp := r.Rsps[len(r.Rsps)-1]
-       return rsp.Rc
+       if len(r.Rsps) > 0 {
+               return r.Rsps[len(r.Rsps)-1].Rc
+       } else {
+               return nmp.NMP_ERR_EUNKNOWN
+       }
 }
 
 func buildFsUploadReq(name string, fileSz int, chunk []byte,
diff --git a/nmxact/xact/fs_test.go b/nmxact/xact/fs_test.go
index 81798da..90f2f37 100644
--- a/nmxact/xact/fs_test.go
+++ b/nmxact/xact/fs_test.go
@@ -481,3 +481,31 @@ func TestFsDownloadRunTxError(t *testing.T) {
                t.Errorf("no error")
        }
 }
+
+// Status() of a result without responses (e.g. uploading an empty file,
+// which sends no request) must not panic.
+func TestMultiRspResultStatusEmpty(t *testing.T) {
+       results := []Result{
+               &FsDownloadResult{},
+               &FsUploadResult{},
+               &CoreLoadResult{},
+               &ImageUploadResult{},
+               &LogShowFullResult{},
+       }
+       for _, r := range results {
+               if st := r.Status(); st != nmp.NMP_ERR_EUNKNOWN {
+                       t.Errorf("%T: status %d", r, st)
+               }
+       }
+
+       s := newFakeSesn(newFakeFsDev().handle)
+       c := NewFsUploadCmd()
+       c.Name = "/f"
+       res, err := runCmd(t, c, s)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if st := res.Status(); st != nmp.NMP_ERR_EUNKNOWN {
+               t.Errorf("empty upload: status %d", st)
+       }
+}
diff --git a/nmxact/xact/image.go b/nmxact/xact/image.go
index e81a2bd..f212a47 100644
--- a/nmxact/xact/image.go
+++ b/nmxact/xact/image.go
@@ -783,8 +783,11 @@ func newCoreLoadResult() *CoreLoadResult {
 }
 
 func (r *CoreLoadResult) Status() int {
-       rsp := r.Rsps[len(r.Rsps)-1]
-       return rsp.Rc
+       if len(r.Rsps) > 0 {
+               return r.Rsps[len(r.Rsps)-1].Rc
+       } else {
+               return nmp.NMP_ERR_EUNKNOWN
+       }
 }
 
 func (c *CoreLoadCmd) Run(s sesn.Sesn) (Result, error) {

Reply via email to