~sircmpwn/aerc

panic: runtime error: invalid memory address or nil pointer dereference v1 PROPOSED

Robin Jarry: 1
 view,list: fix crash when viewing incomplete imap messages

 2 files changed, 7 insertions(+), 48 deletions(-)
I am still having this segfaul daily.. I leave aerc on 24/7 and every single day I have to restart it because of this..

aerc -v                                                                                                               ░▒▓ ✔  at 19:24:12  

aerc 0.5.2.r50.g0b19b5e

```
goroutine 1 [running]:                                                                                                         ░▒▓ ✔  at 16:27:35  

runtime/debug.Stack()
	runtime/debug/stack.go:24 +0x65
runtime/debug.PrintStack()
	runtime/debug/stack.go:16 +0x19
main.PanicTermFix(0x0)
	git.sr.ht/~sircmpwn/aerc/aerc.go:212 +0x45
panic({0x55b23cd32920, 0x55b23d0b1330})
	runtime/panic.go:1047 +0x266
git.sr.ht/~sircmpwn/aerc/lib/format.ParseMessageFormat({0xc0001a4258, 0x11}, {0xc0001a4270, 0x10}, {{0xc000124751, 0x20}, {0xc000118b30, 0xa}, 0x342, 0xc0000a8500, ...})
	git.sr.ht/~sircmpwn/aerc/lib/format/format.go:149 +0x925
git.sr.ht/~sircmpwn/aerc/widgets.(*MessageList).Draw(0xc0003a84e0, 0xc000325320)
	git.sr.ht/~sircmpwn/aerc/widgets/msglist.go:156 +0xf08
git.sr.ht/~sircmpwn/aerc/lib/ui.(*Grid).Draw(0xc0003ea210, 0xc000325260)
	git.sr.ht/~sircmpwn/aerc/lib/ui/grid.go:144 +0x2ef
git.sr.ht/~sircmpwn/aerc/widgets.(*AccountView).Draw(0x203000, 0x0)
	git.sr.ht/~sircmpwn/aerc/widgets/account.go:139 +0x1d
git.sr.ht/~sircmpwn/aerc/lib/ui.(*TabContent).Draw(0xc000090000, 0x0)
	git.sr.ht/~sircmpwn/aerc/lib/ui/tab.go:394 +0x143
git.sr.ht/~sircmpwn/aerc/lib/ui.(*Grid).Draw(0xc0003ea000, 0xc000090000)
	git.sr.ht/~sircmpwn/aerc/lib/ui/grid.go:144 +0x2ef
git.sr.ht/~sircmpwn/aerc/widgets.(*Aerc).Draw(0xc0003ea0b0, 0xc000090000)
	git.sr.ht/~sircmpwn/aerc/widgets/aerc.go:177 +0x2e
git.sr.ht/~sircmpwn/aerc/lib/ui.(*UI).Tick(0xc00009c000)
	git.sr.ht/~sircmpwn/aerc/lib/ui/ui.go:113 +0x1f7
main.main()
	git.sr.ht/~sircmpwn/aerc/aerc.go:197 +0xa0c
aerc crashed: runtime error: invalid memory address or nil pointer dereference
```
Happy Hacking
ReK2


Salut
ReK2 ReK2WiLdS BBK
rek2@hispagatos.org
https://hispagatos.org
https://hispagatos.space
gemini://rek2.hispagatos.org
gemini://hispagatos.org

Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

El jueves, 30 de septiembre de 2021 a las 21:26, Kiril Vladimirov <kiril@vladimiroff.org> escribió:
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/patches/26013/mbox | git am -3
Learn more about email & git

[PATCH] view,list: fix crash when viewing incomplete imap messages Export this patch

With IMAP, due to an unidentified reason, some messages to not have any
body accessible. When viewing them, aerc crashes:

  git.sr.ht/~sircmpwn/aerc/lib.usePGP
          lib/messageview.go:37
  git.sr.ht/~sircmpwn/aerc/lib.NewMessageStoreView
          lib/messageview.go:67
  git.sr.ht/~sircmpwn/aerc/commands/account.ViewMessage.Execute
          commands/account/view.go:45
  git.sr.ht/~sircmpwn/aerc/commands.(*Commands).ExecuteCommand
          commands/commands.go:66
  main.execCommand
          aerc.go:61
  main.main.func2
          aerc.go:160
  aerc crashed: runtime error: invalid memory address or nil pointer
  dereference

Check the pointer before dereferencing.

Also, add a global check in ParseMessageFormat where a similar issue may
occur.

Signed-off-by: Robin Jarry <robin@jarry.cc>
---
 lib/format/format.go | 52 ++++----------------------------------------
 lib/messageview.go   |  3 +++
 2 files changed, 7 insertions(+), 48 deletions(-)

diff --git a/lib/format/format.go b/lib/format/format.go
index 16398865c8d6..4ee62acaea9c 100644
--- a/lib/format/format.go
+++ b/lib/format/format.go
@@ -58,6 +58,10 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
	}

	envelope := ctx.MsgInfo.Envelope
	if envelope == nil {
		return "", nil,
			errors.New("no envelope available for this message")
	}

	var c rune
	for i, ni := 0, 0; i < len(format); {
@@ -105,10 +109,6 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
		case '%':
			retval = append(retval, '%')
		case 'a':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			if len(envelope.From) == 0 {
				return "", nil,
					errors.New("found no address for sender")
@@ -117,10 +117,6 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
			retval = append(retval, 's')
			args = append(args, addr.Address)
		case 'A':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			var addr *mail.Address
			if len(envelope.ReplyTo) == 0 {
				if len(envelope.From) == 0 {
@@ -156,10 +152,6 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
				dummyIfZeroDate(date.Local(),
					timeFmt, thisDayTimeFmt, thisYearTimeFmt))
		case 'f':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			if len(envelope.From) == 0 {
				return "", nil,
					errors.New("found no address for sender")
@@ -168,10 +160,6 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
			retval = append(retval, 's')
			args = append(args, addr)
		case 'F':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			if len(envelope.From) == 0 {
				return "", nil,
					errors.New("found no address for sender")
@@ -196,17 +184,9 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
			args = append(args, strings.Join(ctx.MsgInfo.Labels, ", "))

		case 'i':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			retval = append(retval, 's')
			args = append(args, envelope.MessageId)
		case 'n':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			if len(envelope.From) == 0 {
				return "", nil,
					errors.New("found no address for sender")
@@ -221,33 +201,17 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
			retval = append(retval, 's')
			args = append(args, val)
		case 'r':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			addrs := FormatAddresses(envelope.To)
			retval = append(retval, 's')
			args = append(args, addrs)
		case 'R':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			addrs := FormatAddresses(envelope.Cc)
			retval = append(retval, 's')
			args = append(args, addrs)
		case 's':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			retval = append(retval, 's')
			args = append(args, envelope.Subject)
		case 't':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			if len(envelope.To) == 0 {
				return "", nil,
					errors.New("found no address for recipient")
@@ -259,10 +223,6 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
			retval = append(retval, 's')
			args = append(args, ctx.AccountName)
		case 'u':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			if len(envelope.From) == 0 {
				return "", nil,
					errors.New("found no address for sender")
@@ -275,10 +235,6 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
			retval = append(retval, 's')
			args = append(args, mailbox)
		case 'v':
			if envelope == nil {
				return "", nil,
					errors.New("no envelope available for this message")
			}
			if len(envelope.From) == 0 {
				return "", nil,
					errors.New("found no address for sender")
diff --git a/lib/messageview.go b/lib/messageview.go
index 08ea92fb8bbe..4f1d0cde1db2 100644
--- a/lib/messageview.go
+++ b/lib/messageview.go
@@ -34,6 +34,9 @@ type MessageView interface {
}

func usePGP(info *models.BodyStructure) bool {
	if info == nil {
		return false
	}
	if info.MIMEType == "application" {
		if info.MIMESubType == "pgp-encrypted" ||
			info.MIMESubType == "pgp-signature" {
-- 
2.30.2