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

[PATCH] line-log: use COPY_ARRAY to fix mis-sized memcpy

From
Jeff King <peff@peff.net>
Date
Mar 5, 2017, 11:44 UTC
Message-ID
<20170305114446.wqm33o52yizlffju@sigill.intra.peff.net>
In-Reply-To
<20170305113618.ko2jymle4n5f2b5l@sigill.intra.peff.net>
On Sun, Mar 05, 2017 at 06:36:19AM -0500, Jeff King wrote:
Show 7 quoted lines
> >  	range_set_init(dst, src->nr);
> > -	memcpy(dst->ranges, src->ranges, src->nr*sizeof(struct range_set));
> > +	memcpy(dst->ranges, src->ranges, src->nr*sizeof(struct range));
> 
> I think "sizeof(*dst->ranges)" is probably an even better fix, as it
> infers the type of "dst". But these days we have COPY_ARRAY() to make it
> even harder to get this kind of thing wrong.

So here's your fix wrapped up with a commit message, mostly for Junio's convenience. I listed you as the author, since you did the hard part. If you're OK with it, please indicate that it's OK to add your signed-off-by. If you prefer to do it differently, feel free to post your own patch.

-- >8 --
From: Vegard Nossum <vegard.nossum@oracle.com>
Subject: [PATCH] line-log: use COPY_ARRAY to fix mis-sized memcpy

This memcpy meant to get the sizeof a "struct range", not a "range_set", as the former is what our array holds. Rather than swap out the types, let's convert this site to COPY_ARRAY, which avoids the problem entirely (and confirms that the src and dst types match).

Note for curiosity's sake that this bug doesn't trigger on I32LP64 systems, but does on ILP32 systems. The mistaken "struct range_set" has two ints and a pointer. That's 16 bytes on LP64, or 12 on ILP32. The correct "struct range" type has two longs, which is also 16 on LP64, but only 8 on ILP32.

Likewise an IL32P64 system would experience the bug.
Signed-off-by: Jeff King <peff@peff.net>
---
 line-log.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/line-log.c b/line-log.c
index 65f3558b3..a2477f601 100644
--- a/line-log.c
+++ b/line-log.c
@@ -43,9 +43,10 @@ void range_set_release(struct range_set *rs)
 static void range_set_copy(struct range_set *dst, struct range_set *src)
 {
 	range_set_init(dst, src->nr);
-	memcpy(dst->ranges, src->ranges, src->nr*sizeof(struct range_set));
+	COPY_ARRAY(dst->ranges, src->ranges, src->nr);
 	dst->nr = src->nr;
 }
+
 static void range_set_move(struct range_set *dst, struct range_set *src)
 {
 	range_set_release(dst);
-- 
2.12.0.426.g9d5d0eeae
Previous: Jeff KingNext: Vegard Nossum
Message 19 of 36 in “Travis: also test on 32-bit Linux”
  1. Travis: also test on 32-bit LinuxJohannes Schindelin, Feb 28, 2017
  2. Johannes SchindelinFeb 28, 2017
  3. Junio C HamanoMar 2, 2017
  4. Travis: also test on 32-bit LinuxLars Schneider, Mar 2, 2017
  5. Johannes SchindelinMar 2, 2017
  6. Lars SchneiderMar 2, 2017
  7. Johannes SchindelinMar 2, 2017
  8. Christian CouderMar 2, 2017
  9. Junio C HamanoMar 2, 2017
  10. Johannes SchindelinMar 3, 2017
  11. Junio C HamanoMar 3, 2017
  12. Junio C HamanoMar 4, 2017
  13. Junio C HamanoMar 4, 2017
  14. Lars SchneiderMar 4, 2017
  15. Vegard NossumMar 4, 2017
  16. Vegard NossumMar 4, 2017
  17. Vegard NossumMar 4, 2017
  18. Jeff KingMar 5, 2017
  19. line-log: use COPY_ARRAY to fix mis-sized memcpyJeff King, Mar 5, 2017
  20. Vegard NossumMar 5, 2017
  21. Jeff KingMar 5, 2017
  22. René ScharfeMar 10, 2017
  23. Jeff KingMar 10, 2017
  24. René ScharfeMar 10, 2017
  25. Jeff KingMar 10, 2017
  26. René ScharfeMar 10, 2017
  27. Jeff KingMar 10, 2017
  28. René ScharfeMar 10, 2017
  29. Junio C HamanoMar 10, 2017
  30. Jeff KingMar 10, 2017
  31. René ScharfeMar 10, 2017
  32. Junio C HamanoMar 10, 2017
  33. René ScharfeMar 11, 2017
  34. Ramsay JonesMar 2, 2017
  35. Lars SchneiderMar 5, 2017
  36. Ramsay JonesMar 5, 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.