Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch error in Goroutines, panic: runtime error: invalid memory address or nil pointer dereference #645

Closed
dmitriblov opened this issue Sep 26, 2024 · 1 comment

Comments

@dmitriblov
Copy link

Hi Salut, I'm facing this issue.
Here is my error below.

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0xc8054]

goroutine 2214 [running]:
io.discard.ReadFrom({}, {0x0, 0x0})
        C:/Program Files/Go/src/io/io.go:666 +0x54
io.copyBuffer({0x355de0, 0x61d100}, {0x0, 0x0}, {0x0, 0x0, 0x0})
        C:/Program Files/Go/src/io/io.go:415 +0x151
io.Copy(...)
        C:/Program Files/Go/src/io/io.go:388
github.com/emersion/go-imap/v2/imapclient.FetchItemDataBodySection.discard(...)
        C:/Users/Administrator/go/pkg/mod/github.com/emersion/go-imap/[email protected]/imapclient/fetch.go:324
github.com/emersion/go-imap/v2/imapclient.(*FetchMessageData).Next(0xc0000bd4a0)
        C:/Users/Administrator/go/pkg/mod/github.com/emersion/go-imap/[email protected]/imapclient/fetch.go:253 +0x46
github.com/emersion/go-imap/v2/imapclient.(*FetchMessageData).discard(...)
        C:/Users/Administrator/go/pkg/mod/github.com/emersion/go-imap/[email protected]/imapclient/fetch.go:263
created by github.com/emersion/go-imap/v2/imapclient.(*Client).handleFetch.func1 in goroutine 5
        C:/Users/Administrator/go/pkg/mod/github.com/emersion/go-imap/[email protected]/imapclient/fetch.go:498 +0x178
exit status 2

I'm trying to return FetchItemDataBodySection inside a Goroutines. I already checked if body_section.Literal is nil but I'm still getting the same error.

literal := body_section.Literal
if literal == nil {
return
}

Here is my code below

all_seq_nums := result.AllSeqNums()
count := len(all_seq_nums)
if count > 0 {
  thread, _ := strconv.Atoi(thread)
  semaphore := make(chan struct{}, thread)
  var wg sync.WaitGroup

  for seq_num := uint32(0); seq_num < uint32(count) ; seq_num++ {
    wg.Add(1)
    semaphore <- struct{}{}
    go func(seq_num uint32) {
      defer wg.Done()
      defer func() { <-semaphore }()

      seq_set := imap.SeqSetNum(all_seq_nums[seq_num])
      fetch_options := &imap.FetchOptions{
        BodySection: []*imap.FetchItemBodySection{{}},
      }
      fetch_cmd := client.Fetch(seq_set, fetch_options)
      if fetch_cmd == nil {
        return
      }

      msg := fetch_cmd.Next()
      if msg == nil {
        fmt.Print("\n[-]  Message not found, skipping...")
        return
      }

      var body_section imapclient.FetchItemDataBodySection
      ok := false
      for {
        item := msg.Next()
        if item == nil {
          break
        }
        body_section, ok = item.(imapclient.FetchItemDataBodySection)
        if ok {
          break
        }
      }

      if ok {
        literal := body_section.Literal
        if literal == nil {
          return
        }

        mr, err := mail.CreateReader(literal)
        if err == nil {
          for {
            p, err := mr.NextPart()
            if err == io.EOF {
              break
            }

            switch p.Header.(type) {
            case *mail.InlineHeader:
              b, _ := io.ReadAll(p.Body)
              parse_message(string(b), imap_credential, mailbox_folder, all_seq_nums[seq_num])
            }
          }
        }
      }
      
      fetch_cmd.Close()
    }(seq_num)
  }

  wg.Wait()
}
@emersion
Copy link
Owner

Duplicate of #644

@emersion emersion marked this as a duplicate of #644 Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants