Skip to content

Parser truncates trailing newlines in multiline quoted scalars #1511

Open

Description

@LucaCiucci

With yaml-cpp 0.9.0 or the current master (c5e65360), parsing the following multiline single-quoted scalar appears to lose one trailing newline:

value: 'a

  b


  '

Expected value:

"a\nb\n\n"

Actual value:

"a\nb\n"

The YAML above was generated by PyYAML's safe_dump, and PyYAML reads it back as the expected five-byte string. Equivalent double-quoted and literal-block representations also produce the expected value in yaml-cpp.

Reproducer

#include <yaml-cpp/yaml.h>

#include <iostream>
#include <string>

int main() {
    const std::string input =
        "value: 'a\n"
        "\n"
        "  b\n"
        "\n"
        "\n"
        "  '\n";

    const std::string expected = "a\nb\n\n";
    const auto actual = YAML::Load(input)["value"].as<std::string>();

    const auto double_quoted =
        YAML::Load("value: \"a\\nb\\n\\n\"\n")["value"].as<std::string>();

    const auto literal =
        YAML::Load("value: |+\n  a\n  b\n\n")["value"].as<std::string>();

    std::cout << "Expected size:      " << expected.size() << '\n'
              << "Single-quoted size: " << actual.size() << '\n'
              << "Double-quoted size: " << double_quoted.size() << '\n'
              << "Literal size:       " << literal.size() << '\n';

    return actual == expected ? 0 : 1;
}

Output:

Expected size:      5
Single-quoted size: 4
Double-quoted size: 5
Literal size:       5

I didn't test older versions

Activity

  1. SGSSGene commented on Oct 15, 2026

    @SGSSGene
    Collaborator

    Thank you for reporting and sending in a PR.

    Could it be that your double_quoted example is false?
    The backslash of \n should not be escaped:

    const auto double_quoted =
            YAML::Load("value: \"a\nb\n\n\"\n")["value"].as<std::string>();
    

    This would mean that the issue does not only appear with 'single-quoted scalar' but also with 'double-quoted scalars'.
    (This is also what I would assume happens, when looking at your PR).

    The biggest question I know have is: Why did we not notice this error earlier?

  2. LucaCiucci commented on Oct 15, 2026

    @LucaCiucci
    Author

    Hi @SGSSGene, thank you for looking into this.

    I confirm that this also happens with multiline double-quoted scalars, and the regression test in #1512 covers both quote styles. I'll update the issue title.

    The \\n is intentional: it produces the following YAML, containing \n escape sequences:

    value: "a\nb\n\n"
    

    This was included as a control case (it preserves the expected value, whereas the equivalent representation using physical line breaks loses a trailing newline).

    As for why it went unnoticed, I guess this edge case is uncommon and usually doesn't produce obvious effects. I noticed it while trying to reach parity with the Rust implementation of an internal tool.

  3. changed the title [-]Parser drops a trailing newline in a multiline single-quoted scalar[/-] [+]Parser truncates trailing newlines in multiline quoted scalars[/+] on Oct 15, 2026
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