https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118808
Bug ID: 118808 Summary: operator new allocates wrong size for object of a template class with two variants Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: bugzi...@little-bat.de Target Milestone: --- Created attachment 60433 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60433&action=edit gcc version, invocation and .ii file compiling with arm-none-eabi for cortex-m0plus compiling for release with -O3 is faulty. compiling for debug with -Og is correct. when allocating an object of a template class, which has two separate definitions for two variants A and B separated by std::enable_if, where variant B is based on variant A, operator new() only allocates the size for variant A when instantiating an object of variant B. the correct constructor is called which overwrites the end of the allocated memory thus corrupting the heap. in this example: virtual base class Canvas template<ColorMode CM, typename = void> class Pixmap; // variant A without attributes: template<ColorMode CM> class Pixmap<CM, typename std::enable_if_t<is_direct_color(CM)>> : public Canvas {...} // variant B with attributes is based on variant A without attributes: template<ColorMode CM> class Pixmap<CM, typename std::enable_if_t<is_attribute_mode(CM)>> final : public Pixmap<ColorMode(get_attrmode(CM))> {...} the invocation is in line 8286 of the .ii file