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

[PATCH 4/6] test-hashmap: use strbuf_getline rather than fgets

From
Jeff King <peff@peff.net>
Date
Feb 14, 2018, 18:07 UTC
Message-ID
<20180214180718.GD9919@sigill.intra.peff.net>
In-Reply-To
<20180214180322.GA9190@sigill.intra.peff.net>

Using fgets() with a fixed-size buffer can lead to lines being accidentally split across two calls if they are larger than the buffer size.

As this is just a test helper, this is unlikely to be a problem in practice. But since people may look at test helpers as reference code, it's a good idea for them to model the preferred behavior.

Signed-off-by: Jeff King <peff@peff.net>
---
 t/helper/test-hashmap.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c
index 28b913fbd6..15fc4e372f 100644
--- a/t/helper/test-hashmap.c
+++ b/t/helper/test-hashmap.c
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "hashmap.h"
+#include "strbuf.h"
 
 struct test_entry
 {
@@ -143,7 +144,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
  */
 int cmd_main(int argc, const char **argv)
 {
-	char line[1024];
+	struct strbuf line = STRBUF_INIT;
 	struct hashmap map;
 	int icase;
 
@@ -152,13 +153,13 @@ int cmd_main(int argc, const char **argv)
 	hashmap_init(&map, test_entry_cmp, &icase, 0);
 
 	/* process commands from stdin */
-	while (fgets(line, sizeof(line), stdin)) {
+	while (strbuf_getline(&line, stdin) != EOF) {
 		char *cmd, *p1 = NULL, *p2 = NULL;
 		int l1 = 0, l2 = 0, hash = 0;
 		struct test_entry *entry;
 
 		/* break line into command and up to two parameters */
-		cmd = strtok(line, DELIM);
+		cmd = strtok(line.buf, DELIM);
 		/* ignore empty lines */
 		if (!cmd || *cmd == '#')
 			continue;
@@ -262,6 +263,7 @@ int cmd_main(int argc, const char **argv)
 		}
 	}
 
+	strbuf_release(&line);
 	hashmap_free(&map, 1);
 	return 0;
 }
-- 
2.16.1.464.gc4bae515b7
Previous: Jeff KingNext: Jeff King
Message 6 of 11 in “minor test-hashmap fixes”
  1. 0/6 minor test-hashmap fixesJeff King, Feb 14, 2018
  2. 1/6 test-hashmap: use ALLOC_ARRAY rather than bare mallocJeff King, Feb 14, 2018
  3. Code AIFeb 14, 2018
  4. 2/6 test-hashmap: check allocation computation for overflowJeff King, Feb 14, 2018
  5. 3/6 test-hashmap: use xsnprintf rather than snprintfJeff King, Feb 14, 2018
  6. 4/6 test-hashmap: use strbuf_getline rather than fgetsJeff King, Feb 14, 2018
  7. 5/6 test-hashmap: simplify alloc_test_entryJeff King, Feb 14, 2018
  8. Junio C HamanoFeb 14, 2018
  9. 6/6 test-hashmap: use "unsigned int" for hash storageJeff King, Feb 14, 2018
  10. Stefan BellerFeb 14, 2018
  11. Jeff KingFeb 14, 2018

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.