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

Re: [PATCH v5 03/10] test-regex: expose full regcomp() to the command line

From
Ramsay Jones <ramsay@ramsayjones.plus.com>
Date
Jan 29, 2016, 14:29 UTC
Message-ID
<56AB7745.8010003@ramsayjones.plus.com>
In-Reply-To
<20160129053143.GA17334@flurp.local>
On 29/01/16 05:31, Eric Sunshine wrote:
Show 70 quoted lines
> On Thu, Jan 28, 2016 at 06:56:16PM +0700, Nguyễn Thái Ngọc Duy wrote:
>> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
>> ---
>> diff --git a/test-regex.c b/test-regex.c
>> @@ -1,19 +1,63 @@
>>  int main(int argc, char **argv)
>>  {
>> -	char *pat = "[^={} \t]+";
>> -	char *str = "={}\nfred";
>> +	const char *pat;
>> +	const char *str;
>> +	int flags = 0;
>>  	regex_t r;
>>  	regmatch_t m[1];
>>  
>> -	if (regcomp(&r, pat, REG_EXTENDED | REG_NEWLINE))
>> +	if (argc == 1) {
>> +		/* special case, bug check */
>> +		pat = "[^={} \t]+";
>> +		str = "={}\nfred";
>> +		flags = REG_EXTENDED | REG_NEWLINE;
>> +	} else {
>> +		argv++;
>> +		pat = *argv++;
>> +		str = *argv++;
> 
> I realize that this is just a test program, but it might be a good
> idea to insert:
> 
>     if (argc < 3)
>         die("usage: ...");
> 
> prior to the *argv++ dereferences to give a controlled failure rather
> than an outright crash when an incorrect number of arguments is
> given.
> 
> More below...
> 
>> +		while (*argv) {
>> +			struct reg_flag *rf;
>> +			for (rf = reg_flags; rf->name; rf++)
>> +				if (!strcmp(*argv, rf->name)) {
>> +					flags |= rf->flag;
>> +					break;
>> +				}
>> +			if (!rf->name)
>> +				die("do not recognize %s", *argv);
>> +			argv++;
>> +		}
>> +		git_setup_gettext();
>> +	}
>> +
>> +	if (regcomp(&r, pat, flags))
>>  		die("failed regcomp() for pattern '%s'", pat);
>> -	if (regexec(&r, str, 1, m, 0))
>> -		die("no match of pattern '%s' to string '%s'", pat, str);
>> +	if (regexec(&r, str, 1, m, 0)) {
>> +		if (argc == 1)
>> +			die("no match of pattern '%s' to string '%s'", pat, str);
>> +		return 1;
>> +	}
>>  
>>  	/* http://sourceware.org/bugzilla/show_bug.cgi?id=3957  */
>> -	if (m[0].rm_so == 3) /* matches '\n' when it should not */
>> +	if (argc == 1 && m[0].rm_so == 3) /* matches '\n' when it should not */
>>  		die("regex bug confirmed: re-build git with NO_REGEX=1");
> 
> Again, I realize that this is just a test program, but sprinkling
> this 'argc == 1' special case throughout the code makes it
> unnecessarily difficult to follow. 
I completely agree!
Show 27 quoted lines
>                                     Some alternatives:
> 
> 1. Rename the existing test-regex to test-regex-bug (or
>    test-regex-bugs), and then name the new general purpose program
>    test-regex.
> 
> 2. Drop the special case altogether and have the program emit the
>    matched text on stdout (in addition to the exit code indicating
>    success/failure). Most callers will care only about the exit
>    status, but the one special case in t0070 which wants to check for
>    the glibc bug can do so itself:
> 
>     test_expect_success 'check for a bug in the regex routines' '
>         # if this test fails, re-build git with NO_REGEX=1
>         printf "fred" >expect &&
>         test-regex "[^={} \t]+" "={}\nfred" EXTENDED NEWLINE >actual &&
>         test_cmp expect actual
>     '
> 
>    Of course, that doesn't actually work because "\n" in the 'str'
>    argument isn't really a newline, so test-regex would have to do a
>    bit of preprocessing of 'str' first (which might be as simple as
>    calling unquote_c_style() or something).
> 
> 3. [less desirable] Move the 'argc == 1' special case to its own
>    function, which will result in a bit of duplicated code, but the
>    result should at least be easier to follow.

I think this is the most desirable (and was going to be my first suggestion); the duplication is minimal and it makes the code _much_ easier to follow. [I suppose separate test programs (ie. point 1) would be my second choice.]

ATB, Ramsay Jones

Previous: Eric SunshineNext: Nguyễn Thái Ngọc Duy
Message 6 of 56 in “Fix icase grep on non-ascii”
  1. 00/10 Fix icase grep on non-asciiNguyễn Thái Ngọc Duy, Jan 28, 2016
  2. 01/10 grep: allow -F -i combinationNguyễn Thái Ngọc Duy, Jan 28, 2016
  3. 02/10 grep: break down an "if" stmt in preparation for next changesNguyễn Thái Ngọc Duy, Jan 28, 2016
  4. 03/10 test-regex: expose full regcomp() to the command lineNguyễn Thái Ngọc Duy, Jan 28, 2016
  5. Eric SunshineJan 29, 2016
  6. Ramsay JonesJan 29, 2016
  7. 04/10 grep/icase: avoid kwsset on literal non-ascii stringsNguyễn Thái Ngọc Duy, Jan 28, 2016
  8. Eric SunshineJan 29, 2016
  9. Eric SunshineJan 29, 2016
  10. 05/10 grep/icase: avoid kwsset when -F is specifiedNguyễn Thái Ngọc Duy, Jan 28, 2016
  11. Eric SunshineJan 29, 2016
  12. 06/10 grep/pcre: prepare locale-dependent tables for icase matchingNguyễn Thái Ngọc Duy, Jan 28, 2016
  13. 07/10 gettext: add is_utf8_locale()Nguyễn Thái Ngọc Duy, Jan 28, 2016
  14. 08/10 grep/pcre: support utf-8Nguyễn Thái Ngọc Duy, Jan 28, 2016
  15. 09/10 diffcore-pickaxe: "share" regex error handling codeNguyễn Thái Ngọc Duy, Jan 28, 2016
  16. 10/10 diffcore-pickaxe: support case insensitive match on non-asciiNguyễn Thái Ngọc Duy, Jan 28, 2016
  17. Eric SunshineJan 29, 2016
  18. Junio C HamanoJan 28, 2016
  19. 00/11 Fix icase grep on non-asciiNguyễn Thái Ngọc Duy, Feb 6, 2016
  20. 01/11 grep: allow -F -i combinationNguyễn Thái Ngọc Duy, Feb 6, 2016
  21. Junio C HamanoJun 17, 2016
  22. Duy NguyenJun 18, 2016
  23. 02/11 grep: break down an "if" stmt in preparation for next changesNguyễn Thái Ngọc Duy, Feb 6, 2016
  24. Junio C HamanoFeb 9, 2016
  25. 03/11 test-regex: isolate the bug test codeNguyễn Thái Ngọc Duy, Feb 6, 2016
  26. 04/11 test-regex: expose full regcomp() to the command lineNguyễn Thái Ngọc Duy, Feb 6, 2016
  27. Eric SunshineFeb 7, 2016
  28. Junio C HamanoFeb 9, 2016
  29. 05/11 grep/icase: avoid kwsset on literal non-ascii stringsNguyễn Thái Ngọc Duy, Feb 6, 2016
  30. 06/11 grep/icase: avoid kwsset when -F is specifiedNguyễn Thái Ngọc Duy, Feb 6, 2016
  31. 07/11 grep/pcre: prepare locale-dependent tables for icase matchingNguyễn Thái Ngọc Duy, Feb 6, 2016
  32. 08/11 gettext: add is_utf8_locale()Nguyễn Thái Ngọc Duy, Feb 6, 2016
  33. 09/11 grep/pcre: support utf-8Nguyễn Thái Ngọc Duy, Feb 6, 2016
  34. 10/11 diffcore-pickaxe: "share" regex error handling codeNguyễn Thái Ngọc Duy, Feb 6, 2016
  35. 11/11 diffcore-pickaxe: support case insensitive match on non-asciiNguyễn Thái Ngọc Duy, Feb 6, 2016
  36. Eric SunshineFeb 7, 2016
  37. 00/12 nd/icase updatesNguyễn Thái Ngọc Duy, Feb 14, 2016
  38. 01/12 grep: allow -F -i combinationNguyễn Thái Ngọc Duy, Feb 14, 2016
  39. 02/12 grep: break down an "if" stmt in preparation for next changesNguyễn Thái Ngọc Duy, Feb 14, 2016
  40. 03/12 test-regex: isolate the bug test codeNguyễn Thái Ngọc Duy, Feb 14, 2016
  41. 04/12 test-regex: expose full regcomp() to the command lineNguyễn Thái Ngọc Duy, Feb 14, 2016
  42. 05/12 grep/icase: avoid kwsset on literal non-ascii stringsNguyễn Thái Ngọc Duy, Feb 14, 2016
  43. 06/12 grep/icase: avoid kwsset when -F is specifiedNguyễn Thái Ngọc Duy, Feb 14, 2016
  44. 07/12 grep/pcre: prepare locale-dependent tables for icase matchingNguyễn Thái Ngọc Duy, Feb 14, 2016
  45. 08/12 gettext: add is_utf8_locale()Nguyễn Thái Ngọc Duy, Feb 14, 2016
  46. 09/12 grep/pcre: support utf-8Nguyễn Thái Ngọc Duy, Feb 14, 2016
  47. 10/12 diffcore-pickaxe: "share" regex error handling codeNguyễn Thái Ngọc Duy, Feb 14, 2016
  48. 11/12 diffcore-pickaxe: support case insensitive match on non-asciiNguyễn Thái Ngọc Duy, Feb 14, 2016
  49. 12/12 grep.c: reuse "icase" variableNguyễn Thái Ngọc Duy, Feb 14, 2016
  50. Junio C HamanoJun 17, 2016
  51. Duy NguyenJun 18, 2016
  52. Duy NguyenJun 22, 2016
  53. Junio C HamanoJun 22, 2016
  54. Duy NguyenJun 22, 2016
  55. Junio C HamanoJun 22, 2016
  56. Junio C HamanoJun 22, 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.