public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*>
@ 2003-06-18 14:52 ysbeer at af dot org dot za
  2003-06-18 22:48 ` [Bug libstdc++/11240] " ysbeer at af dot org dot za
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: ysbeer at af dot org dot za @ 2003-06-18 14:52 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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

           Summary: operator-() fails for reverse_iterator<const char*>
           Product: gcc
           Version: 3.2.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ysbeer@af.org.za
                CC: gcc-bugs@gcc.gnu.org
 GCC build triplet: gcc version 3.2.2 20030222
  GCC host triplet: i386 (Linux/FreeBSD/Cygwin)

Finding differences between reverse_iterators of const char* are broken causing 
algorithms such as std::find to fail.  
 
The following code snippet produces the output 
 
  const char* FWD=3 
  const char* REV= 
  REV DIFF=-1208263166 
  string FWD=3 
  string REV=3 
 
instead of 
 
  const char* FWD=3 
  const char* REV= 3 
  REV DIFF=10 
  string FWD=3 
  string REV=3 
 
#include <iterator> 
#include <algorithm> 
#include <iostream> 
#include <string> 
#include <cstring> 
 
using namespace std; 
 
int main() 
{ 
	typedef reverse_iterator<const char*> REV; 
 
	const char s[]= "0123456789"; 
    const size_t size= strlen(s); 
	const string S(s); 
 
	const char* found= find( s,s+size,'3' ); 
	REV rfound= find(REV(s+size),REV(s),'3'); 
	cout << "const char* FWD=" << *found << endl; 
	cout << "const char* REV=" << *rfound << endl; 
	cout << "REV DIFF=" << (REV(s)-REV(s+size)) << endl; 
 
	string::const_iterator Found= find( S.begin(),S.end(),'3' ); 
	string::const_reverse_iterator rFound= find(S.rbegin(),S.rend(),'3'); 
	cout << "string FWD=" << *Found << endl; 
	cout << "string REV=" << *rFound << endl; 
 
	return 0; 
} 
 
Checked on (gcc -v): 
 
[1] Built with Configured with: ../configure --prefix=/usr --libdir=/usr/lib 
--with-slibdir=/lib --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared 
--enable-threads=posix --disable-checking --enable-long-long 
--enable-__cxa_atexit --enable-languages=c,c++,ada,f77,objc,java 
--host=i586-mandrake-linux-gnu --with-system-zlib 
Thread model: posix 
gcc version 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk) 
 
[2] gcc version 2.95.4 20020320 [FreeBSD] 
 
[3] gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81) 
 
[4] Configured with: ../configure --prefix=/usr --mandir=/usr/share/man 
--infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking 
--with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux 
Thread model: posix 
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) 
 
[5] gcc version 3.2 20020927 (Cygwin) 
 
In all cases compiles done with simple "g++ filename.cpp" command. 
 
Sidenote: The problem disappears if 'const char s[]= "0123456789";' is changed to 
'char s[]="01234567890"'. This makes we wonder if there is  possibly an compiler 
optimisation issue involved.


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

* [Bug libstdc++/11240] operator-() fails for reverse_iterator<const char*>
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
@ 2003-06-18 22:48 ` ysbeer at af dot org dot za
  2003-07-11 21:50 ` dhazeghi at yahoo dot com
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: ysbeer at af dot org dot za @ 2003-06-18 22:48 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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



------- Additional Comments From ysbeer at af dot org dot za  2003-06-18 22:48 -------
Further investigation seems to indicate that the address of the literal is used
in one place and the array address is used in another. It loads the address of
the stack variable and substracts the address of the constant string. This will
explain the massive difference when using the pointer substraction. It seems
more and more like a code generation issue and not a library issue. 

(Thanks goes to Erwin on freenode.net#c++ for working through the assembler part)


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

* [Bug libstdc++/11240] operator-() fails for reverse_iterator<const char*>
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
  2003-06-18 22:48 ` [Bug libstdc++/11240] " ysbeer at af dot org dot za
@ 2003-07-11 21:50 ` dhazeghi at yahoo dot com
  2003-07-25 14:23 ` pinskia at physics dot uc dot edu
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: dhazeghi at yahoo dot com @ 2003-07-11 21:50 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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


dhazeghi at yahoo dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
  GCC build triplet|gcc version 3.2.2 20030222  |
   GCC host triplet|i386 (Linux/FreeBSD/Cygwin) |
           Keywords|                            |wrong-code
   Last reconfirmed|0000-00-00 00:00:00         |2003-07-11 21:50:22
               date|                            |


------- Additional Comments From dhazeghi at yahoo dot com  2003-07-11 21:50 -------
Confirmed with gcc 3.2 and 3.3 branch. With mainline, the code doesn't compile
(perhaps another bug?)

[dara@draco mainline]$ /usr/local/gcc-3.4-branch/bin/g++ test.cpp
In file included from /usr/local/gcc-3.4-branch/include/c++/3.4/ostream:45,
                 from /usr/local/gcc-3.4-branch/include/c++/3.4/iterator:70,
                 from test.cpp:1:
/usr/local/gcc-3.4-branch/include/c++/3.4/bits/char_traits.h:191: error: `
   wstreampos' does not name a type
In file included from /usr/local/gcc-3.4-branch/include/c++/3.4/ios:50,
                 from /usr/local/gcc-3.4-branch/include/c++/3.4/ostream:45,
                 from /usr/local/gcc-3.4-branch/include/c++/3.4/iterator:70,
                 from test.cpp:1:


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

* [Bug libstdc++/11240] operator-() fails for reverse_iterator<const char*>
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
  2003-06-18 22:48 ` [Bug libstdc++/11240] " ysbeer at af dot org dot za
  2003-07-11 21:50 ` dhazeghi at yahoo dot com
@ 2003-07-25 14:23 ` pinskia at physics dot uc dot edu
  2003-07-27 22:57 ` pinskia at physics dot uc dot edu
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at physics dot uc dot edu @ 2003-07-25 14:23 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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

Bug 11240 depends on bug 11653, which changed state.

Bug 11653 Summary: [3.4 Regression] can no longer include iostream
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11653

           What    |Old Value                   |New Value
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
             Status|WAITING                     |RESOLVED
         Resolution|                            |WORKSFORME


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

* [Bug libstdc++/11240] operator-() fails for reverse_iterator<const char*>
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
                   ` (2 preceding siblings ...)
  2003-07-25 14:23 ` pinskia at physics dot uc dot edu
@ 2003-07-27 22:57 ` pinskia at physics dot uc dot edu
  2003-08-23  0:31 ` dhazeghi at yahoo dot com
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at physics dot uc dot edu @ 2003-07-27 22:57 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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


pinskia at physics dot uc dot edu changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2003-07-11 21:50:22         |2003-07-27 22:57:35
               date|                            |


------- Additional Comments From pinskia at physics dot uc dot edu  2003-07-27 22:57 -------
I can confirm this on the mainline (20030727).


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

* [Bug libstdc++/11240] operator-() fails for reverse_iterator<const char*>
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
                   ` (3 preceding siblings ...)
  2003-07-27 22:57 ` pinskia at physics dot uc dot edu
@ 2003-08-23  0:31 ` dhazeghi at yahoo dot com
  2003-10-11  9:14 ` [Bug c++/11240] " paolo at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: dhazeghi at yahoo dot com @ 2003-08-23  0:31 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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


dhazeghi at yahoo dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|3.4                         |---


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

* [Bug c++/11240] operator-() fails for reverse_iterator<const char*>
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
                   ` (4 preceding siblings ...)
  2003-08-23  0:31 ` dhazeghi at yahoo dot com
@ 2003-10-11  9:14 ` paolo at gcc dot gnu dot org
  2003-10-11  9:53 ` [Bug c++/11240] Weirdness with " paolo at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo at gcc dot gnu dot org @ 2003-10-11  9:14 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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


paolo at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nathan at gcc dot gnu dot
                   |                            |org
          Component|libstdc++                   |c++


------- Additional Comments From paolo at gcc dot gnu dot org  2003-10-11 09:14 -------
Really seems a c++, not libstdc++ bug and I'm tentatively changing
its category.

Below a reduced testcase, with two workarounds in comments.

Nathan, could you possibly have a quick look?

Thanks in advance!
Paolo.

///////////

#include <iterator> 
#include <cassert> 

int main() 
{ 
  typedef std::reverse_iterator<const char*> RI;

  // char s[] = "0";
  const char s[] = "0";

  // RI r1(s);
  RI r1 = RI(s);

  const char* n1 = r1.base();
  assert( n1 == s );
}


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

* [Bug c++/11240] Weirdness with reverse_iterator<const char*>
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
                   ` (5 preceding siblings ...)
  2003-10-11  9:14 ` [Bug c++/11240] " paolo at gcc dot gnu dot org
@ 2003-10-11  9:53 ` paolo at gcc dot gnu dot org
  2003-10-11 10:13 ` paolo at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo at gcc dot gnu dot org @ 2003-10-11  9:53 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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



------- Additional Comments From paolo at gcc dot gnu dot org  2003-10-11 09:53 -------
A further reduced testcase.

Paolo.

/////////////

#include <cassert> 

template<typename _Iterator>
  class reverse_iterator 
  {
  protected:
    _Iterator current;

  public:
    typedef _Iterator iterator_type;

    explicit 
    reverse_iterator(iterator_type __x)
    : current(__x)
    { }

    iterator_type 
    base() const { return current; }
  };

int main() 
{ 
  typedef reverse_iterator<const char*> RI;

  // char s[] = "0";
  const char s[] = "0";

  // RI r1(s);
  RI r1 = RI(s);

  const char* n1 = r1.base();
  assert( n1 == s );
}


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

* [Bug c++/11240] Weirdness with reverse_iterator<const char*>
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
                   ` (6 preceding siblings ...)
  2003-10-11  9:53 ` [Bug c++/11240] Weirdness with " paolo at gcc dot gnu dot org
@ 2003-10-11 10:13 ` paolo at gcc dot gnu dot org
  2003-10-16 19:39 ` bangerth at dealii dot org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo at gcc dot gnu dot org @ 2003-10-11 10:13 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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



------- Additional Comments From paolo at gcc dot gnu dot org  2003-10-11 10:13 -------
... and templates have nothing to do with it!

Paolo.

/////////

#include <cassert> 

class r_i 
{
protected:
  const char* current;

public:
  r_i(const char* __x) : current(__x) { }

  const char*
  base() const { return current; }
};

int main() 
{ 
  // char s[] = "0";
  const char s[] = "0";

  // r_i r1(s);
  r_i r1 = r_i(s);

  assert( r1.base() == s );
}


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

* [Bug c++/11240] Weirdness with reverse_iterator<const char*>
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
                   ` (7 preceding siblings ...)
  2003-10-11 10:13 ` paolo at gcc dot gnu dot org
@ 2003-10-16 19:39 ` bangerth at dealii dot org
  2004-01-05 14:23 ` [Bug c++/11240] Pointer comparison problem when using constructors nathan at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: bangerth at dealii dot org @ 2003-10-16 19:39 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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



------- Additional Comments From bangerth at dealii dot org  2003-10-16 19:39 -------
Here's something odd: this test fails with all versions of gcc
------------------------------
#include <cassert>

struct R1 {
  const char* p;
  R1(const char* p) : p(p) { }
};

int main() { 
  const char s[] = "0";
  R1 r1 = R1(s);

  assert( r1.p == s );
}
----------------------------

whereas the following succeeds:
----------------------------
#include <cassert>

struct R1 {
  const char* p;
};

int main() { 
  const char s[] = "0";
  R1 r1 = {s};

  assert( r1.p == s );
}

--------------------------------

I don't know what's going on. The first test also fails if one changes
the type of p to "const char * const". In the debugger, this looks
like so:

(gdb) p &s[0]
$5 = 0xbffff00a "0"
(gdb) p r1.p
$6 = 0x804868f "0"

It looks as if "s" were in the read-only part of memory, while r1.p is
pointing into read-write memory. But I don't know enough about these issues
to speculate further.

W.


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

* [Bug c++/11240] Pointer comparison problem when using constructors
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
                   ` (8 preceding siblings ...)
  2003-10-16 19:39 ` bangerth at dealii dot org
@ 2004-01-05 14:23 ` nathan at gcc dot gnu dot org
  2004-03-04 15:05 ` giovannibajo at libero dot it
  2004-10-20  9:49 ` reichelt at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: nathan at gcc dot gnu dot org @ 2004-01-05 14:23 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From nathan at gcc dot gnu dot org  2004-01-05 14:23 -------
yow! Wolfgang's example is the best. Here is the broken assembler I get with
gcc 3.2
main:
.LFB1:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        subl    $8, %esp
.LCFI2:
        andl    $-16, %esp
        movl    $0, %eax
        subl    %eax, %esp

        movw    .LC0, %ax   ; get initial value for s
        movw    %ax, -2(%ebp) ; and store it into s

        subl    $8, %esp
        pushl   $.LC0          ;; push address of string in .rodata
                               ;; wrong! should be leal -2(%ebp),%eax;push %eax
        leal    -8(%ebp), %eax ;; place to create r1
        pushl   %eax           ;; push this pointer
.LCFI3:
       call    _ZN2R1C1EPKc ;; construct an R1

this could be a bug in cc1plus's 'decl_constant_value' function,
anyone care to take a look?


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2004-01-01 02:37:29         |2004-01-05 14:23:20
               date|                            |


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


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

* [Bug c++/11240] Pointer comparison problem when using constructors
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
                   ` (9 preceding siblings ...)
  2004-01-05 14:23 ` [Bug c++/11240] Pointer comparison problem when using constructors nathan at gcc dot gnu dot org
@ 2004-03-04 15:05 ` giovannibajo at libero dot it
  2004-10-20  9:49 ` reichelt at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: giovannibajo at libero dot it @ 2004-03-04 15:05 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From giovannibajo at libero dot it  2004-03-04 15:05 -------
No, it looks like decl_constant_value receives "s" as <addr_expr> so it doesn't 
take action. Any other suggestion?

-- 


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


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

* [Bug c++/11240] Pointer comparison problem when using constructors
  2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
                   ` (10 preceding siblings ...)
  2004-03-04 15:05 ` giovannibajo at libero dot it
@ 2004-10-20  9:49 ` reichelt at gcc dot gnu dot org
  11 siblings, 0 replies; 13+ messages in thread
From: reichelt at gcc dot gnu dot org @ 2004-10-20  9:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From reichelt at gcc dot gnu dot org  2004-10-20 09:49 -------
Fixed by Mark's patch
http://gcc.gnu.org/ml/gcc-cvs/2004-10/msg01166.html

Do we want to have a testcase for this one?


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mark at codesourcery dot com
             Status|NEW                         |RESOLVED
      Known to fail|2.95.3 3.2.3 3.3.3 3.4.0 4.0|2.95.3 3.2.3 3.3.3 3.4.0
      Known to work|                            |4.0.0
         Resolution|                            |FIXED
   Target Milestone|---                         |4.0.0


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


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

end of thread, other threads:[~2004-10-20  9:49 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-18 14:52 [Bug libstdc++/11240] New: operator-() fails for reverse_iterator<const char*> ysbeer at af dot org dot za
2003-06-18 22:48 ` [Bug libstdc++/11240] " ysbeer at af dot org dot za
2003-07-11 21:50 ` dhazeghi at yahoo dot com
2003-07-25 14:23 ` pinskia at physics dot uc dot edu
2003-07-27 22:57 ` pinskia at physics dot uc dot edu
2003-08-23  0:31 ` dhazeghi at yahoo dot com
2003-10-11  9:14 ` [Bug c++/11240] " paolo at gcc dot gnu dot org
2003-10-11  9:53 ` [Bug c++/11240] Weirdness with " paolo at gcc dot gnu dot org
2003-10-11 10:13 ` paolo at gcc dot gnu dot org
2003-10-16 19:39 ` bangerth at dealii dot org
2004-01-05 14:23 ` [Bug c++/11240] Pointer comparison problem when using constructors nathan at gcc dot gnu dot org
2004-03-04 15:05 ` giovannibajo at libero dot it
2004-10-20  9:49 ` reichelt at gcc dot gnu dot org

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).