git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/5] add SWAP macro

From
René Scharfe <l.s.r@web.de>
Date
Apr 29, 2017, 18:16 UTC
Message-ID
<11699799-6bdf-484d-5a1c-8e8fa7981594@web.de>
In-Reply-To
<20170428214934.tuqihgch6qeen3ni@sigill.intra.peff.net>
Am 28.04.2017 um 23:49 schrieb Jeff King:
Show 27 quoted lines
> On Fri, Apr 28, 2017 at 07:04:51PM +0200, René Scharfe wrote:
> 
>>> What should:
>>>
>>>     SWAP(foo[i], foo[j]);
>>>
>>> do when i == j? With this code, it ends up calling
>>>
>>>     memcpy(&foo[i], &foo[j], ...);
>>>
>>> which can cause valgrind to complain about overlapping memory. I suspect
>>> in practice that noop copies are better off than partial overlaps, but I
>>> think it does still violate the standard.
>>>
>>> Is it worth comparing the pointers and bailing early?
>>
>> Hmm, so swapping a value with itself can be a useful thing to do?
>> Otherwise an assert would be more appropriate.
> 
> No, I doubt that it's useful, and it's probably a sign of a bug
> elsewhere. But it's likely a _harmless_ bug, so it may be irritating to
> die when we hit it rather than continuing.
> 
> I dunno. I could go either way. Or we could leave it as-is, and let
> valgrind find the problem. That has zero run-time cost, but of course
> nobody bothers to run valgrind outside of the test suite, so the inputs
> are not usually very exotic.

It would be problematic on platforms where memcpy has to erase the destination before writing new values (I don't know any example).

We could use two temporary buffers. The object code is the same with GCC around 5 and Clang 3.2 or higher -- at least for prio-queue.c.

With GCC 4.9.2 (that's what Debian stable currently has) the result is actually slightly better with two buffers because a 128-bit move starts to get used (https://godbolt.org/g/18HQDQ).

Show 8 quoted lines
>> Swapping with *partial* overlap sounds tricky, or even evil.  If
>> we want to support that for some reason we'd have to use memmove
>> in the middle.  But that would still corrupt at least one of the
>> objects, wouldn't it?
> 
> Yes, the overlap case is probably an actual bug. Detecting it is a bit
> harder, but definitely possible. I hate to pay the run-time cost for it,
> but I wonder if a compiler could optimize it out.

How is it possible to arrive at such a situation? We'd need two objects of the same size (we check that in SWAP) and one of them would start inside of the other one, i.e. the pointer difference between them would be a fraction of 1. So the type system would have to be tricked into it, right?

How *would* we detect overlaps? The obvious checks (a+len<=b||b+len<=a) are undefined if applied to objects that don't belong to the same array. And members of the same array would not overlap to begin with..

It may be my laziness speaking, but do we really need such a check? If someone constructs interleaving objects then they'd need to implement the necessary checks themselves IMHO.

Show 11 quoted lines
>> The line in question is this one:
>>
>> 	for (i = 0; i <= (j = (queue->nr - 1) - i); i++)
>>
>> Assignment in the middle?  Hmm.  Why not do it like this?
>>
>> 	for (i = 0, j = queue->nr - 1; i < j; i++, j--)
>>
>> Looks less complicated to me.
> 
> Yes, see my other reply. :)

Ah, so that's where I stole it from. ;) Perhaps my source amnesia was in part caused by confusion about your reasoning there: The code does A, B would be better, so let's do C. Wait, what? :)

René
Previous: Jeff KingNext: Jeff King
Message 34 of 50 in “introduce SWAP macro”
  1. 0/5 introduce SWAP macroRené Scharfe, Jan 28, 2017
  2. 1/5 add SWAP macroRené Scharfe, Jan 28, 2017
  3. Johannes SchindelinJan 30, 2017
  4. René ScharfeJan 30, 2017
  5. Johannes SchindelinJan 30, 2017
  6. René ScharfeJan 30, 2017
  7. Johannes SchindelinJan 31, 2017
  8. René ScharfeJan 31, 2017
  9. Ramsay JonesFeb 1, 2017
  10. Johannes SchindelinFeb 1, 2017
  11. Johannes SchindelinJan 30, 2017
  12. René ScharfeJan 30, 2017
  13. Johannes SixtJan 30, 2017
  14. Johannes SchindelinJan 30, 2017
  15. René ScharfeJan 30, 2017
  16. Brandon WilliamsJan 30, 2017
  17. René ScharfeJan 31, 2017
  18. Jeff KingJan 31, 2017
  19. Junio C HamanoJan 31, 2017
  20. Jeff KingJan 31, 2017
  21. Johannes SchindelinFeb 1, 2017
  22. Jeff KingFeb 1, 2017
  23. René ScharfeFeb 1, 2017
  24. Junio C HamanoFeb 1, 2017
  25. René ScharfeFeb 7, 2017
  26. Junio C HamanoFeb 7, 2017
  27. Johannes SchindelinFeb 8, 2017
  28. Johannes SchindelinJan 31, 2017
  29. Jeff KingApr 24, 2017
  30. Jeff KingApr 24, 2017
  31. Duy NguyenApr 24, 2017
  32. René ScharfeApr 28, 2017
  33. Jeff KingApr 28, 2017
  34. René ScharfeApr 29, 2017
  35. Jeff KingApr 30, 2017
  36. René ScharfeMay 2, 2017
  37. 2/5 apply: use SWAP macroRené Scharfe, Jan 28, 2017
  38. 3/5 use SWAP macroRené Scharfe, Jan 28, 2017
  39. Johannes SchindelinJan 30, 2017
  40. René ScharfeJan 30, 2017
  41. Junio C HamanoJan 30, 2017
  42. René ScharfeJan 31, 2017
  43. 4/5 diff: use SWAP macroRené Scharfe, Jan 28, 2017
  44. Johannes SchindelinJan 30, 2017
  45. René ScharfeJan 30, 2017
  46. Junio C HamanoJan 30, 2017
  47. 5/5 graph: use SWAP macroRené Scharfe, Jan 28, 2017
  48. Johannes SchindelinJan 30, 2017
  49. René ScharfeJan 30, 2017
  50. Junio C HamanoJan 30, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.