From d15cab21bc7218f3812bdbf5714be77205b4bd2a Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 21 Apr 2017 13:15:39 +1200 Subject: [PATCH] Address Daira's further comments --- src/test/torcontrol_tests.cpp | 10 ++++++++++ src/torcontrol.cpp | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/test/torcontrol_tests.cpp b/src/test/torcontrol_tests.cpp index 608991250..2a4457721 100644 --- a/src/test/torcontrol_tests.cpp +++ b/src/test/torcontrol_tests.cpp @@ -163,6 +163,16 @@ BOOST_AUTO_TEST_CASE(util_ParseTorReplyMapping) {"TwoOctal", "TwoEnd\11"}, }); + // Special handling for null case + // (needed because string comparison reads the null as end-of-string) + BOOST_TEST_MESSAGE(std::string("CheckParseTorReplyMapping(Null=\"\\0\")")); + auto ret = ParseTorReplyMapping("Null=\"\\0\""); + BOOST_CHECK_EQUAL(ret.size(), 1); + auto r_it = ret.begin(); + BOOST_CHECK_EQUAL(r_it->first, "Null"); + BOOST_CHECK_EQUAL(r_it->second.size(), 1); + BOOST_CHECK_EQUAL(r_it->second[0], '\0'); + // A more complex valid grammar. PROTOCOLINFO accepts a VersionLine that // takes a key=value pair followed by an OptArguments, making this valid. // Because an OptArguments contains no semantic data, there is no point in diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp index 4ca6f8571..446d4402a 100644 --- a/src/torcontrol.cpp +++ b/src/torcontrol.cpp @@ -327,7 +327,7 @@ static std::map ParseTorReplyMapping(const std::string // Octal escape sequences have a limit of three octal digits, // but terminate at the first character that is not a valid // octal digit if encountered sooner. - for (j = 1; '0' <= value[i+j] && value[i+j] <= '7' && j < 3; ++j) {} + for (j = 1; j < 3 && (i+j) < value.size() && '0' <= value[i+j] && value[i+j] <= '7'; ++j) {} // Tor restricts first digit to 0-3 for three-digit octals. if (j < 3 || ('0' <= value[i] && value[i] <= '3')) { escaped_value.push_back(strtol(value.substr(i, j).c_str(), NULL, 8));