<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugzilla.yoctoproject.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.6"
          urlbase="https://bugzilla.yoctoproject.org/"
          
          maintainer="it-coreprojects-helpdesk@linuxfoundation.org"
>

    <bug>
          <bug_id>6743</bug_id>
          
          <creation_ts>2014-09-19 09:22:08 +0000</creation_ts>
          <short_desc>Segmentation fault with C test code when using -O2 option</short_desc>
          <delta_ts>2015-01-15 12:24:09 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>11</classification_id>
          <classification>Runtime</classification>
          <product>General Runtime</product>
          <component>General Runtime</component>
          <version>1.7</version>
          <rep_platform>x86</rep_platform>
          <op_sys>Multiple</op_sys>
          <bug_status>VERIFIED</bug_status>
          <resolution>NOTABUG</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>Medium+</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>1.7 M4</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Yi Zhao">yi.zhao</reporter>
          <assigned_to name="LiRongQing">rongqing.li</assigned_to>
          <cc>alejandro.hernandez</cc>
    
    <cc>alexandru.c.georgescu</cc>
    
    <cc>ke.zou</cc>
    
    <cc>koen</cc>
    
    <cc>raj.khem</cc>
    
    <cc>richard.purdie</cc>
    
    <cc>rongqing.li</cc>
    
    <cc>zhangle.yang</cc>
          
          <qa_contact name="Lucian Musat">georgex.l.musat</qa_contact>
          <cf_os>---</cf_os>
          <cf_regression_type>Regression (Used to work)</cf_regression_type>
          
          <cf_docchange>No (bug/feature does not impact docs)</cf_docchange>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>45705</commentid>
    <comment_count>0</comment_count>
      <attachid>2130</attachid>
    <who name="Yi Zhao">yi.zhao</who>
    <bug_when>2014-09-19 09:22:08 +0000</bug_when>
    <thetext>Created attachment 2130
test.c

Git rev: master/429802fe6691c8a70ec9735c0db4c6a4e1e1b038

The attachment is a piece of C code.
When compile it with -O2 option, a segfault occurs:
##################
root@qemux86-64:~# gcc -o test test.c
root@qemux86-64:~# ./test 
192.168.1.1
root@qemux86-64:~# 
root@qemux86-64:~# gcc -O2 -o test test.c
root@qemux86-64:~# ./test 
test[893]: segfault at 0 ip 0000000000400944 sp 00007fff87c8f0f0 error 4 in test[400000+1000]
Segmentation fault
root@qemux86-64:~#
##################

gcc version 4.9.1 (GCC)

This issue happens on all qemus and BSPs, regardless the archs.

I test it on Yocto 1.6.1, it works well:
###################
root@qemux86-64:~# gcc -o test test.c 
root@qemux86-64:~# ./test 
192.168.1.1
root@qemux86-64:~# gcc -O2 -o test test.c 
root@qemux86-64:~# ./test 
192.168.1.1
root@qemux86-64:~# 
###################

gcc version 4.8.2 (GCC)


I also test it on some linux distributions, it works well on all of them:
Ubuntu 12.04 x86_64:
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 

Ubuntu 14.04 x86_64:
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)

Fedora 20 x86 and x86_64:
gcc version 4.8.3 20140624 (Red Hat 4.8.3-1) (GCC)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>45812</commentid>
    <comment_count>1</comment_count>
    <who name="Alejandro Hernandez">alejandro.hernandez</who>
    <bug_when>2014-09-24 23:03:12 +0000</bug_when>
    <thetext>The native compiler produces the same assembly code hence the same error when optimized for any level other than &quot;1&quot;, it seems like it is a gcc-4.9 issue.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>45814</commentid>
    <comment_count>2</comment_count>
    <who name="Khem Raj">raj.khem</who>
    <bug_when>2014-09-24 23:41:11 +0000</bug_when>
    <thetext>does this same binary when run on pc segfault too?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>45835</commentid>
    <comment_count>3</comment_count>
    <who name="Saul Wold">sgw</who>
    <bug_when>2014-09-25 14:35:14 +0000</bug_when>
    <thetext>Yes the compiled binary segfaults on both the host and target as far as I understand from Alejandro, I will confirm.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46099</commentid>
    <comment_count>4</comment_count>
    <who name="Khem Raj">raj.khem</who>
    <bug_when>2014-10-04 20:33:10 +0000</bug_when>
    <thetext>Hmm although it crashes but it does not seem like a compiler issue. You might have a coding error in your application. Whats happening is that at -O2 gcc is reordering NULL pointer checks in a way that dereferencing is happening before NULL pointer check.

	strncpy(new, old, cp-old);

here if old is NULL then its undefined behavior which is what you are seeing.
and gcc assumes that old can never be NULL.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46100</commentid>
    <comment_count>5</comment_count>
    <who name="Khem Raj">raj.khem</who>
    <bug_when>2014-10-04 20:52:15 +0000</bug_when>
    <thetext>This is how you can fix it in your code

--- a.c	2014-10-04 12:28:46.918718452 -0700
+++ a.c.khem	2014-10-04 13:54:41.905347173 -0700
@@ -36,9 +36,11 @@
 		else
 			cp = cp + strlen(cp);
 	}
-
-	strncpy(new, old, cp-old);
-	new[cp-old] = 0;
+	if (old) {
+		strncpy(new, old, cp-old);
+		new[cp-old] = 0;
+	} else
+		new[0] = 0;
 
 	if (cp != old &amp;&amp; !*cp)
 		strcat(new, &quot;,&quot;);</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46207</commentid>
    <comment_count>6</comment_count>
    <who name="LiRongQing">rongqing.li</who>
    <bug_when>2014-10-09 02:28:36 +0000</bug_when>
    <thetext>If the results are different since use different Gcc parameter to building, it should be Gcc bug.

Gcc should keep the same behaviours among different versions, but 4.6.3 and 4.8.2 can work, 4.9.1 did not work...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46208</commentid>
    <comment_count>7</comment_count>
    <who name="Khem Raj">raj.khem</who>
    <bug_when>2014-10-09 02:35:41 +0000</bug_when>
    <thetext>(In reply to comment #6)
&gt; If the results are different since use different Gcc parameter to building,
&gt; it should be Gcc bug.
&gt; 
&gt; Gcc should keep the same behaviours among different versions, but 4.6.3 and
&gt; 4.8.2 can work, 4.9.1 did not work...

No. A C compiler complies to C standard and where behavior is undefined like this case it has liberty to do whatever it wishes. If your code has been relying on undefined behavior go fix the code, not the compiler.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46209</commentid>
    <comment_count>8</comment_count>
    <who name="LiRongQing">rongqing.li</who>
    <bug_when>2014-10-09 03:06:03 +0000</bug_when>
    <thetext>which line is undefined?  strncpy(new, 0, 0)? no, it should work since the length of strncpy is 0, and src of strncpy will not be used.

I think any change can make these codes work, since any change will make the
gcc optimize to change</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46210</commentid>
    <comment_count>9</comment_count>
    <who name="Khem Raj">raj.khem</who>
    <bug_when>2014-10-09 04:49:32 +0000</bug_when>
    <thetext>(In reply to comment #8)
&gt; which line is undefined?  strncpy(new, 0, 0)? no, it should work since the
&gt; length of strncpy is 0, and src of strncpy will not be used.

I could not infer this from OpenGroup standardIEEE Std 1003.1 where strncpy is defined.
can you kindly show me which standard describes strncpy behavior the way you are mentioning ?

&gt; 
&gt; I think any change can make these codes work, since any change will make the
&gt; gcc optimize to change

Not anything, you have to describe your expected behavior by guarding it with source being valid.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46211</commentid>
    <comment_count>10</comment_count>
    <who name="LiRongQing">rongqing.li</who>
    <bug_when>2014-10-09 05:20:06 +0000</bug_when>
    <thetext>From the test result, strncpy(buf, 0,0) works.
--------------------------------------
#cat aa.c

#include &lt;string.h&gt;
main()
{
	char buf[10];
	strncpy(buf, 0, 0);
}
#

#./sysroots/x86_64-linux/usr/libexec/x86_64-wrs-linux/gcc/x86_64-wrs-linux/4.9.0/x86_64-wrs-linux-gcc -O2 aa.c
#
#./a.out
#
-----------------------------------
but ./sysroots/x86_64-linux/usr/libexec/x86_64-wrs-linux/gcc/x86_64-wrs-linux/4.9.0/x86_64-wrs-linux-gcc -O2 /tmp/test.c
#
#./a.out
Segmentation fault (core dumped)
#</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46212</commentid>
    <comment_count>11</comment_count>
    <who name="LiRongQing">rongqing.li</who>
    <bug_when>2014-10-09 05:21:47 +0000</bug_when>
    <thetext>man strncpy, it says:

       A simple implementation of strncpy() might be:

           char *
           strncpy(char *dest, const char *src, size_t n)
           {
               size_t i;

               for (i = 0; i &lt; n &amp;&amp; src[i] != &apos;\0&apos;; i++)
                   dest[i] = src[i];
               for ( ; i &lt; n; i++)
                   dest[i] = &apos;\0&apos;;

               return dest;
           }</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46213</commentid>
    <comment_count>12</comment_count>
    <who name="Khem Raj">raj.khem</who>
    <bug_when>2014-10-09 05:38:46 +0000</bug_when>
    <thetext>and you think man strcpy is what standards depend upon ?

Please compare the assembly code it will generate for the example you are sighting. Its passing a direct NULL pointer, gcc is smart enough to know that
its no way representative of the original testcase.

I have explained it enough and I do not have more time for it. I am assigning this bug to you and if you feel like fixing it in gcc feel free to. if not you are also welcome to raise this with gcc community.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46215</commentid>
    <comment_count>13</comment_count>
    <who name="LiRongQing">rongqing.li</who>
    <bug_when>2014-10-09 05:47:07 +0000</bug_when>
    <thetext>OK, I will paste it to GCC community</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46219</commentid>
    <comment_count>14</comment_count>
    <who name="LiRongQing">rongqing.li</who>
    <bug_when>2014-10-09 07:19:49 +0000</bug_when>
    <thetext>https://www.mail-archive.com/gcc-bugs@gcc.gnu.org/msg433516.html</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>46220</commentid>
    <comment_count>15</comment_count>
    <who name="LiRongQing">rongqing.li</who>
    <bug_when>2014-10-09 07:49:16 +0000</bug_when>
    <thetext>GCC community think this is a testcase bug, same as  Khem Raj.
and Khem Raj&apos;s patch works.

Thanks</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>47925</commentid>
    <comment_count>16</comment_count>
    <who name="Lucian Musat">georgex.l.musat</who>
    <bug_when>2015-01-15 12:24:09 +0000</bug_when>
    <thetext>This looks like NOTABUG. Setting accordingly.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>2130</attachid>
            <date>2014-09-19 09:22:08 +0000</date>
            <delta_ts>2014-09-19 09:22:08 +0000</delta_ts>
            <desc>test.c</desc>
            <filename>test.c</filename>
            <type>text/x-csrc</type>
            <size>966</size>
            <attacher name="Yi Zhao">yi.zhao</attacher>
            
              <data encoding="base64">I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5o
PgoKc3RhdGljIGludCBuYW1lX2NtcChjb25zdCBjaGFyICphLCBjb25zdCBjaGFyICpiKQp7Cgkv
KiBjb21wYXJlIHN0cmluZ3MgYSBhbmQgYiwgYnV0IG9ubHkgdXB0byAnLCcgaW4gYSAqLwoJd2hp
bGUgKCphICYmICpiICYmICphICE9ICcsJyAmJiAqYSA9PSAqYikKCQlhKyssIGIrKzsKCWlmICgh
KmIgJiYgKCEqYSB8fCAqYSA9PSAnLCcpKQoJCXJldHVybiAwOwoJaWYgKCEqYikgcmV0dXJuIDE7
CglpZiAoISphIHx8ICphID09ICcsJykgcmV0dXJuIC0xOwoJcmV0dXJuICphIC0gKmI7Cn0KCmNo
YXIgKmFkZF9uYW1lKGNoYXIgKm9sZCwgY29uc3QgY2hhciAqYWRkKQp7CglpbnQgbGVuID0gc3Ry
bGVuKGFkZCkgKyAyOwoJY2hhciAqbmV3OwoJY2hhciAqY3A7CgoJaWYgKG9sZCkKCQlsZW4gKz0g
c3RybGVuKG9sZCk7CgoJbmV3ID0gKGNoYXIgKiltYWxsb2MobGVuKTsKCWlmICghbmV3KSAKCQly
ZXR1cm4gTlVMTDsKCgljcCA9IG9sZDsKCXdoaWxlIChjcCAmJiAqY3AgJiYgbmFtZV9jbXAoY3As
IGFkZCkgPCAwKSB7CgkJLyogc3RlcCBjcCBmb3J3YXJkIG92ZXIgYSBuYW1lICovCgkJY2hhciAq
ZSA9IHN0cmNocihjcCwgJywnKTsKCQlpZiAoZSkKCQkJY3AgPSBlKzE7CgkJZWxzZQoJCQljcCA9
IGNwICsgc3RybGVuKGNwKTsKCX0KCglzdHJuY3B5KG5ldywgb2xkLCBjcC1vbGQpOwoJbmV3W2Nw
LW9sZF0gPSAwOwoKCWlmIChjcCAhPSBvbGQgJiYgISpjcCkKCQlzdHJjYXQobmV3LCAiLCIpOwoK
CXN0cmNhdChuZXcsIGFkZCk7CgoJaWYgKGNwICYmICpjcCkgewoJCXN0cmNhdChuZXcsICIsIik7
CgkJc3RyY2F0KG5ldywgY3ApOwoJfQoKCXJldHVybiBuZXc7Cn0KCmludCBtYWluKCkKewoJcHJp
bnRmKCIlc1xuIiwgYWRkX25hbWUoMCwiMTkyLjE2OC4xLjEiKSk7CglyZXR1cm4gMDsKfSAK
</data>

          </attachment>
      

    </bug>

</bugzilla>