From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22873 invoked by alias); 9 Oct 2014 05:55:00 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 22863 invoked by uid 89); 9 Oct 2014 05:55:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail1.windriver.com Received: from mail1.windriver.com (HELO mail1.windriver.com) (147.11.146.13) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Thu, 09 Oct 2014 05:54:59 +0000 Received: from ALA-HCA.corp.ad.wrs.com (ala-hca.corp.ad.wrs.com [147.11.189.40]) by mail1.windriver.com (8.14.9/8.14.5) with ESMTP id s995svEq013368 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL) for ; Wed, 8 Oct 2014 22:54:57 -0700 (PDT) Received: from [128.224.162.141] (128.224.162.141) by ALA-HCA.corp.ad.wrs.com (147.11.189.50) with Microsoft SMTP Server id 14.3.174.1; Wed, 8 Oct 2014 22:54:57 -0700 Message-ID: <54362331.8060202@windriver.com> Date: Thu, 09 Oct 2014 05:55:00 -0000 From: Rongqing Li User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 MIME-Version: 1.0 To: Subject: gcc 4.9.1 Bug or not Content-Type: multipart/mixed; boundary="------------060908050709050502020008" X-IsSubscribed: yes X-SW-Source: 2014-10/txt/msg00639.txt.bz2 --------------060908050709050502020008 Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit Content-length: 1720 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:~# ################## 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) ---------------------------------------- #include #include #include static int name_cmp(const char *a, const char *b) { /* compare strings a and b, but only upto ',' in a */ while (*a && *b && *a != ',' && *a == *b) a++, b++; if (!*b && (!*a || *a == ',')) return 0; if (!*b) return 1; if (!*a || *a == ',') return -1; return *a - *b; } char *add_name(char *old, const char *add) { int len = strlen(add) + 2; char *new; char *cp; if (old) len += strlen(old); new = (char *)malloc(len); if (!new) return NULL; cp = old; while (cp && *cp && name_cmp(cp, add) < 0) { /* step cp forward over a name */ char *e = strchr(cp, ','); if (e) cp = e+1; else cp = cp + strlen(cp); } strncpy(new, old, cp-old); new[cp-old] = 0; if (cp != old && !*cp) strcat(new, ","); strcat(new, add); if (cp && *cp) { strcat(new, ","); strcat(new, cp); } return new; } int main() { printf("%s\n", add_name(0,"192.168.1.1")); return 0; } --------------060908050709050502020008 Content-Type: text/x-csrc; name="test.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="test.c" Content-length: 966 #include #include #include static int name_cmp(const char *a, const char *b) { /* compare strings a and b, but only upto ',' in a */ while (*a && *b && *a != ',' && *a == *b) a++, b++; if (!*b && (!*a || *a == ',')) return 0; if (!*b) return 1; if (!*a || *a == ',') return -1; return *a - *b; } char *add_name(char *old, const char *add) { int len = strlen(add) + 2; char *new; char *cp; if (old) len += strlen(old); new = (char *)malloc(len); if (!new) return NULL; cp = old; while (cp && *cp && name_cmp(cp, add) < 0) { /* step cp forward over a name */ char *e = strchr(cp, ','); if (e) cp = e+1; else cp = cp + strlen(cp); } strncpy(new, old, cp-old); new[cp-old] = 0; if (cp != old && !*cp) strcat(new, ","); strcat(new, add); if (cp && *cp) { strcat(new, ","); strcat(new, cp); } return new; } int main() { printf("%s\n", add_name(0,"192.168.1.1")); return 0; } --------------060908050709050502020008--