Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iostreams\detail\streambuf\indirect_streambuf.hpp(440) : warning C4702: unreachable code #138

Open
pawelsopensource opened this issue Nov 25, 2021 · 3 comments

Comments

@pawelsopensource
Copy link

hi,
i'm compiling my .dll code for release without optimization (/W3 /WX /MD /Od) and msvc reports warning (error in my case):

boost-1.77.0\include\boost\iostreams\detail\streambuf\indirect_streambuf.hpp(440) : error C2220: the following warning is treated as an error
boost-1.77.0\include\boost\iostreams\detail\streambuf\indirect_streambuf.hpp(440) : warning C4702: unreachable code

as a local workaround i've added following pragma to disable_warnings.hpp

pragma warning(disable:4702) // Unreachable code

@mclow
Copy link
Contributor

mclow commented Nov 30, 2021

I'm not seeing how that code is unreachable. Can you post more info?

438:    if (shared_buffer() && gptr() != 0) {
439:        obj().seek(static_cast<off_type>(gptr() - egptr()), BOOST_IOS::cur, BOOST_IOS::in, next_);
440:        setg(0, 0, 0);
441:    }
442:    if (output_buffered())
443:        setp(out().begin(), out().end());
444:    else
445:        setp(0, 0);

@balagansky-work
Copy link
Contributor

balagansky-work commented Jul 7, 2023

This code is unreachable under at least some versions of MSVC (14.36 but not 14.35) if shared_buffer() returns false, only with certain combinations of settings.

A minimal repro with MSVC 14.36 is simply:

#include <boost/iostreams/filtering_stream.hpp>
boost::iostreams::filtering_ostream s;

Starting from an empty project's Release configuration, only the following changes from the default project settings are needed:

  • boost include dir is specified
  • Warning Level = 4
  • Whole Program Optimization = No

Since filtering_ostream's category/mode is 'output', the condition on line 438 will always be false, and both 439 and 440 would be unreachable.

Curiously, the warning is on line 440 (setg), not line 439 (immediately after the condition). I wonder if the internal mechanism doesn't have directly to do with shared_buffer always returning false due to the trait checks, but rather something about line 439 - perhaps it's seen by the compiler as always throwing, or something like that.

We also see little choice but to use the pragma workaround mentioned by OP.

@balagansky-work
Copy link
Contributor

balagansky-work added a commit to balagansky-work/boost-iostreams that referenced this issue Jul 26, 2023
This warning has been observed under more than one version. One
specific example, as described in issue boostorg#138, is on
indirect_streambuf.hpp(440) when Whole Program Optimization is off.
In this case, the line before is seen by the optimizer as always
throwing for particular instantiations of the template,
and therefore the line in question is flagged as not reachable.

MSVC team has stated this is is expected behavior.

This is a simple and hopefully good-enough fix.
mclow pushed a commit that referenced this issue Aug 3, 2023
This warning has been observed under more than one version. One
specific example, as described in issue #138, is on
indirect_streambuf.hpp(440) when Whole Program Optimization is off.
In this case, the line before is seen by the optimizer as always
throwing for particular instantiations of the template,
and therefore the line in question is flagged as not reachable.

MSVC team has stated this is is expected behavior.

This is a simple and hopefully good-enough fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants