From bf76321c9ea74651898d677ec6d2aabcc524ed89 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 5 Aug 2016 10:48:09 +0100 Subject: [PATCH 1/6] Move LiteralString::toString from the header --- libsolidity/ast/Types.cpp | 5 +++++ libsolidity/ast/Types.h | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 5630743bd..df4ec0aaa 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -852,6 +852,11 @@ bool StringLiteralType::operator==(const Type& _other) const return m_value == dynamic_cast(_other).m_value; } +std::string StringLiteralType::toString(bool) const +{ + return "literal_string \"" + m_value + "\""; +} + TypePointer StringLiteralType::mobileType() const { return make_shared(DataLocation::Memory, true); diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 1ee762e53..1282e5d82 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -419,7 +419,7 @@ public: virtual bool canLiveOutsideStorage() const override { return false; } virtual unsigned sizeOnStack() const override { return 0; } - virtual std::string toString(bool) const override { return "literal_string \"" + m_value + "\""; } + virtual std::string toString(bool) const override; virtual TypePointer mobileType() const override; std::string const& value() const { return m_value; } From 7945f41ccc63510a3fcb68d7c00e0c48143c4ed3 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 5 Aug 2016 10:56:59 +0100 Subject: [PATCH 2/6] Include UTF8 helpers in libdevcore --- libdevcore/UTF8.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++ libdevcore/UTF8.h | 40 +++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 libdevcore/UTF8.cpp create mode 100644 libdevcore/UTF8.h diff --git a/libdevcore/UTF8.cpp b/libdevcore/UTF8.cpp new file mode 100644 index 000000000..4bae75efa --- /dev/null +++ b/libdevcore/UTF8.cpp @@ -0,0 +1,86 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** @file UTF8.cpp + * @author Alex Beregszaszi + * @date 2016 + * + * UTF-8 related helpers + */ + +#include "UTF8.h" + + +namespace dev +{ + +namespace utf8 +{ + + +bool validate(std::string input, int &invalidPosition) +{ + const int length = input.length(); + bool valid = true; + int i = 0; + + for (; i < length; i++) + { + if ((unsigned char)input[i] < 0x80) + continue; + + int count = 0; + switch(input[i] & 0xe0) { + case 0xc0: count = 1; break; + case 0xe0: count = 2; break; + case 0xf0: count = 3; break; + default: break; + } + + if (count == 0) + { + valid = false; + break; + } + + if ((i + count) >= length) + { + valid = false; + break; + } + + for (int j = 0; j < count; j++) + { + i++; + if ((input[i] & 0xc0) != 0x80) + { + valid = false; + break; + } + } + } + + if (valid) + return true; + + invalidPosition = i; + return false; +} + + +} + +} diff --git a/libdevcore/UTF8.h b/libdevcore/UTF8.h new file mode 100644 index 000000000..39f76a116 --- /dev/null +++ b/libdevcore/UTF8.h @@ -0,0 +1,40 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** @file UTF8.h + * @author Alex Beregszaszi + * @date 2016 + * + * UTF-8 related helpers + */ + +#pragma once + +#include + +namespace dev +{ + +namespace utf8 +{ + +/// Validate an input for UTF8 encoding +/// @returns true if it is invalid and the first invalid position in invalidPosition +bool validate(std::string input, int &invalidPosition); + +} + +} From e8c2e87397c0dbb8e5be3e096385e45740a4cbf3 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 5 Aug 2016 18:27:44 +0100 Subject: [PATCH 3/6] Use utf8::validate in StringLiteral::toString --- libsolidity/ast/Types.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index df4ec0aaa..d6f6225ce 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -854,6 +855,11 @@ bool StringLiteralType::operator==(const Type& _other) const std::string StringLiteralType::toString(bool) const { + int invalidSequence; + + if (!dev::utf8::validate(m_value, invalidSequence)) + return "literal_string (contains invalid UTF-8 sequence at position " + dev::toString(invalidSequence) + ")"; + return "literal_string \"" + m_value + "\""; } From c1571634413fe7de3140b7fdbf55b1d884ef03ff Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 8 Aug 2016 19:12:11 +0100 Subject: [PATCH 4/6] Use consts in dev::utf8::validate() --- libdevcore/UTF8.cpp | 12 ++++++------ libdevcore/UTF8.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libdevcore/UTF8.cpp b/libdevcore/UTF8.cpp index 4bae75efa..0c385e815 100644 --- a/libdevcore/UTF8.cpp +++ b/libdevcore/UTF8.cpp @@ -31,19 +31,19 @@ namespace utf8 { -bool validate(std::string input, int &invalidPosition) +bool validate(std::string const& _input, int& _invalidPosition) { - const int length = input.length(); + const int length = _input.length(); bool valid = true; int i = 0; for (; i < length; i++) { - if ((unsigned char)input[i] < 0x80) + if ((unsigned char)_input[i] < 0x80) continue; int count = 0; - switch(input[i] & 0xe0) { + switch(_input[i] & 0xe0) { case 0xc0: count = 1; break; case 0xe0: count = 2; break; case 0xf0: count = 3; break; @@ -65,7 +65,7 @@ bool validate(std::string input, int &invalidPosition) for (int j = 0; j < count; j++) { i++; - if ((input[i] & 0xc0) != 0x80) + if ((_input[i] & 0xc0) != 0x80) { valid = false; break; @@ -76,7 +76,7 @@ bool validate(std::string input, int &invalidPosition) if (valid) return true; - invalidPosition = i; + _invalidPosition = i; return false; } diff --git a/libdevcore/UTF8.h b/libdevcore/UTF8.h index 39f76a116..2824d5bf6 100644 --- a/libdevcore/UTF8.h +++ b/libdevcore/UTF8.h @@ -33,7 +33,7 @@ namespace utf8 /// Validate an input for UTF8 encoding /// @returns true if it is invalid and the first invalid position in invalidPosition -bool validate(std::string input, int &invalidPosition); +bool validate(std::string const& _input, int& _invalidPosition); } From f1df3dee537e827bc4f74ed92ab8a58cd4cf2ac0 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 8 Aug 2016 19:12:52 +0100 Subject: [PATCH 5/6] Use size_t in dev::utf8::validate() --- libdevcore/UTF8.cpp | 10 +++++----- libdevcore/UTF8.h | 2 +- libsolidity/ast/Types.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libdevcore/UTF8.cpp b/libdevcore/UTF8.cpp index 0c385e815..a71693a8c 100644 --- a/libdevcore/UTF8.cpp +++ b/libdevcore/UTF8.cpp @@ -31,18 +31,18 @@ namespace utf8 { -bool validate(std::string const& _input, int& _invalidPosition) +bool validate(std::string const& _input, size_t& _invalidPosition) { - const int length = _input.length(); + const size_t length = _input.length(); bool valid = true; - int i = 0; + size_t i = 0; for (; i < length; i++) { if ((unsigned char)_input[i] < 0x80) continue; - int count = 0; + size_t count = 0; switch(_input[i] & 0xe0) { case 0xc0: count = 1; break; case 0xe0: count = 2; break; @@ -62,7 +62,7 @@ bool validate(std::string const& _input, int& _invalidPosition) break; } - for (int j = 0; j < count; j++) + for (size_t j = 0; j < count; j++) { i++; if ((_input[i] & 0xc0) != 0x80) diff --git a/libdevcore/UTF8.h b/libdevcore/UTF8.h index 2824d5bf6..d6959f3ea 100644 --- a/libdevcore/UTF8.h +++ b/libdevcore/UTF8.h @@ -33,7 +33,7 @@ namespace utf8 /// Validate an input for UTF8 encoding /// @returns true if it is invalid and the first invalid position in invalidPosition -bool validate(std::string const& _input, int& _invalidPosition); +bool validate(std::string const& _input, size_t& _invalidPosition); } diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index d6f6225ce..aae189347 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -855,7 +855,7 @@ bool StringLiteralType::operator==(const Type& _other) const std::string StringLiteralType::toString(bool) const { - int invalidSequence; + size_t invalidSequence; if (!dev::utf8::validate(m_value, invalidSequence)) return "literal_string (contains invalid UTF-8 sequence at position " + dev::toString(invalidSequence) + ")"; From 5d9347f0225d62c51fc4a8a846eaf0468c3077b9 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 8 Aug 2016 19:18:26 +0100 Subject: [PATCH 6/6] Remove dev::utf8 namespace --- libdevcore/UTF8.cpp | 5 ----- libdevcore/UTF8.h | 5 ----- libsolidity/ast/Types.cpp | 2 +- 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/libdevcore/UTF8.cpp b/libdevcore/UTF8.cpp index a71693a8c..d742fe665 100644 --- a/libdevcore/UTF8.cpp +++ b/libdevcore/UTF8.cpp @@ -27,9 +27,6 @@ namespace dev { -namespace utf8 -{ - bool validate(std::string const& _input, size_t& _invalidPosition) { @@ -81,6 +78,4 @@ bool validate(std::string const& _input, size_t& _invalidPosition) } -} - } diff --git a/libdevcore/UTF8.h b/libdevcore/UTF8.h index d6959f3ea..3e39273cd 100644 --- a/libdevcore/UTF8.h +++ b/libdevcore/UTF8.h @@ -28,13 +28,8 @@ namespace dev { -namespace utf8 -{ - /// Validate an input for UTF8 encoding /// @returns true if it is invalid and the first invalid position in invalidPosition bool validate(std::string const& _input, size_t& _invalidPosition); } - -} diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index aae189347..28f7e1b7e 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -857,7 +857,7 @@ std::string StringLiteralType::toString(bool) const { size_t invalidSequence; - if (!dev::utf8::validate(m_value, invalidSequence)) + if (!dev::validate(m_value, invalidSequence)) return "literal_string (contains invalid UTF-8 sequence at position " + dev::toString(invalidSequence) + ")"; return "literal_string \"" + m_value + "\"";