CrowCpp / Crow

A Fast and Easy to use microframework for the web.
https://crowcpp.org
Other
3.3k stars 364 forks source link

Segfault while processing a mustache template #899

Open GEOEGII555 opened 2 months ago

GEOEGII555 commented 2 months ago
Program received signal SIGSEGV, Segmentation fault.
0x000000555557f460 in crow::mustache::template_t::parse (this=0x7ffffef938)
    at /usr/local/include/crow/mustache.h:521
521                                     auto& matched = actions_[blockPositions.back()];
(gdb) bt
#0  0x000000555557f460 in crow::mustache::template_t::parse (this=0x7ffffef938)
    at /usr/local/include/crow/mustache.h:521
#1  0x000000555557d268 in crow::mustache::template_t::template_t (
    this=0x7ffffef938, body="")
    at /usr/local/include/crow/mustache.h:140
#2  0x0000005555580978 in crow::mustache::compile (
    body="<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <link rel=\"stylesheet\" href=\"/static/style.css\">\n    <"...)
    at /usr/local/include/crow/mustache.h:693
#3  0x00000055555810a0 in crow::mustache::load (filename="base.html")
    at /usr/local/include/crow/mustache.h:816
#4  0x00000055555627b8 in main ()
    at main.cpp:86

The code that loads the template:

auto basePage = crow::mustache::load("base.html");
gittiver commented 2 months ago

Could you attach the template file to make it reproducible?

GEOEGII555 commented 2 months ago
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/static/style.css">
    <title>А</title>
    {{$head}}{{/head}}
</head>
<body>
    {{$navbar}}
        <nav>
            <h1>А</h1>
            <ul>
                <li><a href="/">Главная</a></li>
            </ul>
        </nav>
    {{/navbar}}
    {{#flashes}}
        <div class="message">
            {{.}}
        </div>
    {{/flashes}}
    {{$body}}

    {{/body}}
</body>
</html>
GEOEGII555 commented 2 months ago

It's trying to get the last element of blockPositions in mustache::template_t::parse, but the list is empty. actions_ only has a single element.

std::vector size 0

A block begins with a dollar and ends with a slash. That is, {{$title}} begins a "title" block and {{/title}} ends it. {{$block}} doesn't add anything to blockPositions (there's no case '$', it falls into the default case): line 617 in crow/mustache.h

Lines 617-624 - no blockPositions.emplace_back

The error message about an unmatched pair only mentions the {{#-{{/ pair, which suggests at the fact that mustache {{$block}}s weren't taken into account at all while writing the closing tag code.

Only section blocks mentioned in the exception code

The-EDev commented 3 weeks ago

Digging into the spec, the {{$ tags specify inheritance, which is an optional component of mustache that unfortunately Crow doesn't support. Which explains why they're not being taken into account.

The bug here is that Crow doesn't mention this information when an inheritance tag is used.