<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - -fsanitize=undefined does not catch UB static pointers initialization"
   href="https://bugs.llvm.org/show_bug.cgi?id=35782">35782</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>-fsanitize=undefined does not catch UB static pointers initialization
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>5.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </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>C++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>shachar@shemesh.biz
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Please consider the following (rather strange) program:

#include <cstdlib>

// _static_, no-one outside this translation unit can touch it
// Global static --> initialized to nullptr
static void (*fun) ();

void evil() {
    system("rm -rf /");
}

void set() {
    fun = &evil;
}

int main() {
    fun(); // nullptr dereference = UB.
    // Static variable, so no-one can touch it except us. So...
    // the only non-UB thing that could have happened is that
    // set() was called before main(). So let's assume that and
    // continue optimization...
    // >:D
}

When you switch the system call with a printf and compile with clang++-5.0, the
function gets triggered (-O3 -Wall -Wextra). The comment does a good job of
explaining why.

But when you add -fsanitize=undefined, I expected there to be some complaint
about the code running undefined behavior. No such complaint arises, and the
code still runs, still executes "evil", same as without the sanitizer.</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>