From 88540cc96a2ed68049d443e03030252e0e454b42 Mon Sep 17 00:00:00 2001 From: beder Date: Thu, 12 Jan 2012 23:52:51 -0600 Subject: [PATCH] Fixed double -> int conversion (now throws) for old API --- include/yaml-cpp/old-api/conversion.h | 5 ++--- test/old-api/parsertests.cpp | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/include/yaml-cpp/old-api/conversion.h b/include/yaml-cpp/old-api/conversion.h index cff904c..1b557b5 100644 --- a/include/yaml-cpp/old-api/conversion.h +++ b/include/yaml-cpp/old-api/conversion.h @@ -50,9 +50,8 @@ namespace YAML inline bool Convert(const std::string& input, T& output, typename enable_if >::type * = 0) { std::stringstream stream(input); stream.unsetf(std::ios::dec); - stream >> output; - if(!!stream) - return true; + if((stream >> output) && (stream >> std::ws).eof()) + return true; if(std::numeric_limits::has_infinity) { if(IsInfinity(input)) { diff --git a/test/old-api/parsertests.cpp b/test/old-api/parsertests.cpp index 7f80f98..7145e15 100644 --- a/test/old-api/parsertests.cpp +++ b/test/old-api/parsertests.cpp @@ -903,7 +903,24 @@ namespace Test return doc["foo"].to() == "\n"; } - } + + bool DoubleAsInt() + { + std::string input = "1.5"; + std::stringstream stream(input); + YAML::Parser parser(stream); + YAML::Node doc; + parser.GetNextDocument(doc); + + try { + doc.to(); + } catch(const YAML::InvalidScalar& e) { + return true; + } + + return false; + } + } namespace { void RunScalarParserTest(void (*test)(std::string&, std::string&), const std::string& name, int& passed, int& total) { @@ -1184,6 +1201,7 @@ namespace Test RunParserTest(&Parser::NonConstKey, "non const key", passed, total); RunParserTest(&Parser::SingleChar, "single char", passed, total); RunParserTest(&Parser::QuotedNewline, "quoted newline", passed, total); + RunParserTest(&Parser::DoubleAsInt, "double as int", passed, total); RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed, total); RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed, total);