<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 - Clang "-O2" option cause if statement check error"
   href="https://bugs.llvm.org/show_bug.cgi?id=35057">35057</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang "-O2" option cause if statement check error
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.8
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>FreeBSD
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>Zhixin.Wan@watchguard.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=19333" name="attach_19333" title="a small C file to reproduce this issue">attachment 19333</a> <a href="attachment.cgi?id=19333&action=edit" title="a small C file to reproduce this issue">[details]</a></span>
a small C file to reproduce this issue

Overview:
While building Apple's mDNSResponder on FreeBSD 11.0, I found a strange issue:
one if statement in mDNS_Lock() function failed to return true under extreme
condition where m->timenow_last=2147483647 and m->timenow=-2147483648.

(line 109, test.c)
        if (m->timenow_last - m->timenow > 0)
...

To reproduce this problem, I write a small C file, and just copy mDNS_Lock()
function implementation from mDNSResponder, and comment/replace some
unimportant function inside, but keep main statement of it.

Please see the attachment: test.c

Steps to Reproduce:
 1) Compile it with Clang 3.8.0 built in FreeBSD 11.0 AMD64 with "-O2" option:
 $ cc -O2 -o test test.c
 $ ./test
 $ mDNSPlatformRawTime went backwards by 4294967295 ticks; setting correction
factor to 4294967295

 2) Compile it again without "-O2" option:
 $ cc -o test test.c
 $ ./test
 $ 

It looks like "-O2" option would optimize the statement to unexpected assemble
codes:
 if (m->timenow_last - m->timenow > 0)

What I expected is that (m->timenow_last - m->timenow) should result to signed
integer -1, but it's treated as signed integer 4294967295 with "-O2" option.

To verify if latest Clang version has fixed this issue, install latest Clang
6.0 compiler to try:
# pkg install clang-devel-6.0.d20170920_1

After installlation, compile it with new Clang compiler:
 1) $ clang-devel -O2 -o test test.c
    $ ./test
    $ mDNSPlatformRawTime went backwards by 4294967295 ticks; setting
correction factor to 4294967295

 2) $ clang-devel -o test test.c
    $ ./test
    $
Got the same result as previous.

Use gcc version 4.9.4, I didn't encounter this problem.

Build Date & Hardware:
I can reproduced this issue when compiled test.c with
Clang 3.8.0, Clang 6.0 on FreeBSD 11.0 AMD64.
Apple LLVM version 9.0.0 (clang-900.0.38) on macOS 10.13, MacBook Air.</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>