https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68100

            Bug ID: 68100
           Summary: runtime segfault ARM boost::regex_replace
                    -fsanitize=undefined member access within misaligned
                    address
           Product: gcc
           Version: 5.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yba at tkos dot co.il
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org
  Target Milestone: ---

Created attachment 36587
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=36587&action=edit
crosstool-ng .config file for arm-mxs-linux-gnueabihf

Command line:
arm-mxs-linux-gnueabihf-g++ \
        -Wall -Wextra \
        -fsanitize=undefined \
        -O3 \
        --sysroot=${STAGING} \
        -L ${STAGING}/usr/lib \
        -l boost_regex \
        -o main main.cpp

Code:
#include <stdio.h>
#include <boost/regex.hpp>
#include <string>

int main(int argc, char* argv[]) {
    boost::regex two_apost_to_quote;
    two_apost_to_quote.assign("( |^)(''|' ')"); // change '' or ' ' to "
    std::string two_apost_to_quote_fmt = "\\1\"";

    std::string input = "hi there";
    printf("before: %s\n", input.c_str());
    std::string output = boost::regex_replace(input, two_apost_to_quote,
        two_apost_to_quote_fmt, boost::match_default | boost::format_all);
    printf("after: %s\n", output.c_str());
}

Runtime execution:
before: hi there
/env/dev/filesystem/rootfs_images/mxs/sysroot/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:188:27:
runtime error: member call on misaligned address 0x952b4cc2 for type 'struct
perl_matcher', which requires 4 byte alignment
0x952b4cc2: note: pointer points here
<memory cannot be printed>
/env/dev/filesystem/rootfs_images/mxs/sysroot/usr/include/boost/regex/v4/perl_matcher_non_recursive.hpp:349:54:
runtime error: member access within misaligned address 0x952b4cc2 for type
'struct perl_matcher', which requires 4 byte alignment
0x952b4cc2: note: pointer points here
<memory cannot be printed>
Segmentation fault

Comments:
1. The space in the string "input" is required

2. Boost version is boost-1.58.0, built with GCC 5.1.0 built with crosstool-ng
using attached .config

3. The problem does not happen with the same GCC 5.1.0 and boost version built
using crosstool-ng with a similar .config

4. The runtime platform is i.MX6

5. Removing the optimization from the command line prevents the segfault

Reply via email to