Boost C++ Libraries: Ticket #4946: Uninitialized variable warning in lexical_cast.hpp https://svn.boost.org/trac10/ticket/4946 <p> 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. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4946 Trac 1.4.3 Rich McKeever <richmckeever@…> Tue, 07 Dec 2010 19:01:43 GMT attachment set https://svn.boost.org/trac10/ticket/4946 https://svn.boost.org/trac10/ticket/4946 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">lexical_cast.hpp.diff</span> </li> </ul> Ticket Vicente Botet <vicente.botet@…> Tue, 07 Dec 2010 19:28:59 GMT <link>https://svn.boost.org/trac10/ticket/4946#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4946#comment:1</guid> <description> <p> The attached patch will generate another self assignment warning. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Fri, 13 May 2011 09:56:48 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/4946#comment:2 https://svn.boost.org/trac10/ticket/4946#comment:2 <ul> <li><strong>owner</strong> changed from <span class="trac-author">nasonov</span> to <span class="trac-author">Antony Polukhin</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Antony Polukhin Sat, 14 May 2011 07:58:30 GMT <link>https://svn.boost.org/trac10/ticket/4946#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4946#comment:3</guid> <description> <p> Some time ago, I saw that bug. But now I can not reproduce it.<br /> If you still get this bug, please test the following patch, and write about the results </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sat, 14 May 2011 07:59:16 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4946 https://svn.boost.org/trac10/ticket/4946 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">lexical_cast.diff</span> </li> </ul> <p> New fix </p> Ticket Antony Polukhin Thu, 08 Dec 2011 15:39:08 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/4946#comment:4 https://svn.boost.org/trac10/ticket/4946#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">worksforme</span> </li> </ul> <p> A lot of things have changed in lexical_cast since 1.44 version. Now, I can not reproduce this warning. </p> Ticket eduardorp1@… Tue, 17 Jun 2014 19:06:36 GMT status, version changed; resolution deleted https://svn.boost.org/trac10/ticket/4946#comment:5 https://svn.boost.org/trac10/ticket/4946#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.44.0</span> → <span class="trac-field-new">Boost 1.55.0</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">worksforme</span> </li> </ul> <p> Still happens on Mac OS X 10.9. </p> <pre class="wiki">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&lt;int, std::__1::basic_string&lt;char&gt; &gt;::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&lt;int, std::__1::basic_string&lt;char&gt; &gt;' requested here v = any(lexical_cast&lt;T&gt;(s)); ^ /opt/local/include/boost/program_options/detail/value_semantic.hpp:170:13: note: in instantiation of function template specialization 'boost::program_options::validate&lt;int, char&gt;' 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&lt;int, char&gt;::xparse' requested here typed_value&lt;T&gt;* r = new typed_value&lt;T&gt;(v); ^ /Users/epoyart/projects/(...)/main.cpp:39:24: note: in instantiation of function template specialization 'boost::program_options::value&lt;int&gt;' requested here ("num_images", value&lt;int&gt;(&amp;numImages)-&gt;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. </pre> Ticket Maxim.Yanchenko@… Tue, 09 Sep 2014 10:48:12 GMT <link>https://svn.boost.org/trac10/ticket/4946#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4946#comment:6</guid> <description> <p> same in boost 1.56.0 compiled with gcc4.8.2, lexical_cast.hpp:2314:16: </p> <div class="wiki-code"><div class="code"><pre> <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">Target</span><span class="p">,</span> <span class="k">typename</span> <span class="n">Source</span><span class="o">&gt;</span> <span class="kr">inline</span> <span class="n">Target</span> <span class="n">lexical_cast</span><span class="p">(</span><span class="k">const</span> <span class="n">Source</span> <span class="o">&amp;</span><span class="n">arg</span><span class="p">)</span> <span class="p">{</span> <span class="n">Target</span> <span class="n">result</span><span class="p">;</span> <span class="c1">// &lt;&lt;&lt;&lt;&lt; line 2314</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">boost</span><span class="o">::</span><span class="n">conversion</span><span class="o">::</span><span class="n">detail</span><span class="o">::</span><span class="n">try_lexical_convert</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">result</span><span class="p">))</span> <span class="n">BOOST_LCAST_THROW_BAD_CAST</span><span class="p">(</span><span class="n">Source</span><span class="p">,</span> <span class="n">Target</span><span class="p">);</span> <span class="k">return</span> <span class="n">result</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><p> which I believe goes down to the line 2083: </p> <div class="wiki-code"><div class="code"><pre> <span class="k">static</span> <span class="kr">inline</span> <span class="kt">bool</span> <span class="nf">try_convert</span><span class="p">(</span><span class="k">const</span> <span class="n">Source</span><span class="o">&amp;</span> <span class="n">arg</span><span class="p">,</span> <span class="n">Target</span><span class="o">&amp;</span> <span class="n">result</span><span class="p">)</span> <span class="p">{</span> <span class="n">i_interpreter_type</span> <span class="n">i_interpreter</span><span class="p">;</span> <span class="c1">// Disabling ADL, by directly specifying operators.</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">i_interpreter</span><span class="p">.</span><span class="k">operator</span> <span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">arg</span><span class="p">)))</span> <span class="k">return</span> <span class="nb">false</span><span class="p">;</span> <span class="c1">// &lt;&lt;&lt;&lt;&lt; line 2083</span> </pre></div></div><p> P.S. It looks like a GCC bug, to me, as I see in the preprocessed output that <code>throw_exception</code> is noreturn so GCC should see that the whole <code>lexical_cast</code> function is noreturn if <code>try_convert</code> returns false: </p> <div class="wiki-code"><div class="code"><pre><span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">E</span><span class="o">&gt;</span> <span class="n">__attribute__</span> <span class="p">((</span><span class="n">__noreturn__</span><span class="p">))</span> <span class="kr">inline</span> <span class="kt">void</span> <span class="n">throw_exception</span><span class="p">(</span> <span class="n">E</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">e</span> <span class="p">)</span> </pre></div></div> </description> <category>Ticket</category> </item> <item> <author>ytizikc@…</author> <pubDate>Thu, 14 May 2015 22:44:11 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4946#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4946#comment:7</guid> <description> <p> 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. </p> </description> <category>Ticket</category> </item> <item> <author>yitzikc@…</author> <pubDate>Thu, 14 May 2015 22:53:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4946#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4946#comment:8</guid> <description> <p> 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. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Fri, 25 Sep 2015 17:51:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4946#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4946#comment:9</guid> <description> <p> Another attempt to <a class="ext-link" href="https://github.com/boostorg/lexical_cast/commit/038e80ec7a7f34f61379c8885f386b230ef09624"><span class="icon">​</span>fix the issue in develop branch</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 22 Jul 2016 09:00:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4946#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4946#comment:10</guid> <description> <p> I'm also seeing this issue with gcc 4.8.4 and boost 1.56 but only when -Og is used. </p> <p> 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. </p> <p> The only way I've found to get around it is to disable -Wuninitialized in debug builds. </p> </description> <category>Ticket</category> </item> <item> <author>martin.braun@…</author> <pubDate>Tue, 29 Nov 2016 02:25:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4946#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4946#comment:11</guid> <description> <p> Can still confirm with gcc 5.4.0 and Boost 1.58: </p> <pre class="wiki">#include &lt;boost/lexical_cast.hpp&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; int main(void) { std::vector&lt;int&gt; v; std::string s("5"); v.push_back(boost::lexical_cast&lt;int&gt;(s)); return 0; } </pre><pre class="wiki">$ 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&lt;int&gt;(s)); </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Martin</dc:creator> <pubDate>Tue, 13 Dec 2016 18:00:47 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4946#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4946#comment:12</guid> <description> <p> 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 </p> <pre class="wiki">Target result = Target(); </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Fri, 27 Jan 2017 18:58:35 GMT</pubDate> <title>status, milestone changed; resolution set https://svn.boost.org/trac10/ticket/4946#comment:13 https://svn.boost.org/trac10/ticket/4946#comment:13 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.61.0</span> </li> </ul> <p> Fixed in <a class="ext-link" href="https://github.com/boostorg/lexical_cast/commit/e6adec141147c6ca3acf289342bff9bb28b0d675"><span class="icon">​</span>e6adec14</a>, merged to master and released in Boost 1.61 </p> Ticket