Crow icon indicating copy to clipboard operation
Crow copied to clipboard

Segfault while processing a mustache template

Open GEOEGII555 opened this issue 1 year ago • 4 comments

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");

GEOEGII555 avatar Sep 11 '24 07:09 GEOEGII555

Could you attach the template file to make it reproducible?

gittiver avatar Sep 12 '24 14:09 gittiver

<!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 avatar Sep 12 '24 17:09 GEOEGII555

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

GEOEGII555 avatar Sep 14 '24 09:09 GEOEGII555

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.

The-EDev avatar Oct 26 '24 05:10 The-EDev