Opened 12 years ago

Closed 6 years ago

#4946 closed Bugs (fixed)

Uninitialized variable warning in lexical_cast.hpp

Reported by: Rich McKeever <richmckeever@…> Owned by: Antony Polukhin
Milestone: Boost 1.61.0 Component: lexical_cast
Version: Boost 1.55.0 Severity: Problem
Keywords: Cc:

Description

Target lexical_cast(param_type, CharT*, size_t) [Line 1150] has an uninitialized variable called "result" that is immediately read with a stream extraction operator. GCC warns about the variable being uninitialized. The warning is suppressed for msvc. The attached patch suppresses it on GCC 4.3.0 on Mac OS 10.6.4.

Attachments (2)

lexical_cast.hpp.diff (721 bytes ) - added by Rich McKeever <richmckeever@…> 12 years ago.
lexical_cast.diff (368 bytes ) - added by Antony Polukhin 11 years ago.
New fix

Download all attachments as: .zip

Change History (15)

by Rich McKeever <richmckeever@…>, 12 years ago

Attachment: lexical_cast.hpp.diff added

comment:1 by Vicente Botet <vicente.botet@…>, 12 years ago

The attached patch will generate another self assignment warning.

comment:2 by Antony Polukhin, 11 years ago

Owner: changed from nasonov to Antony Polukhin
Status: newassigned

comment:3 by Antony Polukhin, 11 years ago

Some time ago, I saw that bug. But now I can not reproduce it.
If you still get this bug, please test the following patch, and write about the results

by Antony Polukhin, 11 years ago

Attachment: lexical_cast.diff added

New fix

comment:4 by Antony Polukhin, 11 years ago

Resolution: worksforme
Status: assignedclosed

A lot of things have changed in lexical_cast since 1.44 version. Now, I can not reproduce this warning.

comment:5 by eduardorp1@…, 8 years ago

Resolution: worksforme
Status: closedreopened
Version: Boost 1.44.0Boost 1.55.0

Still happens on Mac OS X 10.9.

Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0

In file included from /Users/epoyart/projects/(...)/main.cpp:15:
In file included from /opt/local/include/boost/program_options.hpp:15:
In file included from /opt/local/include/boost/program_options/options_description.hpp:13:
In file included from /opt/local/include/boost/program_options/value_semantic.hpp:14:
/opt/local/include/boost/lexical_cast.hpp:2377:24: warning: variable 'result' may be uninitialized when used here [-Wconditional-uninitialized]
                return result;
                       ^~~~~~
/opt/local/include/boost/lexical_cast.hpp:2543:29: note: in instantiation of member function 'boost::detail::lexical_cast_do_cast<int, std::__1::basic_string<char> >::lexical_cast_impl' requested here
        return caster_type::lexical_cast_impl(arg);
                            ^
In file included from /Users/epoyart/projects/(...)/main.cpp:15:
In file included from /opt/local/include/boost/program_options.hpp:15:
In file included from /opt/local/include/boost/program_options/options_description.hpp:13:
In file included from /opt/local/include/boost/program_options/value_semantic.hpp:418:
/opt/local/include/boost/program_options/detail/value_semantic.hpp:89:21: note: in instantiation of function template specialization 'boost::lexical_cast<int, std::__1::basic_string<char> >' requested here
            v = any(lexical_cast<T>(s));
                    ^
/opt/local/include/boost/program_options/detail/value_semantic.hpp:170:13: note: in instantiation of function template specialization 'boost::program_options::validate<int, char>' requested here
            validate(value_store, new_tokens, (T*)0, 0);
            ^
/opt/local/include/boost/program_options/detail/value_semantic.hpp:185:33: note: in instantiation of member function 'boost::program_options::typed_value<int, char>::xparse' requested here
        typed_value<T>* r = new typed_value<T>(v);
                                ^
/Users/epoyart/projects/(...)/main.cpp:39:24: note: in instantiation of function template specialization 'boost::program_options::value<int>' requested here
        ("num_images", value<int>(&numImages)->default_value(0), "number of images to convert; 0 = all")
                       ^
In file included from /Users/epoyart/projects/(...)/main.cpp:15:
In file included from /opt/local/include/boost/program_options.hpp:15:
In file included from /opt/local/include/boost/program_options/options_description.hpp:13:
In file included from /opt/local/include/boost/program_options/value_semantic.hpp:14:
/opt/local/include/boost/lexical_cast.hpp:2366:30: note: initialize the variable 'result' to silence this warning
                Target result;               
                             ^
                              = 0
1 warning generated.

comment:6 by Maxim.Yanchenko@…, 8 years ago

same in boost 1.56.0 compiled with gcc4.8.2, lexical_cast.hpp:2314:16:

    template <typename Target, typename Source>
    inline Target lexical_cast(const Source &arg)
    {
        Target result;     // <<<<< line 2314

        if (!boost::conversion::detail::try_lexical_convert(arg, result))
            BOOST_LCAST_THROW_BAD_CAST(Source, Target);

        return result;
    }

which I believe goes down to the line 2083:

            static inline bool try_convert(const Source& arg, Target& result) {
                i_interpreter_type i_interpreter;

                // Disabling ADL, by directly specifying operators.
                if (!(i_interpreter.operator <<(arg)))
                    return false;     // <<<<< line 2083

P.S. It looks like a GCC bug, to me, as I see in the preprocessed output that throw_exception is noreturn so GCC should see that the whole lexical_cast function is noreturn if try_convert returns false:

template<class E> __attribute__ ((__noreturn__)) inline void throw_exception( E const & e )

comment:7 by ytizikc@…, 7 years ago

Interestingly I've been able to reproduce this for boost 1.56 on Linux running gcc 4.8.2 when using the -O0 -Og -g3 flags, but not when using just plain -O0 (not follows by -Og), nor with -O3 (naturally without -Og -g3) used.

comment:8 by yitzikc@…, 7 years ago

Interestingly I've been able to reproduce this for boost 1.56 on Linux running gcc 4.8.2 when using the -O0 -Og -g3 flags, but not when using just plain -O0 (not follows by -Og), nor with -O3 (naturally without -Og -g3) used.

comment:9 by Antony Polukhin, 7 years ago

comment:10 by anonymous, 6 years ago

I'm also seeing this issue with gcc 4.8.4 and boost 1.56 but only when -Og is used.

For me the warnings are reported at the point where lexical cast is used rather than in the boost header, so our usual warning suppressions don't do anything. I've tried adding the suggested #pragma GCC diagnostic push stuff around our include of boost/lexical_cast.hpp but it also has no effect.

The only way I've found to get around it is to disable -Wuninitialized in debug builds.

comment:11 by martin.braun@…, 6 years ago

Can still confirm with gcc 5.4.0 and Boost 1.58:

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <vector>
#include <string>

int main(void)
{
    std::vector<int> v;
    std::string s("5");
    v.push_back(boost::lexical_cast<int>(s));
    return 0;
}
$ g++ -Og -Wall warning.cpp
warning.cpp: In function ‘int main()’:
warning.cpp:10:16: warning: ‘result’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     v.push_back(boost::lexical_cast<int>(s));

comment:12 by Martin, 6 years ago

Is there a reason not to explicitly default-initialize 'result'? One of the earlier patches was to do just that. Basically, the safe equivalent of

Target result = Target();

comment:13 by Antony Polukhin, 6 years ago

Milestone: To Be DeterminedBoost 1.61.0
Resolution: fixed
Status: reopenedclosed

Fixed in e6adec14, merged to master and released in Boost 1.61

Note: See TracTickets for help on using tickets.