Boost C++ Libraries: Ticket #8958: invalid static_casts inside any_cast that trip clang's -fsanitize=undefined https://svn.boost.org/trac10/ticket/8958 <p> The implementation of boost::any::any_cast(any * operand) uses a static_cast to convert from the held type to the requested type. The preceding check doesn't (and shouldn't) cover toplevel cv-qualifiers. In the trunk version the holder's ValueType contains a toplevel const; when requesting a non-const pointer to the held value the resulting static_cast is to a non-const pointer and thus invalid. Changing the holder's ValueType to a const type was probably unintended since it would make accesses through a non const pointer invalid no matter how it is cast. In the 1.54.0 release the holder's ValueType was non-const; this still causes a problem when requesting a const pointer to the held value as adding of const qualifiers through static_cast is still invalid. </p> <p> Clang's -fsanitize=undefined is able to detect these invalid casts. This happens for the existing tests for the any library as well as for any code that makes use of program_options' variables_map. </p> <p> In addition the parts of any that use C++11 rvalue-references seem to completely ignore cv-qualifiers and thus might be able to trigger a similar behavior, but I have not tested that. </p> <p> Attached is a proposed fix that applies boost::remove_cv to the holder's ValueType as well as to the static_cast's target type, thereby avoiding any cv-qualifier mismatch or const removing casts. The addition of a const qualifier when requested happens implicitly outside of the static_cast. </p> <p> It might still be possible to create holders with a const ValueType using the C++11 only parts, but I am not sure what the intended behavior should be. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8958 Trac 1.4.3 Jannis Harder <jix@…> Fri, 02 Aug 2013 13:31:50 GMT attachment set https://svn.boost.org/trac10/ticket/8958 https://svn.boost.org/trac10/ticket/8958 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">any_fix_invalid_static_cast_using_remove_cv.patch</span> </li> </ul> Ticket Antony Polukhin Sun, 20 Oct 2013 18:28:40 GMT owner, status changed https://svn.boost.org/trac10/ticket/8958#comment:1 https://svn.boost.org/trac10/ticket/8958#comment:1 <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 Mon, 11 Aug 2014 09:31:14 GMT milestone changed https://svn.boost.org/trac10/ticket/8958#comment:2 https://svn.boost.org/trac10/ticket/8958#comment:2 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.57.0</span> </li> </ul> Ticket Antony Polukhin Mon, 11 Aug 2014 10:12:45 GMT <link>https://svn.boost.org/trac10/ticket/8958#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8958#comment:3</guid> <description> <p> Fix was applied to the <a class="ext-link" href="https://github.com/boostorg/any/commit/c92ab03ab35775b6aab30f6cdc3d95b7dd8fc5c6"><span class="icon">​</span>develop branch</a>. </p> <p> Sorry for the delay and great thanks for supplying a patch! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sat, 25 Oct 2014 09:50:08 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8958#comment:4 https://svn.boost.org/trac10/ticket/8958#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">fixed</span> </li> </ul> Ticket