<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - strerror_r interceptor incorrectly checks return value: POSIX may return any non-zero value for failure"
   href="http://llvm.org/bugs/show_bug.cgi?id=17138">17138</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>strerror_r interceptor incorrectly checks return value: POSIX may return any non-zero value for failure
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>compiler-rt
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>compiler-rt
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>dfarler@apple.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=11153" name="attach_11153" title="Sample C code to print all error strings in OS X with strerror_r">attachment 11153</a> <a href="attachment.cgi?id=11153&action=edit" title="Sample C code to print all error strings in OS X with strerror_r">[details]</a></span>
Sample C code to print all error strings in OS X with strerror_r

Per the POSIX specification:

"The strerror_r() function shall map the error number in errnum to a
locale-dependent error message string and shall return the string in the buffer
pointed to by strerrbuf, with length buflen."
(<a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html">http://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html</a>)

OS X strerror_r will return EINVAL (22) and populate the buffer with "Unknown
error: %d" for all errnos > 106 as they are undefined (see
/usr/include/sys/errno.h).

In
<a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?diff_format=h&revision=187876&view">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?diff_format=h&revision=187876&view</a>=
, the check for whether this is a POSIX strerror_r states that "POSIX version
returns 0 on success, negative error code on failure...". This is incorrect but
then goes on to only determine POSIX with res == 0.

Additionally, POSIX specifies:

"If the value of errnum is a valid error number, the message string shall
indicate what error occurred; if the value of errnum is zero, the message
string shall either be an empty string or indicate that no error occurred;
otherwise, if these functions complete successfully, the message string shall
indicate that an unknown error occurred."
(<a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html">http://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html</a>)

In this regard, OS X complies with POSIX for strerror_r.

STEPS TO REPRODUCE:
Compile the attached file with and without -fsanitize=address on OS X.

RESULTS:
- EXC_BAD_ACCESS with -fsanitize=address
- Successful exit with "Unknown error: %d" for errnos > 106 without</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>