aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYoichi Hirai <i@yoichihirai.com>2016-11-08 20:37:59 +0800
committerYoichi Hirai <i@yoichihirai.com>2016-11-12 00:48:03 +0800
commit98dcd883e4ee1b3d62a1b9e7d4e5d4e038ea0434 (patch)
tree4bf7151af69100652b42cf989d1451e6e41d128e
parent8856adce8f1829cecf7353c64c8cf07058da6f4f (diff)
downloaddexon-solidity-98dcd883e4ee1b3d62a1b9e7d4e5d4e038ea0434.tar
dexon-solidity-98dcd883e4ee1b3d62a1b9e7d4e5d4e038ea0434.tar.gz
dexon-solidity-98dcd883e4ee1b3d62a1b9e7d4e5d4e038ea0434.tar.bz2
dexon-solidity-98dcd883e4ee1b3d62a1b9e7d4e5d4e038ea0434.tar.lz
dexon-solidity-98dcd883e4ee1b3d62a1b9e7d4e5d4e038ea0434.tar.xz
dexon-solidity-98dcd883e4ee1b3d62a1b9e7d4e5d4e038ea0434.tar.zst
dexon-solidity-98dcd883e4ee1b3d62a1b9e7d4e5d4e038ea0434.zip
codegen: check the value range after converting something to an enum element
-rw-r--r--Changelog.md1
-rw-r--r--libsolidity/codegen/CompilerUtils.cpp8
2 files changed, 9 insertions, 0 deletions
diff --git a/Changelog.md b/Changelog.md
index d76303ab..b4456a86 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -3,6 +3,7 @@
Features:
* Do-while loops: support for a C-style do{<block>}while(<expr>); control structure
* Type checker: now more eagerly searches for a common type of an inline array with mixed types
+ * Code generator: generates a runtime error when an out-of-range value is converted into an enum type.
### 0.4.4 (2016-10-31)
diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp
index e064c1a6..5e045996 100644
--- a/libsolidity/codegen/CompilerUtils.cpp
+++ b/libsolidity/codegen/CompilerUtils.cpp
@@ -656,6 +656,14 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp
solAssert(_typeOnStack == _targetType, "Invalid type conversion requested.");
break;
}
+
+ // Check the conversion result fits in a range.
+ if (targetTypeCategory == Type::Category::Enum)
+ {
+ EnumType const& enumType = dynamic_cast<decltype(enumType)>(_targetType);
+ m_context << u256(enumType.numberOfMembers()) << Instruction::DUP2 << Instruction::LT << Instruction::ISZERO;
+ m_context.appendConditionalJumpTo(m_context.errorTag());
+ }
}
void CompilerUtils::pushZeroValue(Type const& _type)