title: Is it legal to pass a non-null-terminated string to strncmp in C?
body: <p>I have an array of 16 bytes that holds the name of an executable's segment.</p>
  
  <pre><code>char segname[16];
  </code></pre>
  
  <p>If the segment name length is less than 16 bytes, then the rest is padded with null bytes. Otherwise, there is no terminating null byte.</p>
  
  <p>I want to compare <code>segname</code> to various strings, e.g. <code>__text</code>.</p>
  
  <p>Is it legal to call <code>strncmp</code> with a non-null-terminated string?</p>
  
  <p><a href="https://stackoverflow.com/questions/28997095/comparing-null-terminated-string-with-a-non-null-terminated-string-in-c">This post</a> assumes it is legal. <a href="https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/ppc.subproj/strncmp.c" rel="nofollow noreferrer">This source code</a> makes it legal too. But my man's page says:</p>
  
  <blockquote>
    <p>The <code>strncmp()</code> function lexicographically compares the null-terminated strings <code>s1</code> and <code>s2</code>.</p>
  </blockquote>
  
  <p>The size passed to <code>strncmp</code> will be the size of <code>segname</code>.</p>
  
  <p>I'm wondering what I should refer to.</p>