'C++ regcomp Segmentation Fault
enum {
TK_NOTYPE = 256, TK_EQ,
TK_NEQ, TK_AND, TK_OR,
TK_LARGE, TK_SMALL, TK_GE, TK_SE,
};
static struct rule {
const char *regex;
int token_type;
} rules[] = {
{" +", TK_NOTYPE}, // spaces
{"\n+", TK_NOTYPE}, // line feed
{"\\+", '+'}, // plus
{"-", '-'}, // minus
{"\\*", '*'}, // multiply or pointer
{"/", '/'}, // divide
{"\\^", '^'}, // power
{"0x[a-f,0-9]+", 'x'}, // heximal with sign
{"[0-9]+u", 'd'}, // unsigned decimal
{"[0-9]+", 'd'}, // decimal
{"\\$[a-z,0-9]{0,3}", 'r'}, // registers
{"[a-z][a-z,-,0-9]*", 'v'}, // variable
{"\\(", '('}, // left bracket
{"\\)", ')'}, // right bracket
{"\\[", '['}, // left mid-size bracket
{"\\]", ']'}, // right mid-size bracket
{"==", TK_EQ}, // equal
{"!=", TK_NEQ}, // not equal
{"&&", TK_AND}, // and
{"||", TK_OR}, // or
{">", TK_LARGE}, // larger
{"<", TK_SMALL}, // small
{">=", TK_GE}, // larger or equal
{"<=", TK_SE}, // smaller or equal
};
#define NR_REGEX (int)(sizeof(rules)/sizeof(rules[0]))
static regex_t re[NR_REGEX] = {};
void init_regex() {
int i;
char error_msg[128];
int ret;
printf("%d\n", NR_REGEX);
printf("%ld\n", sizeof(re)/sizeof(re[0]));
for (i = 0; i < NR_REGEX; i ++) {
ret = regcomp(&re[i], rules[i].regex, REG_EXTENDED);
if (ret != 0) {
printf("YEAH\n");
regerror(ret, &re[i], error_msg, 128);
panic("regex compilation failed: %s\n%s", error_msg, rules[i].regex);
}
}
printf("%d\n", i);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
