[Bug c++/57111] New: Core dump - invalid pointer detected after std::unique_ptr

2013-04-29 Thread jb.1234abcd at gmail dot com


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



 Bug #: 57111

   Summary: Core dump - invalid pointer detected after

std::unique_ptr

Classification: Unclassified

   Product: gcc

   Version: 4.7.2

Status: UNCONFIRMED

  Severity: normal

  Priority: P3

 Component: c++

AssignedTo: unassig...@gcc.gnu.org

ReportedBy: jb.1234a...@gmail.com





$ cat uniqueptr.cpp

#include 

#include 



int main () {

  int arr[]={1,2};

  std::unique_ptr up(arr);

  std::cout << up[0];

  return 0;

}

$ g++ -std=c++11 -Wall -o uniqueptr uniqueptr.cpp 

$ ./uniqueptr 

*** glibc detected *** ./uniqueptr: free(): invalid pointer: 0xbfe35788 ***

=== Backtrace: =

/lib/libc.so.6[0x4ce44ff9]

/lib/libstdc++.so.6(_ZdlPv+0x20)[0x4d414500]

/lib/libstdc++.so.6(_ZdaPv+0x1c)[0x4d41455c]

./uniqueptr[0x80489fd]

./uniqueptr[0x8048966]

./uniqueptr[0x8048765]

/lib/libc.so.6(__libc_start_main+0xf5)[0x4cde8865]

./uniqueptr[0x8048611]

=== Memory map: 

08048000-0804a000 r-xp  08:09 1963079/home/jb/prog-c++/uniqueptr

0804a000-0804b000 r--p 1000 08:09 1963079/home/jb/prog-c++/uniqueptr

0804b000-0804c000 rw-p 2000 08:09 1963079/home/jb/prog-c++/uniqueptr

08dbf000-08de rw-p  00:00 0  [heap]

4cdac000-4cdcb000 r-xp  08:09 655817 /usr/lib/ld-2.16.so

4cdcb000-4cdcc000 r--p 0001e000 08:09 655817 /usr/lib/ld-2.16.so

4cdcc000-4cdcd000 rw-p 0001f000 08:09 655817 /usr/lib/ld-2.16.so

4cdcf000-4cf7f000 r-xp  08:09 659071 /usr/lib/libc-2.16.so

4cf7f000-4cf81000 r--p 001b 08:09 659071 /usr/lib/libc-2.16.so

4cf81000-4cf82000 rw-p 001b2000 08:09 659071 /usr/lib/libc-2.16.so

4cf82000-4cf85000 rw-p  00:00 0 

4cfce000-4d00c000 r-xp  08:09 664509 /usr/lib/libm-2.16.so

4d00c000-4d00d000 r--p 0003d000 08:09 664509 /usr/lib/libm-2.16.so

4d00d000-4d00e000 rw-p 0003e000 08:09 664509 /usr/lib/libm-2.16.so

4d01-4d02c000 r-xp  08:09 664696

/usr/lib/libgcc_s-4.7.2-20121109.so.1

4d02c000-4d02d000 r--p 0001b000 08:09 664696

/usr/lib/libgcc_s-4.7.2-20121109.so.1

4d02d000-4d02e000 rw-p 0001c000 08:09 664696

/usr/lib/libgcc_s-4.7.2-20121109.so.1

4d3c9000-4d4a9000 r-xp  08:09 664705 /usr/lib/libstdc++.so.6.0.17

4d4a9000-4d4ad000 r--p 000df000 08:09 664705 /usr/lib/libstdc++.so.6.0.17

4d4ad000-4d4af000 rw-p 000e3000 08:09 664705 /usr/lib/libstdc++.so.6.0.17

4d4af000-4d4b5000 rw-p  00:00 0 

b000-b777a000 rw-p  00:00 0 

b778b000-b778e000 rw-p  00:00 0 

b778e000-b778f000 r-xp  00:00 0  [vdso]

bfe17000-bfe38000 rw-p  00:00 0  [stack]

1Aborted (core dumped)

$ 



Packages:

gcc-c++-4.7.2-8.fc18.i686

glibc-2.16-30.fc18.i686

libstdc++-4.7.2-8.fc18.i686


[Bug c++/57111] Core dump - invalid pointer detected after std::unique_ptr

2013-04-29 Thread jb.1234abcd at gmail dot com


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



--- Comment #2 from jb  2013-04-29 14:04:38 UTC 
---

(In reply to comment #1)

> That's not how you use unique_ptr.



That's besides the point when you get a dump.

If the proper use of unique_ptr with array is:

unique_ptr up(new int[4]); //array version of unique_ptr

then the compiler should give a warning on "improper use", do not you think ?


[Bug c++/57111] Core dump - invalid pointer detected after std::unique_ptr

2013-04-30 Thread jb.1234abcd at gmail dot com


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



--- Comment #5 from jb  2013-04-30 21:57:00 UTC 
---

(In reply to comment #4)

> gcc has -Wfree-nonheap-object, which works for free but not for delete or

> delete[]. Extending it to these functions seems like a reasonable RFE.



I agree with your suggestion.



gcc(1)

   -Wno-free-nonheap-object

   Do not warn when attempting to free an object that was not

   allocated on the heap.



The above is a non-default option.



$ cat free-test.c

#include 



int main () {

  int arr[] = {1,2};

  int * p = arr;

  free(p);

  return 0;

}



$ gcc -Wall -o free-test free-test.c 

$ ./free-test  

Segmentation fault (core dumped)



$ gcc -Wall -Wfree-nonheap-object -o free-test free-test.c

$ ./free-test

Segmentation fault (core dumped)



$ gcc -Wall -Wno-free-nonheap-object -o free-test free-test.c

$ ./free-test

Segmentation fault (core dumped)

$



It seems to me that the option "free-nonheap-object" does not work in gcc.


[Bug c++/57111] Core dump - invalid pointer detected after std::unique_ptr

2013-04-30 Thread jb.1234abcd at gmail dot com

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

--- Comment #7 from jb  2013-04-30 22:22:44 UTC 
---
(In reply to comment #6)
> (In reply to comment #5)
> > It seems to me that the option "free-nonheap-object" does not work in gcc.
> 
> You need to add -O2 (maybe -O1 is enough, sometimes you need -O3), otherwise
> gcc does not propagate the information of what p is all the way to free.

OK, that worked.

$ gcc -O1 -Wall -o free-test free-test.c 
free-test.c: In function ‘main’:
free-test.c:6:7: warning: attempt to free a non-heap object ‘arr’
[-Wfree-nonheap-object]

[Bug c++/57111] Core dump - invalid pointer detected after std::unique_ptr

2013-04-30 Thread jb.1234abcd at gmail dot com


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



jb  changed:



   What|Removed |Added



 Status|RESOLVED|VERIFIED



--- Comment #8 from jb  2013-04-30 22:50:12 UTC 
---

Please reopen this bug report.

The RFE will make this feature consistent across gcc and g++.