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

[PATCH] sha1_name: use strlcpy() to copy strings

From
René Scharfe <l.s.r@web.de>
Date
Feb 21, 2015, 19:55 UTC
Message-ID
<54E8E2AA.1020300@web.de>

Use strlcpy() instead of calling strncpy() and then setting the last byte of the target buffer to NUL explicitly. This shortens and simplifies the code a bit.

Signed-of-by: Rene Scharfe <l.s.r@web.de>
---
 sha1_name.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index cf2a83b..95f9f8f 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -1391,9 +1391,7 @@ static int get_sha1_with_context_1(const char *name,
 			namelen = strlen(cp);
 		}
 
-		strncpy(oc->path, cp,
-			sizeof(oc->path));
-		oc->path[sizeof(oc->path)-1] = '\0';
+		strlcpy(oc->path, cp, sizeof(oc->path));
 
 		if (!active_cache)
 			read_cache();
@@ -1443,9 +1441,7 @@ static int get_sha1_with_context_1(const char *name,
 							   name, len);
 			}
 			hashcpy(oc->tree, tree_sha1);
-			strncpy(oc->path, filename,
-				sizeof(oc->path));
-			oc->path[sizeof(oc->path)-1] = '\0';
+			strlcpy(oc->path, filename, sizeof(oc->path));
 
 			free(new_filename);
 			return ret;
-- 
2.3.0
Next: Junio C Hamano
Message 1 of 4 in “sha1_name: use strlcpy() to copy strings”
  1. sha1_name: use strlcpy() to copy stringsRené Scharfe, Feb 21, 2015
  2. Junio C HamanoFeb 22, 2015
  3. René ScharfeFeb 22, 2015
  4. Jeff KingFeb 23, 2015

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.