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

[PATCH 3/6] strbuf: use size_t for length in intermediate variables

From
Jeff King <peff@peff.net>
Date
Jul 24, 2018, 10:51 UTC
Message-ID
<20180724105108.GC17165@sigill.intra.peff.net>
In-Reply-To
<20180724104852.GA14638@sigill.intra.peff.net>

A few strbuf functions store the length of a strbuf in a temporary variable. We should always use size_t for this, as it's possible for a strbuf to exceed an "int" (e.g., a 2GB string on a 64-bit system). This is unlikely in practice, but we should try to behave sensibly on silly or malicious input.

Signed-off-by: Jeff King <peff@peff.net>
---
 strbuf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index e79758b942..6ff1f80129 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -209,7 +209,7 @@ void strbuf_list_free(struct strbuf **sbs)
 
 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
 {
-	int len = a->len < b->len ? a->len: b->len;
+	size_t len = a->len < b->len ? a->len: b->len;
 	int cmp = memcmp(a->buf, b->buf, len);
 	if (cmp)
 		return cmp;
@@ -389,7 +389,7 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
 
 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
 {
-	int i, len = src->len;
+	size_t i, len = src->len;
 
 	for (i = 0; i < len; i++) {
 		if (src->buf[i] == '%')
@@ -960,7 +960,7 @@ static size_t cleanup(char *line, size_t len)
  */
 void strbuf_stripspace(struct strbuf *sb, int skip_comments)
 {
-	int empties = 0;
+	size_t empties = 0;
 	size_t i, j, len, newlen;
 	char *eol;
 
-- 
2.18.0.542.g2bf2fc4f7e
Previous: Jeff KingNext: Jeff King
Message 4 of 9 in “use size_t in iconv/strbuf”
  1. 0/6 use size_t in iconv/strbufJeff King, Jul 24, 2018
  2. 1/6 reencode_string: use st_add/st_mult helpersJeff King, Jul 24, 2018
  3. 2/6 reencode_string: use size_t for string lengthsJeff King, Jul 24, 2018
  4. 3/6 strbuf: use size_t for length in intermediate variablesJeff King, Jul 24, 2018
  5. 4/6 strbuf_readlink: use ssize_tJeff King, Jul 24, 2018
  6. 5/6 pass st.st_size as hint for strbuf_readlink()Jeff King, Jul 24, 2018
  7. Torsten BögershausenJul 25, 2018
  8. Jeff KingJul 26, 2018
  9. 6/6 strbuf_humanise: use unsigned variablesJeff King, Jul 24, 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.