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

[PATCH 4/4] dir.c: avoid "exceeds maximum object size" error with GCC v12.x

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
May 24, 2022, 00:23 UTC
Message-ID
<365889ee96e37dc9dcbe60d98880eb256dae90ee.1653351786.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1238.git.1653351786.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

Technically, the pointer difference `end - start` _could_ be negative, and when cast to an (unsigned) `size_t` that would cause problems. In this instance, the symptom is:

dir.c: In function 'git_url_basename':
dir.c:3087:13: error: 'memchr' specified bound [9223372036854775808, 0]
       exceeds maximum object size 9223372036854775807
       [-Werror=stringop-overread]
    CC ewah/bitmap.o
 3087 |         if (memchr(start, '/', end - start) == NULL
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

While it is a bit far-fetched to think that `end` (which is defined as `repo + strlen(repo)`) and `start` (which starts at `repo` and never steps beyond the NUL terminator) could result in such a negative difference, GCC has no way of knowing that.

See also https://gcc.gnu.org/bugzilla//show_bug.cgi?id=85783.
Let's just add a safety check, primarily for GCC's benefit.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 dir.c | 9 +++++++++
 1 file changed, 9 insertions(+)
diff --git a/dir.c b/dir.c
index 5aa6fbad0b7..ea78f606230 100644
--- a/dir.c
+++ b/dir.c
@@ -3076,6 +3076,15 @@ char *git_url_basename(const char *repo, int is_bundle, int is_bare)
 			end--;
 	}
 
+	/*
+	 * It should not be possible to overflow `ptrdiff_t` by passing in an
+	 * insanely long URL, but GCC does not know that and will complain
+	 * without this check.
+	 */
+	if (end - start < 0)
+		die(_("No directory name could be guessed.\n"
+		      "Please specify a directory on the command line"));
+
 	/*
 	 * Strip trailing port number if we've got only a
 	 * hostname (that is, there is no dir separator but a
-- 
gitgitgadget
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 18 of 23 in “ci: fix windows-build with GCC v12.x”
  1. 0/4 ci: fix windows-build with GCC v12.xJohannes Schindelin via GitGitGadget, May 24, 2022
  2. 1/4 compat/win32/syslog: fix use-after-reallocJohannes Schindelin via GitGitGadget, May 24, 2022
  3. Johannes SchindelinMay 24, 2022
  4. Junio C HamanoMay 24, 2022
  5. 2/4 nedmalloc: avoid new compile errorJohannes Schindelin via GitGitGadget, May 24, 2022
  6. Ævar Arnfjörð BjarmasonMay 24, 2022
  7. René ScharfeMay 24, 2022
  8. Johannes SchindelinMay 24, 2022
  9. Ævar Arnfjörð BjarmasonMay 24, 2022
  10. 3/4 http.c: avoid danging pointer to local variable `finished`Johannes Schindelin via GitGitGadget, May 24, 2022
  11. Ævar Arnfjörð BjarmasonMay 24, 2022
  12. Junio C HamanoMay 24, 2022
  13. Johannes SchindelinMay 24, 2022
  14. Ævar Arnfjörð BjarmasonMay 24, 2022
  15. Junio C HamanoMay 24, 2022
  16. Johannes SchindelinMay 25, 2022
  17. Ævar Arnfjörð BjarmasonMay 25, 2022
  18. 4/4 dir.c: avoid "exceeds maximum object size" error with GCC v12.xJohannes Schindelin via GitGitGadget, May 24, 2022
  19. Ævar Arnfjörð BjarmasonMay 24, 2022
  20. Johannes SchindelinMay 24, 2022
  21. Derrick StoleeMay 25, 2022
  22. Junio C HamanoMay 25, 2022
  23. Jeff HostetlerMay 24, 2022

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.