From e018d62a671bfd6901d07556592e900ae489e9de Mon Sep 17 00:00:00 2001 From: Bhargava Shastry Date: Thu, 1 Aug 2019 16:15:54 +0200 Subject: [PATCH] Dynamically sized array dimensions can be zero sized --- test/tools/ossfuzz/protoToAbiV2.cpp | 13 +++++-------- test/tools/ossfuzz/protoToAbiV2.h | 22 ++++++++++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/test/tools/ossfuzz/protoToAbiV2.cpp b/test/tools/ossfuzz/protoToAbiV2.cpp index fa5785287..dd766ddb4 100644 --- a/test/tools/ossfuzz/protoToAbiV2.cpp +++ b/test/tools/ossfuzz/protoToAbiV2.cpp @@ -328,13 +328,10 @@ void ProtoConverter::visit(StructType const&) std::string ProtoConverter::arrayDimInfoAsString(ArrayDimensionInfo const& _x) { - unsigned arrLength = getArrayLengthFromFuzz(_x.length()); - if (_x.is_static()) - return Whiskers(R"([])") - ("length", std::to_string(arrLength)) + return Whiskers(R"([])") + ("isStatic", _x.is_static()) + ("length", std::to_string(getStaticArrayLengthFromFuzz(_x.length()))) .render(); - else - return R"([])"; } void ProtoConverter::arrayDimensionsAsStringVector( @@ -398,7 +395,7 @@ ProtoConverter::DataType ProtoConverter::getDataTypeByBaseType(ArrayType const& // Adds a resize operation for a given dimension of type `_type` and expression referenced // by `_var`. `_isStatic` is true for statically sized dimensions, false otherwise. // `_arrayLen` is equal to length of statically sized array dimension. For dynamically -// sized dimension, we use `getArrayLengthFromFuzz()` and a monotonically increasing +// sized dimension, we use `getDynArrayLengthFromFuzz()` and a monotonically increasing // counter to obtain actual length. Function returns dimension length. unsigned ProtoConverter::resizeDimension( bool _isStatic, @@ -413,7 +410,7 @@ unsigned ProtoConverter::resizeDimension( length = _arrayLen; else { - length = getArrayLengthFromFuzz(_arrayLen, getNextCounter()); + length = getDynArrayLengthFromFuzz(_arrayLen, getNextCounter()); // If local var, new T(l); // Else, l; diff --git a/test/tools/ossfuzz/protoToAbiV2.h b/test/tools/ossfuzz/protoToAbiV2.h index edef94c3e..d676cbb32 100644 --- a/test/tools/ossfuzz/protoToAbiV2.h +++ b/test/tools/ossfuzz/protoToAbiV2.h @@ -334,14 +334,28 @@ private: return toHex(maskUnsignedInt(_counter, _numMaskNibbles), HexPrefix::Add); } - static unsigned getArrayLengthFromFuzz(unsigned _fuzz, unsigned _counter = 0) + /// Dynamically sized arrays can have a length of at least zero + /// and at most s_maxArrayLength. + static unsigned getDynArrayLengthFromFuzz(unsigned _fuzz, unsigned _counter) { - return ((_fuzz + _counter) % s_maxArrayLength) + 1; + // Increment modulo value by one in order to meet upper bound + return (_fuzz + _counter) % (s_maxArrayLength + 1); + } + + /// Statically sized arrays must have a length of at least one + /// and at most s_maxArrayLength. + static unsigned getStaticArrayLengthFromFuzz(unsigned _fuzz) + { + return _fuzz % s_maxArrayLength + 1; } static std::pair arrayDimInfoAsPair(ArrayDimensionInfo const& _x) { - return std::make_pair(_x.is_static(), getArrayLengthFromFuzz(_x.length())); + return ( + _x.is_static() ? + std::make_pair(true, getStaticArrayLengthFromFuzz(_x.length())) : + std::make_pair(false, getDynArrayLengthFromFuzz(_x.length(), 0)) + ); } /// Contains the test program @@ -360,7 +374,7 @@ private: unsigned m_varCounter; /// Monotonically increasing return value for error reporting unsigned m_returnValue; - static unsigned constexpr s_maxArrayLength = 2; + static unsigned constexpr s_maxArrayLength = 4; static unsigned constexpr s_maxArrayDimensions = 10; /// Prefixes for declared and parameterized variable names static auto constexpr s_varNamePrefix = "x_";