view,list: fix crash when viewing incomplete imap messages

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>
This commit is contained in:
Robin Jarry 2021-10-28 14:59:54 +02:00
parent fc84b19bba
commit 074b0a1bd8
2 changed files with 7 additions and 48 deletions

View file

@ -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")

View file

@ -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" {