Root-level block scalar with content at column 0 fails to load ("unexpected token after end of document")

Aperta
#1,514 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
data

Direzione di ricerca

Start in Scanner::ScanBlockScalar in src/scantoken.cpp, where params.indent is set from GetTopIndent(), then read ScanScalar in src/scanscalar.cpp to see how FOLD_BLOCK and the indentation check decide what counts as content. Reproduce the four root-level inputs from the issue (--- > and --- | with column-0 content) and confirm they throw. Done when those inputs return the expected strings and the existing test results are unchanged.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Location (jbeder/yaml-cpp @ default branch c5e6536):

  ScanScalarParams params;
  params.indent = 1;
  params.detectIndent = true;
...
  // set the initial indentation
  if (GetTopIndent() >= 0)
    params.indent += GetTopIndent();

Problem
When a literal (|) or folded (>) block scalar is the root node of a document and its content lines start at column 0, YAML::Load throws unexpected token after end of document instead of returning the scalar. Scanner::ScanBlockScalar starts with params.indent = 1 and only adds the parent indent when GetTopIndent() >= 0, so at the root the scanner requires content at column >= 1 and the column-0 lines are not consumed as part of the scalar.

In YAML 1.2.2 a document's root node is parsed with indentation n = -1: 9.1.3 Bare Documents, production [207] l-bare-document ::= s-l+block-node(-1,BLOCK-IN), which 9.1.4 Explicit Documents ([208]) reuses after ---. A block scalar's content is at n+m with auto-detected m >= 1 ([170] c-l+literal(n), [174] c-l+folded(n), 8.1.1.1), so column 0 is a valid content indentation at the root. The spec itself uses this form: Example 9.3 (| then %!PS-Adobe-2.0 # Not the first line) and Example 9.5 (--- | then %!PS-Adobe-2.0) both expect "%!PS-Adobe-2.0\n".

Expected values:

Input Expected value
"--- >\nline1\nline2\nline3\n" "line1 line2 line3\n"
"--- |\nline1\nline2\nline3\n" "line1\nline2\nline3\n"
"--- >\nline1\n# no comment\nline3\n" "line1 # no comment line3\n"
"--- |+\nline1\nline2\n\n" "line1\nline2\n\n"

Block scalars nested under a mapping key (key: | followed by indented content) are not affected. Scope: this is only about reading root-level block scalar content at column 0 with the usual chomping rules. It does not ask the parser to accept trailing content after the document or to change multi-document handling.

Reproduction

#include <iostream>
#include "yaml-cpp/yaml.h"

int main() {
  for (const char* in : {"--- >\nline1\nline2\nline3\n",
                         "--- |\nline1\nline2\nline3\n",
                         "--- >\nline1\n# no comment\nline3\n",
                         "--- |+\nline1\nline2\n\n"}) {
    try {
      std::cout << "[" << YAML::Load(in).as<std::string>() << "]\n";
    } catch (const YAML::Exception& e) {
      std::cout << e.what() << "\n";
    }
  }
}

Observed output for each of the four inputs (gtest run on the commit above):

C++ exception with description "yaml-cpp: error at line 5, column 1: unexpected token after end of document" thrown in the test body.

Environment:

  • Commit: c5e65360373d1d99d6b6848c991cd4fcee435009 (master)
  • Toolchain: GCC 11.5.0

Suggested fix
Treat the document root as indentation -1 when computing a block scalar's content indentation, so root content may start at column 0; such a scalar then ends at a ---/... marker, and folded content followed by EOF or a document marker ends with a line break rather than a fold:

diff --git a/src/scanscalar.cpp b/src/scanscalar.cpp
--- a/src/scanscalar.cpp
+++ b/src/scanscalar.cpp
@@ -163,8 +163,11 @@ std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) {
           scalar += "\n";
           break;
         case FOLD_BLOCK:
+          // (content that is followed by EOF or a document marker ends with a
+          // line break, not a fold)
           if (!emptyLine && !nextEmptyLine && !moreIndented &&
-              !nextMoreIndented && INPUT.column() >= params.indent) {
+              !nextMoreIndented && INPUT.column() >= params.indent && INPUT &&
+              !(INPUT.column() == 0 && Exp::DocIndicator().Matches(INPUT))) {
             scalar += " ";
           } else if (nextEmptyLine) {
             foldedNewlineCount++;
diff --git a/src/scantoken.cpp b/src/scantoken.cpp
--- a/src/scantoken.cpp
+++ b/src/scantoken.cpp
@@ -422,13 +422,15 @@ void Scanner::ScanBlockScalar() {
   if (INPUT && !Exp::Break().Matches(INPUT))
     throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_BLOCK);
 
-  // set the initial indentation
-  if (GetTopIndent() >= 0)
-    params.indent += GetTopIndent();
+  // set the initial indentation (the document root has indentation -1, so a
+  // root block scalar may have its content at column 0)
+  params.indent += GetTopIndent();
 
   params.eatLeadingWhitespace = false;
   params.trimTrailingSpaces = false;
   params.onTabInIndentation = THROW;
+  // content at column 0 ends at a document marker
+  params.onDocIndicator = BREAK;
 
   scalar = ScanScalar(INPUT, params);
 

Verified on c5e6536 (GCC 11.4.0, bundled GoogleTest 1.16.0): the test above fails without this patch and passes with it; no other test results change.

I can submit a PR for this.

Related: open PR #1493 adds Ex9_3_BareDocuments and Ex9_5_DirectivesDocuments as DISABLED_ tests that contain this construct (no fix); #1505 (emitter drops a final newline) and #819 / #1458 (trailing scalar at end of document) are different problems.

Lingua principale
C++
Stelle
6.1k
Fork
2.2k
Merge medio
22h 2m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di jbeder/yaml-cpp

Tutte le issue di jbeder/yaml-cpp

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.