codegen: check the value range after converting something to an enum element

This commit is contained in:
Yoichi Hirai 2016-11-08 13:37:59 +01:00
parent 8856adce8f
commit 98dcd883e4
No known key found for this signature in database
GPG Key ID: E7B75D080FCF7992
2 changed files with 9 additions and 0 deletions

View File

@ -3,6 +3,7 @@
Features: Features:
* Do-while loops: support for a C-style do{<block>}while(<expr>); control structure * 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 * 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) ### 0.4.4 (2016-10-31)

View File

@ -656,6 +656,14 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp
solAssert(_typeOnStack == _targetType, "Invalid type conversion requested."); solAssert(_typeOnStack == _targetType, "Invalid type conversion requested.");
break; 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) void CompilerUtils::pushZeroValue(Type const& _type)