I wrote a clang tool based on libtooling.
<br>This tool will insert some code into the source file through Rewriter.
<br><br>I encounted SIGBUS when insert a sentence after a SourceLocation.
<br>I debugged using CLion and found this SIGBUS happend here:
<br><br>Rewriter.cpp, Line 257 @Rewriter::InsertText(SourceLocation Loc, String Ref Str, bool InsertAfter, bool IndentNewLine)
<br><br>the code writes:
<br>while(isWhitespaceExceptNL(MB[i]) //the<b> MB[i]</b> will cause SIGBUS sometimes.
<br><br>The MB is a instance of StringRef
<br><br>I encounted this problem only for the main.c and utils.c from graph500-2.1.4, in the mpi folder.
<br><br>when processing main.c, the problem happens when trying to insert a '\n' after the for statement in line 45:
<br>for(i=0;i<n;++i)  tmp+=x[i];
<br>-------------------^-----//try to insert here
<br><br>can anyone tells me how to check the problem?
<br><br>I used gcc4.8.4, Linux Mint 17.3, kernel version:3.13.0-24, core i7 4710M<br><br>here is my code:<br>(if I delete the code before <pre style="background-color:#ffffff;color:#000000;font-family:'DejaVu Sans Mono';font-size:9.0pt;"><span style="color:#000080;font-weight:bold;">if</span>(!isa<<span style="color:#008080;">CompoundStmt</span>>(stmt))</pre> then the problem disappears)<br><br><pre style="background-color:#ffffff;color:#000000;font-family:'DejaVu Sans Mono';font-size:9.0pt;"><span style="color:#1f542e;font-weight:bold;">bool </span><span style="color:#008080;">StmtHandler</span>::InsertText(<span style="color:#008080;">Stmt </span>*stmt, <span style="color:#371f80;">string </span>toInsert) {<br>    <span style="color:#808080;font-style:italic;">//get rewriter from buffer<br></span><span style="color:#808080;font-style:italic;">    </span><span style="color:#008080;">SourceLocation </span>slc = stmt->getLocStart();<br>    <span style="color:#000080;font-weight:bold;">unsigned int </span>fsl = <span style="color:#660e7a;">TheContext</span>.getFullLoc(slc).getSpellingColumnNumber();<br><br>    <span style="color:#371f80;">string </span>filename = <span style="color:#660e7a;">TheContext</span>.getSourceManager().getFilename(slc).str();<br>    <span style="color:#371f80;">FileIdMap</span>::<span style="color:#371f80;">iterator </span>index = <span style="color:#660e7a;">fileMap</span>.find(filename);<br>    <span style="color:#008080;">FileID </span>id = <span style="color:#660e7a;">TheRewriter</span>.getSourceMgr().getFileID(slc);<br>    <span style="color:#000080;font-weight:bold;">if</span>(index <span style="color:#008080;">== </span><span style="color:#660e7a;">fileMap</span>.end()){<br>        <span style="color:#660e7a;">fileMap</span><span style="color:#008080;">[</span>filename<span style="color:#008080;">] </span>= id;<br>        <span style="color:#808080;font-style:italic;">//we need to judge whether the file was instrumented already<br></span><span style="color:#808080;font-style:italic;">        </span><span style="color:#000080;font-weight:bold;">const </span><span style="color:#008080;">RewriteBuffer </span>&buffer = <span style="color:#660e7a;">TheRewriter</span>.getEditBuffer(id);<br>        <span style="color:#371f80;">string </span>content(buffer.begin(), buffer.end());<br>        <span style="color:#371f80;">size_t </span>pos = content.find(<span style="color:#008000;font-weight:bold;">'</span><span style="color:#000080;font-weight:bold;">\n</span><span style="color:#008000;font-weight:bold;">'</span>, <span style="color:#0000ff;">0</span>);<br>        <span style="color:#371f80;">string </span>start = content.substr(<span style="color:#0000ff;">0</span>, pos + <span style="color:#0000ff;">1</span>);<br>        <span style="color:#371f80;">string </span>comp = <span style="color:#008000;font-weight:bold;">"//instrumented "</span><span style="color:#008080;">+</span><span style="color:#660e7a;">namespace_origin</span><span style="color:#008080;">+</span><span style="color:#008000;font-weight:bold;">"</span><span style="color:#000080;font-weight:bold;">\n</span><span style="color:#008000;font-weight:bold;">"</span>;<br>        <span style="color:#808080;font-style:italic;">//if we can not write the file, we regard it as instrumented<br></span><span style="color:#808080;font-style:italic;">        </span><span style="color:#008080;">std</span>::<span style="color:#008080;">error_code </span>OutErrorInfo;<br>        <span style="color:#008080;">std</span>::<span style="color:#008080;">error_code </span>ok;<br>        <span style="color:#008080;">llvm</span>::<span style="color:#008080;">raw_fd_ostream </span>tmpFile(filename, OutErrorInfo, <span style="color:#008080;">llvm</span>::<span style="color:#008080;">sys</span>::<span style="color:#008080;">fs</span>::<span style="color:#660e7a;font-weight:bold;font-style:italic;">F_RW</span>);<br>        <span style="color:#000080;font-weight:bold;">if</span>(OutErrorInfo <span style="color:#008080;">!= </span>ok){<br>            <span style="color:#660e7a;">fileInstrumented</span><span style="color:#008080;">[</span>id<span style="color:#008080;">] </span>= <span style="color:#1f542e;font-weight:bold;">true</span>;<br>            <span style="color:#000080;font-weight:bold;">return </span><span style="color:#1f542e;font-weight:bold;">false</span>;<br>        }<br>        <span style="color:#808080;font-style:italic;">//see if this file was ever instrumented<br></span><span style="color:#808080;font-style:italic;">        </span><span style="color:#000080;font-weight:bold;">if</span>(start <span style="color:#008080;">!= </span>comp){<br>            <span style="color:#660e7a;">fileInstrumented</span><span style="color:#008080;">[</span>id<span style="color:#008080;">] </span>= <span style="color:#1f542e;font-weight:bold;">false</span>;<br>            <span style="color:#660e7a;">TheRewriter</span>.InsertText(<span style="color:#660e7a;">TheRewriter</span>.getSourceMgr().getLocForStartOfFile(id), comp,<span style="color:#1f542e;font-weight:bold;">false</span>);<br>        }<span style="color:#000080;font-weight:bold;">else</span>{<br>            <span style="color:#660e7a;">fileInstrumented</span><span style="color:#008080;">[</span>id<span style="color:#008080;">] </span>= <span style="color:#1f542e;font-weight:bold;">true</span>;<br>            <span style="color:#008080;">llvm</span>::errs()<span style="color:#008080;"><<</span><span style="color:#008000;font-weight:bold;">"file "</span><span style="color:#008080;"><<</span>filename<span style="color:#008080;"><<</span><span style="color:#008000;font-weight:bold;">" was ignored.</span><span style="color:#000080;font-weight:bold;">\n</span><span style="color:#008000;font-weight:bold;">"</span>;<br>            <span style="color:#000080;font-weight:bold;">return </span><span style="color:#1f542e;font-weight:bold;">false</span>;<br>        }<br><br>    }<span style="color:#000080;font-weight:bold;">else</span>{<br>        <span style="color:#000080;font-weight:bold;">if</span>(<span style="color:#660e7a;">fileInstrumented</span><span style="color:#008080;">[</span>id<span style="color:#008080;">]</span>)<br>            <span style="color:#000080;font-weight:bold;">return </span><span style="color:#1f542e;font-weight:bold;">false</span>;<br>    }<br><br>    <span style="color:#000080;font-weight:bold;">if</span>(!isa<<span style="color:#008080;">CompoundStmt</span>>(stmt)){<br>        <span style="color:#008080;">SourceLocation </span>ST = stmt->getLocStart();<br><span style="color:#808080;font-style:italic;"></span><span style="color:#808080;font-style:italic;"><br></span><span style="color:#808080;font-style:italic;">        </span><span style="color:#660e7a;">TheRewriter</span>.InsertText(ST, <span style="color:#008000;font-weight:bold;">"{</span><span style="color:#000080;font-weight:bold;">\n</span><span style="color:#008000;font-weight:bold;">"</span>, <span style="color:#1f542e;font-weight:bold;">true</span>, <span style="color:#1f542e;font-weight:bold;">true</span>);<br>        <span style="color:#660e7a;">TheRewriter</span>.InsertText(ST, toInsert <span style="color:#008080;">+ </span><span style="color:#008000;font-weight:bold;">"</span><span style="color:#000080;font-weight:bold;">\n</span><span style="color:#008000;font-weight:bold;">"</span>, <span style="color:#1f542e;font-weight:bold;">true</span>, <span style="color:#1f542e;font-weight:bold;">true</span>);<span style="color:#808080;font-style:italic;"><br></span><span style="color:#808080;font-style:italic;">        </span><span style="color:#008080;">SourceLocation </span><span style="background-color:#ffe4ff;">END</span> = stmt->getLocEnd();<span style="color:#808080;font-style:italic;"><br>       </span><span style="color:#000080;font-weight:bold;">int </span>offset = <span style="color:#008080;">Lexer</span>::MeasureTokenLength(<span style="background-color:#e4e4ff;">END</span>,<br>                                               <span style="color:#660e7a;">TheRewriter</span>.getSourceMgr(),<br>                                               <span style="color:#660e7a;">TheRewriter</span>.getLangOpts()) + <span style="color:#0000ff;">1</span>;<br><br>        <span style="color:#008080;">SourceLocation </span>END1 = <span style="background-color:#e4e4ff;">END</span>.getLocWithOffset(offset);<br>        <span style="color:#660e7a;">TheRewriter</span>.InsertText(END1, <span style="color:#008000;font-weight:bold;">"</span><span style="color:#000080;font-weight:bold;">\n</span><span style="color:#008000;font-weight:bold;">}"</span>, <span style="color:#1f542e;font-weight:bold;">true</span>, <span style="color:#1f542e;font-weight:bold;">true</span>);<br>    }<br>    <span style="color:#000080;font-weight:bold;">else</span>{<br>        <span style="color:#008080;">CompoundStmt </span>*compstmt = cast<<span style="color:#008080;">CompoundStmt</span>>(stmt);<br>        <span style="color:#008080;">SourceLocation </span>compLoc = compstmt->getLocStart();<br>        <span style="color:#660e7a;">TheRewriter</span>.InsertText(compLoc.getLocWithOffset(<span style="color:#0000ff;">1</span>),<span style="color:#008000;font-weight:bold;">"</span><span style="color:#000080;font-weight:bold;">\n</span><span style="color:#008000;font-weight:bold;">"</span><span style="color:#008080;">+</span>toInsert<span style="color:#008080;">+</span><span style="color:#008000;font-weight:bold;">"</span><span style="color:#000080;font-weight:bold;">\n</span><span style="color:#008000;font-weight:bold;">"</span>, <span style="color:#1f542e;font-weight:bold;">true</span>, <span style="color:#1f542e;font-weight:bold;">true</span>);<br>    }<br>    <span style="color:#000080;font-weight:bold;">return </span><span style="color:#1f542e;font-weight:bold;">true</span>;<br>}</pre>