Skip to content

incorrect regex compilation #19

Description

@Sinfolke

I honestly didn't learn the c++ and i want to use compiler just to create a quick binary file.
The part of ts file:

/**
 * Defines whether a line contains unclosed brackets.
 * @param symbol the close and open symbol. Example: {|}, '{' is opend and '}' is close symbol     
 * @param line the line to be searched in
 * @returns undefined if all brackets on a line are closed or array with amount of unclosed brackets and id of last unclosed
 */
function unclosed(symbol:string, line: string) {
    let openedBrackets = 0;
    let match;
    let id = 0;
    const splitted = symbol.split('|');
    const escapeCharactersRegex = /(\(|\)|\[|\]|\{|\}|\*|\||\$|\+|\\)/;
    const escapeStart = !!splitted[0].match(escapeCharactersRegex);
    const escapeEnd = !!splitted[1].match(escapeCharactersRegex);
    const openRegex = new RegExp(`(?<!\\\\)(?:\\\\\\\\)*${escapeStart ? '\\' : ''}${splitted[0]}`, "g");
    const closeRegex = new RegExp(`(?<!\\\\)(?:\\\\\\\\)*${escapeEnd ? '\\' : ''}${splitted[1]}`, "g");
    while ((match = openRegex.exec(line)) !== null) {
        openedBrackets++;
        id = match.index + 1;
    }
    while ((match = closeRegex.exec(line)) !== null) {
        openedBrackets--;
    }
    if (openedBrackets % 2 != 0) {
        return [openedBrackets, id];
    }
}

The output c++ file:

any unclosed(string symbol, string line)
{
    auto openedBrackets = 0;
    any match;
    auto id = 0;
    auto splitted = symbol->split(STR("|"));
    auto escapeCharactersRegex = (new RegExp(STR("(\(|\)|\[|\]|\{|\}|\*|\||\$|\+|\\")));
    auto escapeStart = !!const_(splitted)[0]->match(escapeCharactersRegex);
    auto escapeEnd = !!const_(splitted)[1]->match(escapeCharactersRegex);
    auto openRegex = std::make_shared<RegExp>(STR("(?<!\\)(?:\\\\)*") + (escapeStart) ? STR("\") : string_empty + string_empty + const_(splitted)[0] + string_empty, STR("g"));
    auto closeRegex = std::make_shared<RegExp>(STR("(?<!\\)(?:\\\\)*") + (escapeEnd) ? STR("\") : string_empty + string_empty + const_(splitted)[1] + string_empty, STR("g"));
    while ((match = openRegex->exec(line)) != nullptr)
    {
        openedBrackets++;
        id = match["index"] + 1;
    }
    while ((match = closeRegex->exec(line)) != nullptr)
    {
        openedBrackets--;
    }
    if (not_equals(openedBrackets % 2, 0)) {
        return array<js::number>{ openedBrackets, id };
    }
};

The issue is that it escaped the end quote (escapeStart) ? STR("") and so it is not considered end of string, the same is on the second line which contains almost the same pattern

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions