<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 - False positive on reading undefined value from streams"
   href="https://bugs.llvm.org/show_bug.cgi?id=34817">34817</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>False positive on reading undefined value from streams
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Static Analyzer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>kremenek@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>howard.hinnant@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This code gets an analyzer warning:

#include <cassert>
#include <sstream>

int
main()
{
    std::istringstream in{""};
    char c;
    in >> c;
    assert(!in.fail());
    return int{c};
}

scan-build: Using '/Users/howardhinnant/Development/llvm_build/bin/clang-6.0'
for static analysis
test.cpp:492:5: warning: Undefined or garbage value returned to caller
    return int{c};
    ^~~~~~~~~~~~~
1 warning generated.
scan-build: 1 bug found.
scan-build: Run 'scan-view
/var/folders/7d/bq37f_s53dbgw_z48smv9q9m0000gn/T/scan-build-2017-10-03-121247-59896-1'
to examine bug reports.

It would be nice if the analyzer could detect that c can't be garbage because
in.fail() is checked and if true never reaches the use of c.

Even better would be this:

#include <cassert>
#include <sstream>

int
main()
{
    std::istringstream in{""};
    in.exceptions(std::ios::failbit | std::ios::badbit);
    assert(((std::ios::failbit | std::ios::badbit) & in.exceptions()) ==
            (std::ios::failbit | std::ios::badbit));
    char c;
    in >> c;
    return int{c};
}

c can not be used with garbage, proven by the settings in in.exceptions().  So
no warning on the above either.</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>