public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly
@ 2013-08-25 14:44 shiyan2016 at 126 dot com
  2013-08-25 14:50 ` [Bug c/58240] " paolo.carlini at oracle dot com
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: shiyan2016 at 126 dot com @ 2013-08-25 14:44 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

            Bug ID: 58240
           Summary: GCC optimize strncmp when N=1 incorrectly
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shiyan2016 at 126 dot com

Hi,

I have found a weird bug of GCC. I am trying to overwrite the C 'strncmp'
function, but I get different results when N=1 and N>1, see below test case:
/*
    bug of strncmp when N=1
    gcc: 4.7.2
*/
#include <stdio.h>

int strncmp(const char *s1,const char *s2,int n){
        printf("strncmp\n");
        return 0;
}

int main() {
     const char *s1="def";

     int ret;
     printf("test\n");
     ret=strncmp(s1,"def",1);    // bug: "strncmp" is not printed!!!
     printf("test\n");
     ret=strncmp(s1,"def",2);

    return 0;
}

To reproduce:
$gcc --version
gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$gcc 1.c
$./a.out 
test
test
strncmp
$
It will print "strncmp" when N=2, but not printed when N=1. From my
overservation, it seems strncmp(*,*,1) is optimized without calling to strncmp
at all.

Please try to check the behaviour and see whether it is a front-end bug.

By the way, the same test case can work as expected with MS compiler and Intel
compiler.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
@ 2013-08-25 14:50 ` paolo.carlini at oracle dot com
  2013-08-25 14:54 ` shiyan2016 at 126 dot com
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-08-25 14:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|shiyan2016 at 126 dot com          |

--- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com> ---
I can't reproduce this.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
  2013-08-25 14:50 ` [Bug c/58240] " paolo.carlini at oracle dot com
@ 2013-08-25 14:54 ` shiyan2016 at 126 dot com
  2013-08-25 15:04 ` paolo.carlini at oracle dot com
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: shiyan2016 at 126 dot com @ 2013-08-25 14:54 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

--- Comment #2 from shiyan <shiyan2016 at 126 dot com> ---
(In reply to Paolo Carlini from comment #1)
> I can't reproduce this.

Hi,

What is your GCC version? I am using gcc4.7.2. My OS information are as below:
$lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 12.10
Release:    12.10
Codename:    quantal
$uname -a
Linux sgeng2-VirtualBox 3.5.0-27-generic #46-Ubuntu SMP Mon Mar 25 20:00:05 UTC
2013 i686 i686 i686 GNU/Linux
$

Thanks,
Shiyan


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
  2013-08-25 14:50 ` [Bug c/58240] " paolo.carlini at oracle dot com
  2013-08-25 14:54 ` shiyan2016 at 126 dot com
@ 2013-08-25 15:04 ` paolo.carlini at oracle dot com
  2013-08-25 17:56 ` glisse at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-08-25 15:04 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|shiyan2016 at 126 dot com          |

--- Comment #3 from Paolo Carlini <paolo.carlini at oracle dot com> ---
I can reproduce it with -m32. No idea if it's already fixed.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
                   ` (2 preceding siblings ...)
  2013-08-25 15:04 ` paolo.carlini at oracle dot com
@ 2013-08-25 17:56 ` glisse at gcc dot gnu.org
  2013-08-25 18:10 ` paolo.carlini at oracle dot com
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: glisse at gcc dot gnu.org @ 2013-08-25 17:56 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

--- Comment #4 from Marc Glisse <glisse at gcc dot gnu.org> ---
-fno-builtin (or some better such option)?
strncmp is a standard function, your code redefining it has undefined behavior.
gcc optimizes based on the standard behavior of the function.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
                   ` (3 preceding siblings ...)
  2013-08-25 17:56 ` glisse at gcc dot gnu.org
@ 2013-08-25 18:10 ` paolo.carlini at oracle dot com
  2013-08-25 18:12 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-08-25 18:10 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #5 from Paolo Carlini <paolo.carlini at oracle dot com> ---
Closing then.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
                   ` (4 preceding siblings ...)
  2013-08-25 18:10 ` paolo.carlini at oracle dot com
@ 2013-08-25 18:12 ` jakub at gcc dot gnu.org
  2013-08-26  2:12 ` shiyan2016 at 126 dot com
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-08-25 18:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The testcase is clearly bogus.  You are not using the result of either of the
strncmp calls, strncmp is a pure function, so it is fine not to call it at all.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
                   ` (5 preceding siblings ...)
  2013-08-25 18:12 ` jakub at gcc dot gnu.org
@ 2013-08-26  2:12 ` shiyan2016 at 126 dot com
  2013-08-26  2:17 ` shiyan2016 at 126 dot com
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: shiyan2016 at 126 dot com @ 2013-08-26  2:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

--- Comment #8 from shiyan <shiyan2016 at 126 dot com> ---
(In reply to Jakub Jelinek from comment #6)
> The testcase is clearly bogus.  You are not using the result of either of
> the strncmp calls, strncmp is a pure function, so it is fine not to call it
> at all.

Hi Jelinek,

No this is not the reason. I know what you mean. In fact, I have tried to
printf 'ret' in my real case and it will still reproduce the issue. I remove
all other code because I want to sumbit a minimal test case. You may have a try
to use the result of strncmp and check the output. Whatever, the code depends
on "N" of strncmp is crazy behaviour.

Thanks,
Shiyan


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
                   ` (6 preceding siblings ...)
  2013-08-26  2:12 ` shiyan2016 at 126 dot com
@ 2013-08-26  2:17 ` shiyan2016 at 126 dot com
  2013-08-26  6:28 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: shiyan2016 at 126 dot com @ 2013-08-26  2:17 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

--- Comment #9 from shiyan <shiyan2016 at 126 dot com> ---
(In reply to Paolo Carlini from comment #5)
> Closing then.

Hi Carlini,

I still think it is a bug. I know the test case is not practical. In fact, I
will not use such code in real case. But from compiler's side, this is a bug
and I think you may still track it and lower the "importance" if possible. It
it valid test case, though not practical. :)

Also as you mentioned -m64 can work well, I believe it is not technically
difficult to fix. Maybe the compiler front end just do something optimization
too early and removed the call to strncmp when N=1?

Thanks,
Shiyan


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
                   ` (7 preceding siblings ...)
  2013-08-26  2:17 ` shiyan2016 at 126 dot com
@ 2013-08-26  6:28 ` pinskia at gcc dot gnu.org
  2013-08-26  7:03 ` glisse at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-08-26  6:28 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

--- Comment #10 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to shiyan from comment #7)
> (In reply to Marc Glisse from comment #4)
> > -fno-builtin (or some better such option)?
> > strncmp is a standard function, your code redefining it has undefined
> > behavior. gcc optimizes based on the standard behavior of the function.
> Thank you for the suggestion. Yes, I know -fno-builtin can work around it. I
> can think of many possible ways to work around it....but whatever, it is a
> bug. As I known, redefining a function (override?) is not supported in
> standard C, but first most of compiler supports this including GCC itself
> and second it should behaviour consistently as a compiler. It is really
> crazy that it will not work when N=1 only.

Well there are other functions which GCC optimizes only for some input (some
constant arguments too).  This is an acceptable behavior due to overloading the
function is undefined behavior.  GCC in this case is optimizing
strncmp(s1,"def",1); to *s1-'d' .


Note if I move the definition of s1 to the global scope and remove const, I get
constaint behavior between 32 and 64bits.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
                   ` (8 preceding siblings ...)
  2013-08-26  6:28 ` pinskia at gcc dot gnu.org
@ 2013-08-26  7:03 ` glisse at gcc dot gnu.org
  2013-08-26  7:17 ` jakub at gcc dot gnu.org
  2013-08-26  8:01 ` shiyan2016 at 126 dot com
  11 siblings, 0 replies; 13+ messages in thread
From: glisse at gcc dot gnu.org @ 2013-08-26  7:03 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

--- Comment #11 from Marc Glisse <glisse at gcc dot gnu.org> ---
(In reply to shiyan from comment #7)
> Thank you for the suggestion. Yes, I know -fno-builtin can work around it. I
> can think of many possible ways to work around it....but whatever, it is a
> bug.

in your code.

> As I known, redefining a function (override?) is not supported in
> standard C, but first most of compiler supports this including GCC itself

if you pass suitable options to tell the compiler you are doing that. Gcc
supports compiling the linux kernel, but that also requires some special
options, like anything non-standard.

> and second it should behaviour consistently as a compiler. It is really
> crazy that it will not work when N=1 only.

No, that's perfectly normal. Note that N==0 is optimized as well. Gcc optimizes
only when there is something to optimize (N small enough).

> Also, all other mainstream compiler can work well. And it cannot work with
> -O0 also, which means it is not an optimization of code generation.

It is, because gcc does early optimizations during parsing (which it arguably
shouldn't, that's a known design issue) regardless of the optimization flags.

> Maybe it
> just replace strncmp(s1, s2, 1) with (*s1-*s2),

Yes.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
                   ` (9 preceding siblings ...)
  2013-08-26  7:03 ` glisse at gcc dot gnu.org
@ 2013-08-26  7:17 ` jakub at gcc dot gnu.org
  2013-08-26  8:01 ` shiyan2016 at 126 dot com
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-08-26  7:17 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #12 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Removing of strncmp in your bogus testcase because its result is unused doesn't
happen only because you override strncmp in the same compilation unit and
before it is eliminated it your code is inlined.  If you compile with -O2
-fno-inline or redefine strncmp in a different CU, both strncmp calls will be
optimized away.  The C and C++ standard describe exactly what the functions
like strncmp do, and your testcase doesn't do that, both because it returns
unconditionally 0 even when the arguments aren't the same in the length, uses
invalid type for the last argument (it should be size_t) and has user visible
side effect that it shouldn't.  So, if you want this to work, you really need
to pass a non-standard option (-fno-builtin or -fno-builtin-strncmp) to tell
gcc you are not compiling a valid program and force it not to optimize it.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c/58240] GCC optimize strncmp when N=1 incorrectly
  2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
                   ` (10 preceding siblings ...)
  2013-08-26  7:17 ` jakub at gcc dot gnu.org
@ 2013-08-26  8:01 ` shiyan2016 at 126 dot com
  11 siblings, 0 replies; 13+ messages in thread
From: shiyan2016 at 126 dot com @ 2013-08-26  8:01 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58240

--- Comment #13 from shiyan <shiyan2016 at 126 dot com> ---
Hi all,

Thank you for all your explanation. This seems more like a philosophical
discussion.... :)
I do can understand what GCC is doing (as I mentioned, it may be optimized to
*s1-*s2). And I know that this is something not standard and it should work
with '-fno-builtin' (I am familiar with this option).

Whatever, maybe I should really do something standard as all you mentioned, to
use '-fno-builtin' to force it to use my own strncmp implementation.

Thank you again for all your explanation and confirmed my understanding that
GCC did such optimization (*s1-*s2) at front-end stage.

Thanks,
Shiyan


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2013-08-26  8:01 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-25 14:44 [Bug c/58240] New: GCC optimize strncmp when N=1 incorrectly shiyan2016 at 126 dot com
2013-08-25 14:50 ` [Bug c/58240] " paolo.carlini at oracle dot com
2013-08-25 14:54 ` shiyan2016 at 126 dot com
2013-08-25 15:04 ` paolo.carlini at oracle dot com
2013-08-25 17:56 ` glisse at gcc dot gnu.org
2013-08-25 18:10 ` paolo.carlini at oracle dot com
2013-08-25 18:12 ` jakub at gcc dot gnu.org
2013-08-26  2:12 ` shiyan2016 at 126 dot com
2013-08-26  2:17 ` shiyan2016 at 126 dot com
2013-08-26  6:28 ` pinskia at gcc dot gnu.org
2013-08-26  7:03 ` glisse at gcc dot gnu.org
2013-08-26  7:17 ` jakub at gcc dot gnu.org
2013-08-26  8:01 ` shiyan2016 at 126 dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).