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

Segfault while processing a mustache template #899

Open
GEOEGII555 opened this issue Sep 11, 2024 · 4 comments
Open

Segfault while processing a mustache template #899

GEOEGII555 opened this issue Sep 11, 2024 · 4 comments
Labels
bug Something isn't working discussion The viability / implementation of the issue is up for debate
Milestone

Comments

@GEOEGII555
Copy link

GEOEGII555 commented Sep 11, 2024

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
Copy link
Member

Could you attach the template file to make it reproducible?

@gittiver gittiver added bug Something isn't working discussion The viability / implementation of the issue is up for debate labels Sep 12, 2024
@GEOEGII555
Copy link
Author

GEOEGII555 commented Sep 12, 2024

<!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
Copy link
Author

GEOEGII555 commented Sep 14, 2024

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working discussion The viability / implementation of the issue is up for debate
Projects
None yet
Development

No branches or pull requests

3 participants