title: Handling empty string input leading to segmentation fault
body: <p>I'm encoding the contents of a message struct into a buffer.</p>
  
  <pre><code>int encode(const struct message *msg, unsigned char *buffer, size_t max_size)
  {
    if (buffer == NULL)
      return -1;
  
    unsigned char *buf_pos = buffer;
    unsigned char *ep = buffer + max_size;
  
    if (buf_pos + 1 &lt;= ep) {
      *buf_pos++ = SYNC_WORD_1;
    } else {
      return buf_pos - buffer;
    }
    .
    .
    .
  }
  </code></pre>
  
  <p>When I call <code>encode(&amp;message, "", 1024);</code> I encounter a segmentation fault as expected. My understanding is that the segfault is caused by an attempt to access memory not allocated to the program, since <code>""</code> will contain just the null terminator and I'm passing it in place of a pointer.</p>
  
  <p>The problem I'm having is when I try to handle this error. I haven't found a way to identify the invalid input that doesn't either cause a false-positive with valid inputs or another segfault.</p>
  
  <p>So what's the correct way to weed out this kind of input?</p>