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

[PATCH] Use strncpy to protect from buffer overruns.

From
Steven Michalske <smichalske@gmail.com>
Date
Jun 9, 2010, 10:22 UTC
Message-ID
<1276078921-25429-1-git-send-email-smichalske@gmail.com>

is_git_directory() uses strcpy with pointer arithmitic, protect it from overflowing. Even though we currently protect higher up when we have the environment variable path passed in, we should protect the calls here.

Signed-off-by: Steven Michalske <smichalske@gmail.com>
---
 setup.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/setup.c b/setup.c
index 7e04602..0080299 100644
--- a/setup.c
+++ b/setup.c
@@ -170,22 +170,24 @@ static int is_git_directory(const char *suspect)
 	char path[PATH_MAX];
 	size_t len = strlen(suspect);
 
-	strcpy(path, suspect);
+	path[sizeof(path) - 1] = '\0';
+
+	strncpy(path, suspect, sizeof(path) - 1);
 	if (getenv(DB_ENVIRONMENT)) {
 		if (access(getenv(DB_ENVIRONMENT), X_OK))
 			return 0;
 	}
 	else {
-		strcpy(path + len, "/objects");
+		strncpy(path + len, "/objects", sizeof(path) - len - 1);
 		if (access(path, X_OK))
 			return 0;
 	}
 
-	strcpy(path + len, "/refs");
+	strncpy(path + len, "/refs", sizeof(path) - len - 1);
 	if (access(path, X_OK))
 		return 0;
 
-	strcpy(path + len, "/HEAD");
+	strncpy(path + len, "/HEAD", sizeof(path) - len - 1);
 	if (validate_headref(path))
 		return 0;
 
-- 
1.7.0.3
Next: Alex Riesen
Message 1 of 5 in “Use strncpy to protect from buffer overruns.”
  1. Use strncpy to protect from buffer overruns.Steven Michalske, Jun 9, 2010
  2. Alex RiesenJun 9, 2010
  3. Steven MichalskeJun 9, 2010
  4. Alex RiesenJun 9, 2010
  5. Steven MichalskeJun 9, 2010

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.