1 // RUN: %clangxx -O0 -g %s -o %t && %run %t
2 /// Check that REG_STARTEND is handled correctly.
3 /// This is a regression test for https://github.com/google/sanitizers/issues/1371
4 /// Previously, on GLibc systems, the interceptor was calling __compat_regexec
5 /// (regexec@GLIBC_2.2.5) insead of the newer __regexec (regexec@GLIBC_2.3.4).
6 /// The __compat_regexec strips the REG_STARTEND flag but does not report an error
7 /// if other flags are present. This can result in infinite loops for programs that
8 /// use REG_STARTEND to find all matches inside a buffer (since ignoring REG_STARTEND
9 /// means that the search always starts from the first character).
17 /// REG_STARTEND is a BSD extension not supported everywhere.
19 void test_matched(const regex_t
*preg
, const char *string
, size_t start
,
20 size_t end
, const char *expected
) {
22 match
[0].rm_so
= start
;
24 int rv
= regexec(preg
, string
, 1, match
, REG_STARTEND
);
25 int matchlen
= (int)(match
[0].rm_eo
- match
[0].rm_so
);
26 const char *matchstart
= string
+ match
[0].rm_so
;
28 if (expected
== nullptr) {
29 fprintf(stderr
, "ERROR: expected no match but got '%.*s'\n",
30 matchlen
, matchstart
);
32 } else if ((size_t)matchlen
!= strlen(expected
) ||
33 memcmp(matchstart
, expected
, strlen(expected
)) != 0) {
34 fprintf(stderr
, "ERROR: expected '%s' match but got '%.*s'\n",
35 expected
, matchlen
, matchstart
);
38 } else if (rv
== REG_NOMATCH
) {
39 if (expected
!= nullptr) {
40 fprintf(stderr
, "ERROR: expected '%s' match but got no match\n", expected
);
44 printf("ERROR: unexpected regexec return value %d\n", rv
);
51 int rv
= regcomp(®ex
, "[A-Z][A-Z]", 0);
53 test_matched(®ex
, "ABCD", 0, 4, "AB");
54 test_matched(®ex
, "ABCD", 0, 1, nullptr); // Not long enough
55 test_matched(®ex
, "ABCD", 1, 4, "BC");
56 test_matched(®ex
, "ABCD", 1, 2, nullptr); // Not long enough
57 test_matched(®ex
, "ABCD", 2, 4, "CD");
58 test_matched(®ex
, "ABCD", 2, 3, nullptr); // Not long enough
59 test_matched(®ex
, "ABCD", 3, 4, nullptr); // Not long enough
61 printf("Successful test\n");