<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [AVR] Struct pointer is nil after a for loop which reads SPI"
   href="https://bugs.llvm.org/show_bug.cgi?id=50959">50959</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[AVR] Struct pointer is nil after a for loop which reads SPI
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>llgo
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>other
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>pwhittingslow@itba.edu.ar
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Below is the link to the particular commit which implemented a workaround to
the bug:
<a href="https://github.com/soypat/enc28j60/commit/c7cbf9e0f0b06c53e394035f548f8b92b5f14997#diff-4f427d2b022907c552328e63f137561f6de92396d7a6e8f6c2ea1bcf0db52654L18">https://github.com/soypat/enc28j60/commit/c7cbf9e0f0b06c53e394035f548f8b92b5f14997#diff-4f427d2b022907c552328e63f137561f6de92396d7a6e8f6c2ea1bcf0db52654L18</a>



This issue was observed in an Arduino Mega 2560. Compiling with tinygo default
settings (opt=z i believe)

As mentioned in the comment in the commit, moving the declaration of the Packet
struct variable after the for loop fixed the issue. The program being tested
was the following (please take a look at the comments):

```
package main

import (
        "machine"
        "unsafe"

        "github.com/soypat/net"

        "github.com/soypat/enc28j60"
        swtch "github.com/soypat/ether-swtch"
)

func main() {
        println("start")
        // SPI Chip select pin. Can be any Digital pin
        var spiCS = machine.D53
        // Inline declarations so not used as RAM
        var (
                MAC = net.HardwareAddr{0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xFF}
                // MyIP = net.IP{192, 168, 1, 5} //static setup is the only one
available
        )

        machine.SPI0.Configure(machine.SPIConfig{Frequency: 8e6})

        e := enc28j60.New(spiCS, machine.SPI0)
        enc28j60.SDB = true
        err := e.Init(MAC)
        if err != nil {
                println(err.Error())
        }

        c := swtch.NewTCPConn(e, nil, MAC)
        // Decode calls e.NextPacket() which returns the Packet
        // Packet's Read() method is called in Decode() which panics due to
accessing nil `ic` field
        // Repeating these calls in main() does no reproduce the issue.
        // I could not manage to get a MWE working.
        err = c.Decode() 
        if err != nil {
                println(err.Error())
        }
}
```</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>