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

[PATCHv2 09/36] attr.c: plug small leak in parse_attr_line()

From
Stefan Beller <sbeller@google.com>
Date
Oct 28, 2016, 18:54 UTC
Message-ID
<20161028185502.8789-10-sbeller@google.com>
In-Reply-To
<20161028185502.8789-1-sbeller@google.com>
From: Junio C Hamano <gitster@pobox.com>

If any error is noticed after the match_attr structure is allocated, we shouldn't just return NULL from this function.

Add a fail_return label that frees the allocated structure and returns NULL, and consistently jump there when we want to return NULL after cleaning up.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
---
 attr.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/attr.c b/attr.c
index f7cf7ae306..d180c7833e 100644
--- a/attr.c
+++ b/attr.c
@@ -223,7 +223,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 		if (!macro_ok) {
 			fprintf(stderr, "%s not allowed: %s:%d\n",
 				name, src, lineno);
-			return NULL;
+			goto fail_return;
 		}
 		is_macro = 1;
 		name += strlen(ATTRIBUTE_MACRO_PREFIX);
@@ -233,7 +233,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 			fprintf(stderr,
 				"%.*s is not a valid attribute name: %s:%d\n",
 				namelen, name, src, lineno);
-			return NULL;
+			goto fail_return;
 		}
 	}
 	else
@@ -246,7 +246,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 	for (cp = states, num_attr = 0; *cp; num_attr++) {
 		cp = parse_attr(src, lineno, cp, NULL);
 		if (!cp)
-			return NULL;
+			goto fail_return;
 	}
 
 	res = xcalloc(1,
@@ -267,7 +267,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 		if (res->u.pat.flags & EXC_FLAG_NEGATIVE) {
 			warning(_("Negative patterns are ignored in git attributes\n"
 				  "Use '\\!' for literal leading exclamation."));
-			return NULL;
+			goto fail_return;
 		}
 	}
 	res->is_macro = is_macro;
@@ -283,6 +283,10 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 	}
 
 	return res;
+
+fail_return:
+	free(res);
+	return NULL;
 }
 
 /*
-- 
2.10.1.714.ge3da0db
Previous: Stefan BellerNext: Stefan Beller
Message 9 of 50 in “[PATCHv2 00/36] Revamp the attr subsystem!”
  1. Stefan BellerOct 28, 2016
  2. 01/36 commit.c: use strchrnul() to scan for one lineStefan Beller, Oct 28, 2016
  3. 02/36 attr.c: use strchrnul() to scan for one lineStefan Beller, Oct 28, 2016
  4. 03/36 attr.c: update a stale comment on "struct match_attr"Stefan Beller, Oct 28, 2016
  5. 04/36 attr.c: explain the lack of attr-name syntax check in parse_attr()Stefan Beller, Oct 28, 2016
  6. 06/36 attr.c: mark where #if DEBUG ends more clearlyStefan Beller, Oct 28, 2016
  7. 07/36 attr.c: simplify macroexpand_one()Stefan Beller, Oct 28, 2016
  8. 05/36 attr.c: complete a sentence in a commentStefan Beller, Oct 28, 2016
  9. 09/36 attr.c: plug small leak in parse_attr_line()Stefan Beller, Oct 28, 2016
  10. 08/36 attr.c: tighten constness around "git_attr" structureStefan Beller, Oct 28, 2016
  11. 10/36 attr: rename function and struct related to checking attributesStefan Beller, Oct 28, 2016
  12. 14/36 attr: retire git_check_attrs() APIStefan Beller, Oct 28, 2016
  13. 19/36 attr.c: add push_stack() helperStefan Beller, Oct 28, 2016
  14. 25/36 attr.c: outline the future plans by heavily commentingStefan Beller, Oct 28, 2016
  15. 18/36 attr: support quoting pathname patterns in C styleStefan Beller, Oct 28, 2016
  16. 20/36 attr.c: pass struct git_attr_check down the callchainStefan Beller, Oct 28, 2016
  17. 21/36 attr.c: rename a local variable checkStefan Beller, Oct 28, 2016
  18. 22/36 attr.c: correct ugly hack for git_all_attrs()Stefan Beller, Oct 28, 2016
  19. 28/36 attr: keep attr stack for each checkStefan Beller, Oct 28, 2016
  20. Junio C HamanoOct 28, 2016
  21. 23/36 attr.c: introduce empty_attr_check_elems()Stefan Beller, Oct 28, 2016
  22. 26/36 attr: make git_check_attr_counted staticStefan Beller, Oct 28, 2016
  23. 27/36 attr: convert to new threadsafe APIStefan Beller, Oct 28, 2016
  24. Junio C HamanoOct 28, 2016
  25. Stefan BellerOct 28, 2016
  26. Junio C HamanoOct 28, 2016
  27. Johannes SixtOct 29, 2016
  28. 29/36 Documentation: fix a typoStefan Beller, Oct 28, 2016
  29. 34/36 submodule update: add `--init-default-path` switchStefan Beller, Oct 28, 2016
  30. SQUASH to: submodule update: add `--init-default-path` switchStefan Beller, Nov 3, 2016
  31. 32/36 pathspec: allow querying for attributesStefan Beller, Oct 28, 2016
  32. Duy NguyenNov 9, 2016
  33. Stefan BellerNov 9, 2016
  34. 36/36 completion: clone can initialize specific submodulesStefan Beller, Oct 28, 2016
  35. 31/36 pathspec: move prefix check out of the inner loopStefan Beller, Oct 28, 2016
  36. 35/36 clone: add --init-submodule=<pathspec> switchStefan Beller, Oct 28, 2016
  37. 33/36 pathspec: allow escaped query valuesStefan Beller, Oct 28, 2016
  38. 30/36 pathspec: move long magic parsing out of prefix_pathspecStefan Beller, Oct 28, 2016
  39. 24/36 attr.c: always pass check[] to collect_some_attrs()Stefan Beller, Oct 28, 2016
  40. 13/36 attr: convert git_check_attrs() callers to use the new APIStefan Beller, Oct 28, 2016
  41. 17/36 attr: expose validity check for attribute namesStefan Beller, Oct 28, 2016
  42. 16/36 attr: add counted string version of git_attr()Stefan Beller, Oct 28, 2016
  43. 15/36 attr: add counted string version of git_check_attr()Stefan Beller, Oct 28, 2016
  44. 12/36 attr: convert git_all_attrs() to use "struct git_attr_check"Stefan Beller, Oct 28, 2016
  45. 11/36 attr: (re)introduce git_check_attr() and struct git_attr_checkStefan Beller, Oct 28, 2016
  46. Junio C HamanoOct 28, 2016
  47. Stefan BellerOct 28, 2016
  48. Ramsay JonesOct 28, 2016
  49. Johannes SixtNov 3, 2016
  50. Stefan BellerNov 3, 2016

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.