Skip to content

Commit

Permalink
refs #2096 fixed a crash in the scanner handling EOF in regexes
Browse files Browse the repository at this point in the history
refs #4379 fixed an error in the last commit related to this fix
  • Loading branch information
davidnich committed Jan 14, 2022
1 parent f4776b3 commit 95555ae
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 55 deletions.
2 changes: 2 additions & 0 deletions doxygen/lang/900_release_notes.dox.tmpl
Expand Up @@ -21,6 +21,8 @@
(<a href="https://github.com/qorelanguage/qore/issues/4379">issue 4379</a>)
- added a warning for constant operands with the square bracket operator that are not integers
(<a href="https://github.com/qorelanguage/qore/issues/3409">issue 3409</a>)
- fixed a bug handling EOF conditions while parsing the final part of regular expressions
(<a href="https://github.com/qorelanguage/qore/issues/2096">issue 2096</a>)

@section qore_1_0_13 Qore 1.0.13

Expand Down
5 changes: 0 additions & 5 deletions include/qore/intern/QoreLibIntern.h
Expand Up @@ -152,7 +152,6 @@ struct QoreParseContext {
int pflag = 0;
int lvids = 0;
const QoreTypeInfo* typeInfo = nullptr;
qore_type_t value_type = -1;

DLLLOCAL QoreParseContext(QoreProgram* pgm = getProgram()) : pgm(pgm) {
}
Expand All @@ -171,10 +170,6 @@ struct QoreParseContext {
pflag |= flags;
return rv;
}

DLLLOCAL bool isConstant() const {
return value_type >= NT_NOTHING && value_type <= NT_NUMBER;
}
};

class QoreParseContextFlagHelper {
Expand Down
1 change: 0 additions & 1 deletion lib/QoreLib.cpp
Expand Up @@ -518,7 +518,6 @@ bool qore_has_debug() {
}

int parse_init_value(QoreValue& val, QoreParseContext& parse_context) {
parse_context.value_type = val.getType();
if (val.hasNode()) {
AbstractQoreNode* n = val.getInternalNode();
//printd(5, "parse_init_value() n: %p '%s'\n", n, get_type_name(n));
Expand Down
2 changes: 1 addition & 1 deletion lib/QoreSquareBracketsOperatorNode.cpp
Expand Up @@ -134,7 +134,7 @@ int QoreSquareBracketsOperatorNode::parseInitImpl(QoreValue& val, QoreParseConte
edesc->concat(" and so will always evaluate to zero");
qore_program_private::makeParseWarning(getProgram(), *loc, QP_WARN_INVALID_OPERATION, "INVALID-OPERATION",
edesc);
} else if (parse_context.isConstant() && parse_context.value_type != NT_INT) {
} else if (right.isConstant() && right.getType() != NT_INT) {
// FIXME: raise exceptions with %strict-types
QoreStringNode* edesc = new QoreStringNode("the offset operand expression with the '[]' operator is a "
"constant of ");
Expand Down
119 changes: 71 additions & 48 deletions lib/scanner.lpp
Expand Up @@ -570,45 +570,47 @@ static DateTimeNode* makeRelativeTime(char* str) {
return DateTimeNode::makeRelative(0, 0, 0, hour, minute, second, us);
}

static bool isRegexModifier(QoreRegex *qr, int c) {
if (c == 'i')
static bool isRegexModifier(QoreRegex* qr, int c) {
if (c == 'i') {
qr->setCaseInsensitive();
else if (c == 's')
} else if (c == 's') {
qr->setDotAll();
else if (c == 'x')
} else if (c == 'x') {
qr->setExtended();
else if (c == 'm')
} else if (c == 'm') {
qr->setMultiline();
else if (c == 'u') {
} else if (c == 'u') {
qr->setUnicode();
} else {
return false;
}
return true;
}

static bool isRegexExtractModifier(QoreRegex *qr, int c) {
if (isRegexModifier(qr, c))
static bool isRegexExtractModifier(QoreRegex* qr, int c) {
if (isRegexModifier(qr, c)) {
return true;
if (c == 'g')
}
if (c == 'g') {
qr->setGlobal();
else
} else {
return false;
}
return true;
}

static bool isRegexSubstModifier(QoreRegexSubst *qr, int c) {
if (c == 'g')
static bool isRegexSubstModifier(QoreRegexSubst* qr, int c) {
if (c == 'g') {
qr->setGlobal();
else if (c == 'i')
} else if (c == 'i') {
qr->setCaseInsensitive();
else if (c == 's')
} else if (c == 's') {
qr->setDotAll();
else if (c == 'x')
} else if (c == 'x') {
qr->setExtended();
else if (c == 'm')
} else if (c == 'm') {
qr->setMultiline();
else if (c == 'u') {
} else if (c == 'u') {
qr->setUnicode();
} else {
return false;
Expand Down Expand Up @@ -1303,16 +1305,22 @@ RTIME PT(-?[0-9]+(\.[0-9]+)?[HMSu])+
}
<regex_subst2>{
\/ {
// get regex modifiers
int c;
do {
c = yyinput(yyscanner);
} while (isRegexSubstModifier(yylval->RegexSubst, c));
unput(c);
yylloc->restoreFirst();
BEGIN(INITIAL);
yylval->RegexSubst->parse();
return REGEX_SUBST;
// get regex modifiers
int c;
do {
c = yyinput(yyscanner);
} while (isRegexSubstModifier(yylval->RegexSubst, c));
// issue #2096: handle EOF while scanning chars manually
if (!c) {
yylval->RegexSubst->deref();
QORE_FLEX_DO_EOF
} else {
unput(c);
yylloc->restoreFirst();
BEGIN(INITIAL);
yylval->RegexSubst->parse();
return REGEX_SUBST;
}
}
\n {
yylval->RegexSubst->concatTarget('\n');
Expand Down Expand Up @@ -1396,16 +1404,22 @@ RTIME PT(-?[0-9]+(\.[0-9]+)?[HMSu])+
}
<regex_state>{
\/ {
// get regex modifiers
int c;
do {
c = yyinput(yyscanner);
} while (isRegexModifier(yylval->Regex, c));
unput(c);
yylloc->restoreFirst();
BEGIN(INITIAL);
yylval->Regex->parse([&] () { return get_loc(yylloc); });
return REGEX;
// get regex modifiers
int c;
do {
c = yyinput(yyscanner);
} while (isRegexModifier(yylval->Regex, c));
// issue #2096: handle EOF while scanning chars manually
if (!c) {
yylval->Regex->deref();
QORE_FLEX_DO_EOF
} else {
unput(c);
yylloc->restoreFirst();
BEGIN(INITIAL);
yylval->Regex->parse([&] () { return get_loc(yylloc); });
return REGEX;
}
}
\n yylval->Regex->concat('\n');
\\\/ yylval->Regex->concat('/');
Expand All @@ -1422,20 +1436,29 @@ RTIME PT(-?[0-9]+(\.[0-9]+)?[HMSu])+
}
<regex_extract_state>{
\/ {
// get regex modifiers
int c;
do {
c = yyinput(yyscanner);
} while (isRegexExtractModifier(yylval->Regex, c));
unput(c);
yylloc->restoreFirst();
BEGIN(INITIAL);
yylval->Regex->parse([&] () { return get_loc(yylloc); });
return REGEX_EXTRACT;
// get regex modifiers
int c;
do {
c = yyinput(yyscanner);
} while (isRegexExtractModifier(yylval->Regex, c));
// issue #2096: handle EOF while scanning chars manually
if (!c) {
yylval->Regex->deref();
QORE_FLEX_DO_EOF
} else {
unput(c);
yylloc->restoreFirst();
BEGIN(INITIAL);
yylval->Regex->parse([&] () { return get_loc(yylloc); });
return REGEX_EXTRACT;
}
}
\n yylval->Regex->concat('\n');
\\\/ yylval->Regex->concat('/');
\\. { yylval->Regex->concat('\\'); yylval->Regex->concat(yytext[1]); }
\\. {
yylval->Regex->concat('\\');
yylval->Regex->concat(yytext[1]);
}
[^\n\\/]+ {
char* yptr = yytext;
while (*yptr)
Expand Down

0 comments on commit 95555ae

Please sign in to comment.