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 e2365e10cccfdfdc1ef69d1bffef5e3944d78063
Author: Szymon Janc <[email protected]>
AuthorDate: Thu Oct 22 00:25:05 2026 +0200

    newtmgr/core: Fix panic converting a corefile without segments
    
    setProgHdrOff() took the program header size from cc.phdrs[0], so a
    corefile that contains only the header, or only an IMAGE TLV, made
    "newtmgr image coreconvert" (and "image coredownload -e") crash with
    "index out of range" instead of producing output.
    
    Use the size of the elf.Prog32 type instead. The result is a valid,
    if empty, ELF core file with zero program headers, and the image
    hash is still reported to the user, which is more useful than an
    error for a dump that carries only the image hash.
    
    Assisted-by: Claude:claude-opus-5-5
---
 newtmgr/core/core_convert.go      |  2 +-
 newtmgr/core/core_convert_test.go | 41 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/newtmgr/core/core_convert.go b/newtmgr/core/core_convert.go
index 4b52a31..453011a 100644
--- a/newtmgr/core/core_convert.go
+++ b/newtmgr/core/core_convert.go
@@ -238,7 +238,7 @@ func (cc *CoreConvert) makeRegInfo(regs []byte) {
 
 func (cc *CoreConvert) setProgHdrOff() {
        off := binary.Size(cc.elfHdr)
-       off += len(cc.phdrs) * binary.Size(cc.phdrs[0])
+       off += len(cc.phdrs) * binary.Size(elf.Prog32{})
 
        for idx, phdr := range cc.phdrs {
                phdr.Off = uint32(off)
diff --git a/newtmgr/core/core_convert_test.go 
b/newtmgr/core/core_convert_test.go
index aa38611..e286a59 100644
--- a/newtmgr/core/core_convert_test.go
+++ b/newtmgr/core/core_convert_test.go
@@ -310,3 +310,44 @@ func TestConvertMissingFiles(t *testing.T) {
                t.Errorf("Convert() without files: err = %v", err)
        }
 }
+
+// A corefile without memory or register TLVs results in an ELF file with
+// no program headers.  This used to panic.
+func TestConvertNoProgHdrs(t *testing.T) {
+       hash := seq(32, 1)
+
+       tests := []struct {
+               name string
+               tlvs []testTlv
+               hash []byte
+       }{
+               {"header only", nil, nil},
+               {"image only", []testTlv{{COREDUMP_TLV_IMAGE, 0, hash}}, hash},
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       cc, dst, err := convert(t, buildCore(COREDUMP_MAGIC, 
tt.tlvs))
+                       if err != nil {
+                               t.Fatalf("ConvertFilenames: %v", err)
+                       }
+                       if !bytes.Equal(cc.ImageHash, tt.hash) {
+                               t.Errorf("ImageHash = %x, want %x", 
cc.ImageHash, tt.hash)
+                       }
+
+                       f, err := elf.Open(dst)
+                       if err != nil {
+                               t.Fatalf("elf.Open: %v", err)
+                       }
+                       defer f.Close()
+
+                       if f.Type != elf.ET_CORE || f.Machine != elf.EM_ARM {
+                               t.Errorf("type/machine = %v/%v", f.Type, 
f.Machine)
+                       }
+                       if len(f.Progs) != 0 {
+                               t.Errorf("expected no program headers, got %d",
+                                       len(f.Progs))
+                       }
+               })
+       }
+}

Reply via email to