User account creation filtered due to spam.

Bug 51811 - [C++0x] Incorrect increment/decrement of atomic pointers
Summary: [C++0x] Incorrect increment/decrement of atomic pointers
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.6.2
: P3 major
Target Milestone: 4.7.0
Assignee: Benjamin Kosnik
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-10 14:22 UTC by Tobias Schuele
Modified: 2012-07-20 17:28 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-02-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Schuele 2012-01-10 14:22:33 UTC
Incrementation (decrementation) of atomic pointers does not work correctly. On an x86-64 machine the piece of code given below produces the following output (or something similar):

0x7fffbe6d082c
0x7fffbe6d082d
0x7fffbe6d0830
0x7fffbe6d082d

There are two problems here: Firstly, the pointer is incremented by 1 and not by sizeof(int). Secondly, the last two lines of the output should be the same. Note that the second problem seems to be fixed in gcc-4.7-20120107, while the first one still occurs. Thanks for your help!

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

#include <iostream>
#include <atomic>

using namespace std;

int main(int argc, char* argv[]) {
  int value = 42;
  atomic<int*> my_pointer;
  my_pointer = &value;
  cout << my_pointer++ << endl;
  cout << my_pointer << endl;
  my_pointer = &value;
  cout << ++my_pointer << endl;
  cout << my_pointer << endl;
}
Comment 1 Benjamin Kosnik 2012-02-03 19:41:00 UTC
Mine
Comment 2 Benjamin Kosnik 2012-02-03 19:49:17 UTC
Author: bkoz
Date: Fri Feb  3 19:49:11 2012
New Revision: 183875

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183875
Log:
2012-02-03  Benjamin Kosnik  <bkoz@redhat.com>

	PR libstdc++/51811
	* include/bits/atomic_base.h (atomic<_Tp*>): Fix offsets.
	* testsuite/29_atomics/atomic/operators/51811.cc: New.
	* testsuite/29_atomics/atomic/operators/pointer_partial_void.cc: New.

Added:
    trunk/libstdc++-v3/testsuite/29_atomics/atomic/operators/51811.cc
    trunk/libstdc++-v3/testsuite/29_atomics/atomic/operators/pointer_partial_void.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/bits/atomic_base.h
Comment 3 Benjamin Kosnik 2012-02-15 03:01:50 UTC
Fixed for 4.7
Comment 4 Jing Yu 2012-07-20 17:28:09 UTC
Author: jingyu
Date: Fri Jul 20 17:27:57 2012
New Revision: 189724

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189724
Log:
2012-07-19  Jing Yu  <jingyu@google.com>

	Backport r183875 to fix wrong atomic<_Tp*> add_fetch.
	PR libstdc++/51811, Google ref b/6702865
	* include/bits/atomic_0.h (atomic<_Tp*>): Fix offsets.
	* include/bits/atomic_2.h: Likewise.
	* testsuite/29_atomics/atomic/operators/51811.cc: New.
	* testsuite/29_atomics/atomic/operators/pointer_partial_void.cc: New.

Added:
    branches/google/gcc-4_6/libstdc++-v3/testsuite/29_atomics/atomic/operators/51811.cc
    branches/google/gcc-4_6/libstdc++-v3/testsuite/29_atomics/atomic/operators/pointer_partial_void.cc
Modified:
    branches/google/gcc-4_6/libstdc++-v3/ChangeLog.google-4_6
    branches/google/gcc-4_6/libstdc++-v3/include/bits/atomic_0.h
    branches/google/gcc-4_6/libstdc++-v3/include/bits/atomic_2.h