Skip to content

Commit 5b63050

Browse files
committed
Skip memcpy(x, x) in qunique().
It has undefined behavior. Follow the precedent of commit 9a9473f. No back-patch, since the master branch alone has this function. Discussion: https://postgr.es/m/20191229070221.GA13873@gust.leadboat.com
1 parent fac1c04 commit 5b63050

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/include/lib/qunique.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ qunique(void *array, size_t elements, size_t width,
3030

3131
for (i = 1, j = 0; i < elements; ++i)
3232
{
33-
if (compare(bytes + i * width, bytes + j * width) != 0)
34-
memcpy(bytes + ++j * width, bytes + i * width, width);
33+
if (compare(bytes + i * width, bytes + j * width) != 0 &&
34+
++j != i)
35+
memcpy(bytes + j * width, bytes + i * width, width);
3536
}
3637

3738
return j + 1;
@@ -55,8 +56,9 @@ qunique_arg(void *array, size_t elements, size_t width,
5556

5657
for (i = 1, j = 0; i < elements; ++i)
5758
{
58-
if (compare(bytes + i * width, bytes + j * width, arg) != 0)
59-
memcpy(bytes + ++j * width, bytes + i * width, width);
59+
if (compare(bytes + i * width, bytes + j * width, arg) != 0 &&
60+
++j != i)
61+
memcpy(bytes + j * width, bytes + i * width, width);
6062
}
6163

6264
return j + 1;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy