title: Buffer Overflow Sample: Why is this code dangerous?
body: <p>I'm fairly new to c and I'm reading a book regarding Software Vulnerabilities and I came across this buffer overflow sample, it mentions that this can cause a buffer overflow. I am trying to determine how this is the case.</p>
  <pre class="lang-c prettyprint-override"><code>int handle_query_string(char *query_string)
  {
      struct keyval *qstring_values, *ent;
      char buf[1024];
          
      if(!query_string) {
          return 0;
      }
      
      qstring_values = split_keyvalue_pairs(query_string);
          
      if((ent = find_entry(qstring_values, &quot;mode&quot;)) != NULL) {
          sprintf(buf, &quot;MODE=%s&quot;, ent-&gt;value);
          putenv(buf);
      }
  }
  </code></pre>
  <p>I am paying close attention to this block of code because this appears to be where the buffer overflow is caused.</p>
  <pre class="lang-c prettyprint-override"><code>if((ent = find_entry(qstring_values, &quot;mode&quot;)) != NULL)
  {
      sprintf(buf, &quot;MODE=%s&quot;, ent-&gt;value);
      putenv(buf);
  }
  </code></pre>