title: How to work around lack of NUL terminator in strings returned from mmap()?
body: <p>When mmap()ing a text file, like so</p>
  
  <pre><code>int fd = open("file.txt", O_RDWR);
  fstat(fd, &amp;sb)
  char *text = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  </code></pre>
  
  <p>the file contents are mapped into memory directly, and <code>text</code> it will not contain a NUL-terminator so operating on it with normal string functions would not be safe. On Linux (at least) the remaining bytes of the unused page are zero-filled, so effectively you get a NUL terminator in all cases where the file size isn't a multiple of the page size.</p>
  
  <p>But relying on that feels dirty and other <code>mmap()</code> implementations (e.g., in FreeBSD, I think) don't zero-fill partial pages. Mapping files that are multiples of the page size will also lack the NUL terminator.</p>
  
  <p>Are there reasonable ways to work around this or to add the NUL terminator?</p>
  
  <p>Things I've considered</p>
  
  <ol>
  <li>Using <code>strn*()</code> functions exclusively and tracking distance to the end of the buffer.
  
  <ul>
  <li>Pros: No need for NUL terminator</li>
  <li>Cons: Extra tracking required to know distance to end of file when parsing text; some <code>str*()</code> functions don't have <code>strn*()</code> counterpart, like <code>strstr</code>.</li>
  </ul></li>
  <li>As <a href="https://stackoverflow.com/questions/11037521/how-to-use-regexec-with-memory-mapped-files">another answer</a> suggested, make a anonymous mapping at a fixed address following the mapping of your text file.
  
  <ul>
  <li>Pros: Can use regular C <code>str*()</code> functions</li>
  <li>Cons: Using <code>MAP_FIXED</code> is not thread-safe; Seems like an awful hack anyway</li>
  </ul></li>
  <li><code>mmap()</code> an extra byte and make the map writeable, and write the NUL terminator. The OpenGroup's <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/mmap.html" rel="noreferrer">mmap man page</a> says you can make a mapping larger than your object's size but that accessing data outside of the actual mapped object will generate a <code>SIGBUS</code>.
  
  <ul>
  <li>Pros: Can use regular C <code>str*()</code> functions</li>
  <li>Cons: Requires handling (ignoring?) <code>SIGBUS</code>, which could potentially mean something else happened. I'm not actually sure writing the NUL terminator will work?</li>
  </ul></li>
  <li>Expand files with sizes that are multiples of page size with <code>ftruncate()</code> by one byte.
  
  <ul>
  <li>Pros: Can use regular C <code>str*()</code> functions; <code>ftruncate()</code> will write a NUL byte to the newly allocated area for you</li>
  <li>Cons: Means we have to write to the files, which may not be possible or acceptable in all cases; Doesn't solve problem for <code>mmap()</code> implementations that don't zero-fill partial pages</li>
  </ul></li>
  <li>Just <code>read()</code> the file into some <code>malloc()</code>'d memory and forget about <code>mmap()</code>
  
  <ul>
  <li>Pros: Avoids all of <em>these</em> solutions; Easy to <code>malloc()</code> and extra byte for NUL</li>
  <li>Cons: Different performance characteristics than <code>mmap()</code></li>
  </ul></li>
  </ol>
  
  <p>Solution #1 seems generally the best, and just requires a some extra work on the part of the functions reading the text.</p>
  
  <p>Are there better alternatives, or are these the best solutions? Are there aspects of these solutions I haven't considered that makes them more or less attractive?</p>