aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/YAMLParser.cpp
diff options
context:
space:
mode:
authorrkayaith <rkayaith@gmail.com>2022-10-25 15:17:14 -0400
committerRahul Kayaith <rkayaith@gmail.com>2023-02-09 21:56:57 -0500
commit32b3f13337ef0bf747705d058f4772c7fdabd736 (patch)
tree2584ea803724768c19556836bccab1b3d8bb874f /llvm/lib/Support/YAMLParser.cpp
parent35537aea1228df29c4e32049e4750029c5f0693b (diff)
downloadllvm-32b3f13337ef0bf747705d058f4772c7fdabd736.zip
llvm-32b3f13337ef0bf747705d058f4772c7fdabd736.tar.gz
llvm-32b3f13337ef0bf747705d058f4772c7fdabd736.tar.bz2
[YAML] Trim trailing whitespace from plain scalars
In some cases plain scalars are currently parsed with a trailing newline. In particular this shows up often when parsing JSON files, e.g. note the `\n` after `456` below: ``` $ cat test.yaml { "foo": 123, "bar": 456 } $ yaml-bench test.yaml -canonical %YAML 1.2 --- !!map { ? !!str "foo" : !!str "123", ? !!str "bar" : !!str "456\n", } ... ``` The trailing whitespace ends up causing the conversion of the scalar to int/bool/etc. to fail, causing the issue seen here: https://github.com/llvm/llvm-project/issues/15877 From reading the YAML spec (https://yaml.org/spec/1.2.2/#733-plain-style) it seems like plain scalars should never end with whitespace, so this change trims all trailing whitespace characters from the value (specifically `b-line-feed`, `b-carriage-return`, `s-space`, and `s-tab`). Reviewed By: scott.linder Differential Revision: https://reviews.llvm.org/D137118
Diffstat (limited to 'llvm/lib/Support/YAMLParser.cpp')
-rw-r--r--llvm/lib/Support/YAMLParser.cpp7
1 files changed, 5 insertions, 2 deletions
diff --git a/llvm/lib/Support/YAMLParser.cpp b/llvm/lib/Support/YAMLParser.cpp
index b85b1eb..6ac2c6a 100644
--- a/llvm/lib/Support/YAMLParser.cpp
+++ b/llvm/lib/Support/YAMLParser.cpp
@@ -2041,8 +2041,11 @@ StringRef ScalarNode::getValue(SmallVectorImpl<char> &Storage) const {
}
return UnquotedValue;
}
- // Plain or block.
- return Value.rtrim(' ');
+ // Plain.
+ // Trim whitespace ('b-char' and 's-white').
+ // NOTE: Alternatively we could change the scanner to not include whitespace
+ // here in the first place.
+ return Value.rtrim("\x0A\x0D\x20\x09");
}
StringRef ScalarNode::unescapeDoubleQuoted( StringRef UnquotedValue