title: Portable way to check if a char* pointer is a null-terminated string
body: <p>I have a C function that takes in a char* pointer. One of the function's preconditions is that the pointer argument is a null-terminated string</p>
  
  <pre><code>void foo(char *str) {
      int length = strlen(str);
      // ...
  }
  </code></pre>
  
  <p>If <code>str</code> isn't a pointer to a null-terminated string, then <code>strlen</code> crashes. Is there a portable way to ensure that a char* pointer really does point to a null-terminated string?</p>
  
  <p>I was thinking about using <code>VirtualQuery</code> to find lowest address after <code>str</code> that's not readable, and if we haven't seen a null-terminator between the beginning of <code>str</code> and that address, then <code>str</code> doesn't point to a null-terminated string.</p>