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

[PATCH v3 03/18] grep: submodule-related case statements should die if new fields are added

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Apr 20, 2017, 21:23 UTC
Message-ID
<20170420212345.7408-4-avarab@gmail.com>
In-Reply-To
<20170420212345.7408-1-avarab@gmail.com>

Change two case statements added in commit 0281e487fd ("grep: optionally recurse into submodules", 2016-12-16) so that they die if new GREP_PATTERN_* enum fields are added without updating them.

These case statements currently check for an exhaustive list of fields, but if a new field is added it's easy to introduce a bug here where the code will start subtly doing the wrong thing, e.g. if a new pattern type is added we'll fall through to GREP_PATTERN_TYPE_UNSPECIFIED, i.e. the "basic" POSIX regular expressions.

This should arguably be done for the switch(opt->binary) case-statement as well, but isn't trivial to add since that code isn't currently working with an exhaustive list.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/grep.c | 4 ++++
 1 file changed, 4 insertions(+)
diff --git a/builtin/grep.c b/builtin/grep.c
index 3ffb5b4e81..be3dbd6957 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -495,6 +495,8 @@ static void compile_submodule_options(const struct grep_opt *opt,
 		break;
 	case GREP_PATTERN_TYPE_UNSPECIFIED:
 		break;
+	default:
+		die("BUG: Added a new grep pattern type without updating switch statement");
 	}
 
 	for (pattern = opt->pattern_list; pattern != NULL;
@@ -515,6 +517,8 @@ static void compile_submodule_options(const struct grep_opt *opt,
 		case GREP_PATTERN_BODY:
 		case GREP_PATTERN_HEAD:
 			break;
+		default:
+			die("BUG: Added a new grep token type without updating case statement");
 		}
 	}
 
-- 
2.11.0
Previous: Ævar Arnfjörð BjarmasonNext: Brandon Williams
Message 5 of 22 in “PCRE v1 improvements & PCRE v2 support”
  1. 00/18 PCRE v1 improvements & PCRE v2 supportÆvar Arnfjörð Bjarmason, Apr 20, 2017
  2. 01/18 grep: amend submodule recursion test in preparation for rx engine testingÆvar Arnfjörð Bjarmason, Apr 20, 2017
  3. Brandon WilliamsApr 20, 2017
  4. 02/18 grep: add tests for grep pattern types being passed to submodulesÆvar Arnfjörð Bjarmason, Apr 20, 2017
  5. 03/18 grep: submodule-related case statements should die if new fields are addedÆvar Arnfjörð Bjarmason, Apr 20, 2017
  6. Brandon WilliamsApr 20, 2017
  7. Jeff KingApr 20, 2017
  8. 04/18 grep: remove redundant regflags assignment under PCREÆvar Arnfjörð Bjarmason, Apr 20, 2017
  9. 05/18 Makefile & configure: reword outdated comment about PCREÆvar Arnfjörð Bjarmason, Apr 20, 2017
  10. 06/18 grep: add a test for backreferences in PCRE patternsÆvar Arnfjörð Bjarmason, Apr 20, 2017
  11. 08/18 log: add -P as a synonym for --perl-regexpÆvar Arnfjörð Bjarmason, Apr 20, 2017
  12. 09/18 grep & rev-list doc: stop promising libpcre for --perl-regexpÆvar Arnfjörð Bjarmason, Apr 20, 2017
  13. 10/18 grep: make grep.patternType=[pcre|pcre1] a synonym for "perl"Ævar Arnfjörð Bjarmason, Apr 20, 2017
  14. 11/18 test-lib: rename the LIBPCRE prerequisite to PCREÆvar Arnfjörð Bjarmason, Apr 20, 2017
  15. 12/18 grep: change the internal PCRE macro names to be PCRE1Ævar Arnfjörð Bjarmason, Apr 20, 2017
  16. 13/18 grep: change the internal PCRE code & header names to be PCRE1Ævar Arnfjörð Bjarmason, Apr 20, 2017
  17. 07/18 log: add exhaustive tests for pattern style options & configÆvar Arnfjörð Bjarmason, Apr 20, 2017
  18. 14/18 perf: add a performance comparison test of grep -E and -PÆvar Arnfjörð Bjarmason, Apr 20, 2017
  19. 18/18 Makefile & configure: make PCRE v2 the default PCRE implementationÆvar Arnfjörð Bjarmason, Apr 20, 2017
  20. 16/18 grep: add support for PCRE v2Ævar Arnfjörð Bjarmason, Apr 20, 2017
  21. 17/18 grep: remove support concurrent use of both PCRE v1 & v2Ævar Arnfjörð Bjarmason, Apr 20, 2017
  22. 15/18 grep: add support for the PCRE v1 JIT APIÆvar Arnfjörð Bjarmason, Apr 20, 2017

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.