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

Re: [PATCH] gc: fix cast in compare_tasks_by_selection()

From
Jeff King <peff@peff.net>
Date
Nov 17, 2020, 22:30 UTC
Message-ID
<20201117223011.GA642234@coredump.intra.peff.net>
In-Reply-To
<d80423b7-c89d-99a2-ce49-8308822f7769@web.de>
On Tue, Nov 17, 2020 at 10:59:49PM +0100, René Scharfe wrote:
Show 7 quoted lines
> compare_tasks_by_selection() is used with QSORT and gets passed pointers
> to the elements of "static struct maintenance_task tasks[]".  It casts
> the *addresses* of these passed pointers to element pointers, though,
> and thus effectively compares some unrelated values from the stack.  Fix
> the casts to actually compare array elements.
> 
> Detected by USan (make SANITIZE=undefined test).

I checked the caller here, and indeed, it's passing an array-of-struct so your patch is doing the right thing (not that I doubted it, but that's what review is for).

This qsort void-pointer convention seems to create a lot of confusion (not just the lack of type-safety, but the fact that it's getting a pointer to the element). I felt like we had a discussion about this a while ago, and indeed, I found:

  https://lore.kernel.org/git/7b95417a-c8fb-4f1e-cb09-c36804a3a4d0@web.de/
The whole sub-thread is worth reading, but the macro you arrived at in:
  https://lore.kernel.org/git/c141fb44-904f-e8b6-119f-7d2d6bcfd81a@web.de/
seems pretty reasonable.
-Peff
Previous: René Scharfe
Message 2 of 2 in “gc: fix cast in compare_tasks_by_selection()”
  1. gc: fix cast in compare_tasks_by_selection()René Scharfe, Nov 17, 2020
  2. Jeff KingNov 17, 2020

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.