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

[PATCH v2 3/1] t-strvec: tighten .alloc check in check_strvec

From
René Scharfe <l.s.r@web.de>
Date
Jul 14, 2024, 17:06 UTC
Message-ID
<075b3d08-4270-4064-8103-1fece055e197@web.de>
In-Reply-To
<983be396-f47c-4573-8c33-af8367f8ddbe@web.de>

The members .nr and .alloc of an empty strvec are both zero and the trailing NULL is provided by the shared global variable empty_strvec. Once at least one item is added, .alloc needs to be bigger than .nr to leave room for the terminating NULL. The current .alloc check only tests whether it's bigger or equal to .nr in all cases.

Tighten it depending on whether the strvec is backed by empty_strvec or not. A report for a non-zero .alloc in an empty strvec looks like this:

 # check "(&vec)->nr == (&vec)->alloc" failed at t/unit-tests/t-strvec.c:55
 #    left: 0
 #   right: 1
And .alloc being .equal to .nr in a non-empty strvec results in:
 # check "(&vec)->nr < (&vec)->alloc" failed at t/unit-tests/t-strvec.c:55
 #    left: 1
 #   right: 1
Signed-off-by: René Scharfe <l.s.r@web.de>
---
Bonus patch.
 t/unit-tests/t-strvec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/t/unit-tests/t-strvec.c b/t/unit-tests/t-strvec.c
index fdb28ba220..6a4d425840 100644
--- a/t/unit-tests/t-strvec.c
+++ b/t/unit-tests/t-strvec.c
@@ -8,7 +8,9 @@
 		if (check_uint(ARRAY_SIZE(expect), >, 0) && \
 		    check_pointer_eq(expect[ARRAY_SIZE(expect) - 1], NULL) && \
 		    check_uint((vec)->nr, ==, ARRAY_SIZE(expect) - 1) && \
-		    check_uint((vec)->nr, <=, (vec)->alloc)) { \
+		    ((vec)->v == empty_strvec ? \
+		     check_uint((vec)->nr, ==, (vec)->alloc) : \
+		     check_uint((vec)->nr, <, (vec)->alloc))) { \
 			for (size_t i = 0; i < ARRAY_SIZE(expect); i++) { \
 				if (!check_str((vec)->v[i], expect[i])) { \
 					test_msg("      i: %"PRIuMAX, i); \
--
2.45.2
Previous: Junio C HamanoNext: Junio C Hamano
Message 11 of 20 in “t-strvec: use test_msg()”
  1. t-strvec: use test_msg()René Scharfe, Jul 4, 2024
  2. Eric SunshineJul 4, 2024
  3. Phillip WoodJul 5, 2024
  4. t-strvec: use test_msg()René Scharfe, Jul 5, 2024
  5. Jeff KingJul 9, 2024
  6. René ScharfeJul 14, 2024
  7. Jeff KingJul 16, 2024
  8. René ScharfeJul 16, 2024
  9. 2/1 t-strvec: improve check_strvec() outputRené Scharfe, Jul 14, 2024
  10. Junio C HamanoJul 15, 2024
  11. 3/1 t-strvec: tighten .alloc check in check_strvecRené Scharfe, Jul 14, 2024
  12. Junio C HamanoJul 15, 2024
  13. René ScharfeJul 15, 2024
  14. Junio C HamanoJul 15, 2024
  15. t-strvec: improve check_strvec() outputRené Scharfe, Jul 14, 2024
  16. René ScharfeJul 14, 2024
  17. Jeff KingJul 16, 2024
  18. Junio C HamanoJul 16, 2024
  19. t-strvec: fix type mismatch in check_strvecRené Scharfe, Jul 16, 2024
  20. Junio C HamanoJul 16, 2024

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.