title: How to iterate through a linked list without buffer overflow?
body: <p>I wrote</p>
  <pre><code>while (ptr-&gt;next != NULL) {
          //code here
          ptr = ptr-&gt;next;
      }
  </code></pre>
  <p>and AddressSanitizer is throwing an heap-buffer overflow error.</p>
  <p>I added</p>
  <pre><code>if (ptr-&gt;next != NULL) {
      while (ptr-&gt;next != NULL) {
          //code here
          ptr = ptr-&gt;next;
      }
  }
  </code></pre>
  <p>hoping that maybe it would avoid reading a unallocated address, but now AddressSanitizer is terminating my program with SEGV. I'm not really sure how to fix this as I'm new to programming in C, any insights would be very helpful. Thank you!</p>