Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex Insufficient Memory in Template Parser #630

Closed
abtris opened this issue Aug 12, 2016 · 3 comments
Closed

Regex Insufficient Memory in Template Parser #630

abtris opened this issue Aug 12, 2016 · 3 comments

Comments

@abtris
Copy link
Contributor

abtris commented Aug 12, 2016

@bobdercole opened apiaryio/snowcrash#399

Hello,

I am using API Blueprint Sublime Text Plugin and Aglio. I'm having a common issue which I've narrowed down to Snowcrash's URI template parser. The parser seems to fail with very long URI templates.

Here is a working example:

[/test{?abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcab}]

Here is a failing example (notice the additional character at the end):

[/test{?abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc}]

I've tried running the win regex match code with the above strings and this expression in an isolated environment and was able to reproduce the issue. I'm on Windows built with MSVC2015. The regex_error exception is as follows:

There was insufficient memory to determine whether the regular expression could match the specified character sequence.

For the record, I've tried running Aglio with the provided Dockerfile and it works fine with long URI templates.

Any ideas? Thank you!

@abtris
Copy link
Contributor Author

abtris commented Sep 16, 2016

@kylef commented

Hi @bobdercole thanks for the detailed report. Would you be able to share how much memory you have available while running this code?

@abtris
Copy link
Contributor Author

abtris commented Sep 20, 2016

@bobdercole commented

Sure. Here is my isolated sample code along with GlobalMemoryStatusEx. I'm not sure if GlobalMemoryStatusEx provides everything you're looking for. Let me know if there is something else I can share.

#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <regex>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define DIV 1024
#define WIDTH 7

void main()
{
    std::regex pattern("^([?|#|+|&]?(([A-Z|a-z|0-9|_|,])*|(%[A-F|a-f|0-9]{2})*)*\\*?)$", std::regex_constants::extended);
    std::string input = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc";

    bool result = false;

    try
    {
        result = regex_search(input, pattern);
    }
    catch (const std::regex_error& e)
    {
        std::cout << "regex_error caught: " << e.what() << std::endl;
    }

    std::cout << "regex_search result: " << result << std::endl;

    MEMORYSTATUSEX statex;
    statex.dwLength = sizeof(statex);
    GlobalMemoryStatusEx(&statex);

    _tprintf(TEXT("There is  %*ld percent of memory in use.\n"), WIDTH, statex.dwMemoryLoad);
    _tprintf(TEXT("There are %*I64d total KB of physical memory.\n"), WIDTH, statex.ullTotalPhys / DIV);
    _tprintf(TEXT("There are %*I64d free  KB of physical memory.\n"), WIDTH, statex.ullAvailPhys / DIV);
    _tprintf(TEXT("There are %*I64d total KB of paging file.\n"), WIDTH, statex.ullTotalPageFile / DIV);
    _tprintf(TEXT("There are %*I64d free  KB of paging file.\n"), WIDTH, statex.ullAvailPageFile / DIV);
    _tprintf(TEXT("There are %*I64d total KB of virtual memory.\n"), WIDTH, statex.ullTotalVirtual / DIV);
    _tprintf(TEXT("There are %*I64d free  KB of virtual memory.\n"), WIDTH, statex.ullAvailVirtual / DIV);
    _tprintf(TEXT("There are %*I64d free  KB of extended memory.\n"), WIDTH, statex.ullAvailExtendedVirtual / DIV);
}

Here is the output:

regex_error caught: regex_error(error_stack): There was insufficient memory to determine whether the regular expression could match the specified character sequence.
regex_search result: 0
There is       33 percent of memory in use.
There are 16681676 total KB of physical memory.
There are 11175972 free  KB of physical memory.
There are 33361492 total KB of paging file.
There are 27716812 free  KB of paging file.
There are 2097024 total KB of virtual memory.
There are 2083268 free  KB of virtual memory.
There are       0 free  KB of extended memory.

@kylef
Copy link
Member

kylef commented Mar 23, 2020

As of Drafter 5.0.0-rc.1, this issue is no longer applicable as the parser for URI Templates does not use regex and suffer this problem.

@kylef kylef closed this as completed Mar 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants