| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef REGEXP_H | ||
| 2 | #define REGEXP_H | ||
| 3 | |||
| 4 | #include <regex.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include <stddef.h> | ||
| 7 | #include <stdint.h> | ||
| 8 | #include "command/error.h" | ||
| 9 | #include "util/macros.h" | ||
| 10 | |||
| 11 | #if defined(REG_ENHANCED) | ||
| 12 | // The REG_ENHANCED flag enables various extensions on macOS | ||
| 13 | // (see "enhanced features" in re_format(7)). Most of these | ||
| 14 | // extensions are enabled by default on Linux (in both glibc | ||
| 15 | // and musl) without the need for any extra flags. | ||
| 16 | #define DEFAULT_REGEX_FLAGS ((REG_EXTENDED) | (REG_ENHANCED)) | ||
| 17 | #else | ||
| 18 | // POSIX Extended Regular Expressions (ERE) are used almost | ||
| 19 | // everywhere in this codebase, except where Basic Regular | ||
| 20 | // Expressions (BRE) are explicitly called for (most notably | ||
| 21 | // in search_tag(), which is used for ctags patterns). | ||
| 22 | #define DEFAULT_REGEX_FLAGS (REG_EXTENDED) | ||
| 23 | #endif | ||
| 24 | |||
| 25 | // The REG_STARTEND flag is supported by glibc and BSDs, but ASan's | ||
| 26 | // __interceptor_regexec() still produces a "heap-buffer-overflow" | ||
| 27 | // error if the buffer isn't null-terminated. This is contrary to the | ||
| 28 | // entire point of the flag, so we simply use the portable fallback | ||
| 29 | // implementation when the flag is defined (and ostensibly supported) | ||
| 30 | // but known to cause problems. | ||
| 31 | #if defined(REG_STARTEND) && ASAN_ENABLED == 0 && MSAN_ENABLED == 0 | ||
| 32 | #define REGEXP_STARTEND_FLAG (REG_STARTEND) | ||
| 33 | #define HAVE_REG_STARTEND 1 // Always suitable for #if conditions | ||
| 34 | #else | ||
| 35 | #define REGEXP_STARTEND_FLAG 0 | ||
| 36 | #define HAVE_REG_STARTEND 0 | ||
| 37 | #endif | ||
| 38 | |||
| 39 | typedef struct { | ||
| 40 | const char *str; // Pattern string, interned by str_intern() | ||
| 41 | regex_t re; // regex(3) object, compiled with regcomp(3) | ||
| 42 | } InternedRegexp; | ||
| 43 | |||
| 44 | // Platform-specific patterns for matching word boundaries, as detected | ||
| 45 | // and initialized by regexp_get_word_boundary_tokens() | ||
| 46 | typedef struct { | ||
| 47 | char start[8]; | ||
| 48 | char end[8]; | ||
| 49 | uint8_t len; | ||
| 50 | } RegexpWordBoundaryTokens; | ||
| 51 | |||
| 52 | const regex_t *regexp_compile_or_fatal_error(const char *pattern) NONNULL_ARGS_AND_RETURN; | ||
| 53 | RegexpWordBoundaryTokens regexp_get_word_boundary_tokens(void); | ||
| 54 | bool regexp_error_msg(ErrorBuffer *ebuf, const regex_t *re, const char *pattern, int err) NONNULL_ARG(2, 3); | ||
| 55 | char *regexp_escape(const char *pattern, size_t len) NONNULL_ARGS WARN_UNUSED_RESULT; | ||
| 56 | size_t regexp_escapeb(char *buf, size_t buflen, const char *pat, size_t plen) NONNULL_ARG(1) NONNULL_ARG_IF_NONZERO_LENGTH(3, 4); | ||
| 57 | |||
| 58 | const InternedRegexp *regexp_intern(ErrorBuffer *ebuf, const char *pattern) NONNULL_ARG(2) WARN_UNUSED_RESULT; | ||
| 59 | bool regexp_is_interned(const char *pattern) NONNULL_ARGS; | ||
| 60 | void free_interned_regexps(void); | ||
| 61 | |||
| 62 | WARN_UNUSED_RESULT NONNULL_ARG(1, 2) NONNULL_ARG_IF_NONZERO_LENGTH(5, 4) | ||
| 63 | bool regexp_exec ( | ||
| 64 | const regex_t *re, | ||
| 65 | const char *buf, | ||
| 66 | size_t size, | ||
| 67 | size_t nmatch, | ||
| 68 | regmatch_t *pmatch, | ||
| 69 | int flags | ||
| 70 | ); | ||
| 71 | |||
| 72 | WARN_UNUSED_RESULT NONNULL_ARG(2, 3) | ||
| 73 | 303 | static inline bool regexp_compile(ErrorBuffer *ebuf, regex_t *re, const char *pattern, int flags) | |
| 74 | { | ||
| 75 | 303 | int err = regcomp(re, pattern, flags | DEFAULT_REGEX_FLAGS); | |
| 76 |
1/4✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 303 times.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
|
303 | return !err || regexp_error_msg(ebuf, re, pattern, err); |
| 77 | } | ||
| 78 | |||
| 79 | WARN_UNUSED_RESULT NONNULL_ARG(2) | ||
| 80 | 56 | static inline bool regexp_is_valid(ErrorBuffer *ebuf, const char *pattern, int flags) | |
| 81 | { | ||
| 82 | 56 | regex_t re; | |
| 83 |
1/2✓ Branch 3 → 4 taken 56 times.
✗ Branch 3 → 6 not taken.
|
56 | if (!regexp_compile(ebuf, &re, pattern, flags | REG_NOSUB)) { |
| 84 | return false; | ||
| 85 | } | ||
| 86 | 56 | regfree(&re); | |
| 87 | 56 | return true; | |
| 88 | } | ||
| 89 | |||
| 90 | #endif | ||
| 91 |