Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 2d1f8ef

Browse files
rymrgdlang-bot
authored andcommitted
Fix issue 21578 - core.atomic.atomicFetchSub for pointers incorrectly calls wrong function from core.internal.atomic
1 parent 21c2eac commit 2d1f8ef

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/core/atomic.d

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ T atomicFetchSub(MemoryOrder ms = MemoryOrder.seq, T)(ref T val, size_t mod) pur
196196
in (atomicValueIsProperlyAligned(val))
197197
{
198198
static if (is(T == U*, U))
199-
return cast(T)core.internal.atomic.atomicFetchAdd!ms(cast(size_t*)&val, mod * U.sizeof);
199+
return cast(T)core.internal.atomic.atomicFetchSub!ms(cast(size_t*)&val, mod * U.sizeof);
200200
else
201201
return core.internal.atomic.atomicFetchSub!ms(&val, cast(T)mod);
202202
}
@@ -1143,6 +1143,29 @@ version (CoreUnittest)
11431143
}
11441144
}
11451145

1146+
@betterC pure nothrow @nogc unittest
1147+
{
1148+
byte[10] byteArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
1149+
ulong[10] ulongArray = [2, 4, 6, 8, 10, 12, 14, 16, 19, 20];
1150+
1151+
{
1152+
auto array = byteArray;
1153+
byte* ptr = &array[0];
1154+
byte* prevPtr = atomicFetchAdd(ptr, 3);
1155+
assert(prevPtr == &array[0]);
1156+
assert(*prevPtr == 1);
1157+
assert(*ptr == 7);
1158+
}
1159+
{
1160+
auto array = ulongArray;
1161+
ulong* ptr = &array[0];
1162+
ulong* prevPtr = atomicFetchAdd(ptr, 3);
1163+
assert(prevPtr == &array[0]);
1164+
assert(*prevPtr == 2);
1165+
assert(*ptr == 8);
1166+
}
1167+
}
1168+
11461169
@betterC pure nothrow @nogc @safe unittest
11471170
{
11481171
shared ubyte u8 = 1;
@@ -1167,6 +1190,29 @@ version (CoreUnittest)
11671190
}
11681191
}
11691192

1193+
@betterC pure nothrow @nogc unittest
1194+
{
1195+
byte[10] byteArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
1196+
ulong[10] ulongArray = [2, 4, 6, 8, 10, 12, 14, 16, 19, 20];
1197+
1198+
{
1199+
auto array = byteArray;
1200+
byte* ptr = &array[5];
1201+
byte* prevPtr = atomicFetchSub(ptr, 4);
1202+
assert(prevPtr == &array[5]);
1203+
assert(*prevPtr == 11);
1204+
assert(*ptr == 3); // https://issues.dlang.org/show_bug.cgi?id=21578
1205+
}
1206+
{
1207+
auto array = ulongArray;
1208+
ulong* ptr = &array[5];
1209+
ulong* prevPtr = atomicFetchSub(ptr, 4);
1210+
assert(prevPtr == &array[5]);
1211+
assert(*prevPtr == 12);
1212+
assert(*ptr == 4); // https://issues.dlang.org/show_bug.cgi?id=21578
1213+
}
1214+
}
1215+
11701216
@betterC pure nothrow @nogc @safe unittest // issue 16651
11711217
{
11721218
shared ulong a = 2;

0 commit comments

Comments
 (0)