From fdfcdbcc82e06dda958b0e4306a4141891c9ea85 Mon Sep 17 00:00:00 2001 From: Subhransu Mohanty Date: Wed, 26 May 2021 11:32:40 +0900 Subject: [PATCH] remove RAPIDJSON_ASSERT() by placing the parser in error state Change-Id: I2ef64d4a50feb0ca769f19ce38b5bcabb83fd181 --- src/lottie/lottieparser.cpp | 117 ++++++++++-------------------------- 1 file changed, 31 insertions(+), 86 deletions(-) diff --git a/src/lottie/lottieparser.cpp b/src/lottie/lottieparser.cpp index 914507d..6d46cc2 100644 --- a/src/lottie/lottieparser.cpp +++ b/src/lottie/lottieparser.cpp @@ -145,6 +145,10 @@ public: return true; } + void Error() + { + st_ = kError; + } protected: explicit LookaheadParserHandler(char *str); @@ -191,6 +195,7 @@ public: int GetInt(); double GetDouble(); const char * GetString(); + std::string GetStringObject(); bool GetBool(); void GetNull(); @@ -399,7 +404,6 @@ bool LottieParserImpl::EnterObject() { if (st_ != kEnteringObject) { st_ = kError; - RAPIDJSON_ASSERT(false); return false; } @@ -411,7 +415,6 @@ bool LottieParserImpl::EnterArray() { if (st_ != kEnteringArray) { st_ = kError; - RAPIDJSON_ASSERT(false); return false; } @@ -441,7 +444,6 @@ const char *LottieParserImpl::NextObjectKey() } if (st_ != kExitingObject) { - RAPIDJSON_ASSERT(false); st_ = kError; return nullptr; } @@ -465,7 +467,6 @@ bool LottieParserImpl::NextArrayValue() } if (st_ == kError || st_ == kHasKey) { - RAPIDJSON_ASSERT(false); st_ = kError; return false; } @@ -477,7 +478,6 @@ int LottieParserImpl::GetInt() { if (st_ != kHasNumber || !v_.IsInt()) { st_ = kError; - RAPIDJSON_ASSERT(false); return 0; } @@ -490,7 +490,6 @@ double LottieParserImpl::GetDouble() { if (st_ != kHasNumber) { st_ = kError; - RAPIDJSON_ASSERT(false); return 0.; } @@ -503,7 +502,6 @@ bool LottieParserImpl::GetBool() { if (st_ != kHasBool) { st_ = kError; - RAPIDJSON_ASSERT(false); return false; } @@ -526,7 +524,6 @@ const char *LottieParserImpl::GetString() { if (st_ != kHasString) { st_ = kError; - RAPIDJSON_ASSERT(false); return nullptr; } @@ -535,6 +532,17 @@ const char *LottieParserImpl::GetString() return result; } +std::string LottieParserImpl::GetStringObject() +{ + auto str = GetString(); + + if (str) { + return std::string(str); + } + + return {}; +} + void LottieParserImpl::SkipOut(int depth) { do { @@ -543,7 +551,6 @@ void LottieParserImpl::SkipOut(int depth) } else if (st_ == kExitingArray || st_ == kExitingObject) { --depth; } else if (st_ == kError) { - RAPIDJSON_ASSERT(false); return; } @@ -609,7 +616,6 @@ void LottieParserImpl::Skip(const char * /*key*/) model::BlendMode LottieParserImpl::getBlendMode() { - RAPIDJSON_ASSERT(PeekType() == kNumberType); auto mode = model::BlendMode::Normal; switch (GetInt()) { @@ -646,7 +652,6 @@ void LottieParserImpl::resolveLayerRefs() void LottieParserImpl::parseComposition() { - RAPIDJSON_ASSERT(PeekType() == kObjectType); EnterObject(); std::shared_ptr sharedComposition = std::make_shared(); @@ -654,22 +659,16 @@ void LottieParserImpl::parseComposition() compRef = comp; while (const char *key = NextObjectKey()) { if (0 == strcmp(key, "v")) { - RAPIDJSON_ASSERT(PeekType() == kStringType); - comp->mVersion = std::string(GetString()); + comp->mVersion = GetStringObject(); } else if (0 == strcmp(key, "w")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); comp->mSize.setWidth(GetInt()); } else if (0 == strcmp(key, "h")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); comp->mSize.setHeight(GetInt()); } else if (0 == strcmp(key, "ip")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); comp->mStartFrame = GetDouble(); } else if (0 == strcmp(key, "op")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); comp->mEndFrame = GetDouble(); } else if (0 == strcmp(key, "fr")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); comp->mFrameRate = GetDouble(); } else if (0 == strcmp(key, "assets")) { parseAssets(comp); @@ -703,20 +702,16 @@ void LottieParserImpl::parseComposition() void LottieParserImpl::parseMarker() { - RAPIDJSON_ASSERT(PeekType() == kObjectType); EnterObject(); std::string comment; int timeframe{0}; int duration{0}; while (const char *key = NextObjectKey()) { if (0 == strcmp(key, "cm")) { - RAPIDJSON_ASSERT(PeekType() == kStringType); - comment = std::string(GetString()); + comment = GetStringObject(); } else if (0 == strcmp(key, "tm")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); timeframe = GetDouble(); } else if (0 == strcmp(key, "dr")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); duration = GetDouble(); } else { @@ -732,7 +727,6 @@ void LottieParserImpl::parseMarker() void LottieParserImpl::parseMarkers() { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { parseMarker(); @@ -742,7 +736,6 @@ void LottieParserImpl::parseMarkers() void LottieParserImpl::parseAssets(model::Composition *composition) { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { auto asset = parseAsset(); @@ -818,8 +811,6 @@ static std::string toString(const T &value) */ model::Asset *LottieParserImpl::parseAsset() { - RAPIDJSON_ASSERT(PeekType() == kObjectType); - auto asset = allocator().make(); std::string filename; std::string relativePath; @@ -827,30 +818,24 @@ model::Asset *LottieParserImpl::parseAsset() EnterObject(); while (const char *key = NextObjectKey()) { if (0 == strcmp(key, "w")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); asset->mWidth = GetInt(); } else if (0 == strcmp(key, "h")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); asset->mHeight = GetInt(); } else if (0 == strcmp(key, "p")) { /* image name */ asset->mAssetType = model::Asset::Type::Image; - RAPIDJSON_ASSERT(PeekType() == kStringType); - filename = std::string(GetString()); + filename = GetStringObject(); } else if (0 == strcmp(key, "u")) { /* relative image path */ - RAPIDJSON_ASSERT(PeekType() == kStringType); - relativePath = std::string(GetString()); + relativePath = GetStringObject(); } else if (0 == strcmp(key, "e")) { /* relative image path */ embededResource = GetInt(); } else if (0 == strcmp(key, "id")) { /* reference id*/ if (PeekType() == kStringType) { - asset->mRefId = std::string(GetString()); + asset->mRefId = GetStringObject(); } else { - RAPIDJSON_ASSERT(PeekType() == kNumberType); asset->mRefId = toString(GetInt()); } } else if (0 == strcmp(key, "layers")) { asset->mAssetType = model::Asset::Type::Precomp; - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); bool staticFlag = true; while (NextArrayValue()) { @@ -889,7 +874,6 @@ void LottieParserImpl::parseLayers(model::Composition *comp) comp->mRootLayer->mLayerType = model::Layer::Type::Precomp; comp->mRootLayer->setName("__"); bool staticFlag = true; - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { auto layer = parseLayer(); @@ -903,6 +887,8 @@ void LottieParserImpl::parseLayers(model::Composition *comp) model::Color LottieParserImpl::toColor(const char *str) { + if (!str) return {}; + model::Color color; auto len = strlen(str); @@ -928,7 +914,6 @@ model::Color LottieParserImpl::toColor(const char *str) model::MatteType LottieParserImpl::getMatteType() { - RAPIDJSON_ASSERT(PeekType() == kNumberType); switch (GetInt()) { case 1: return model::MatteType::Alpha; @@ -950,7 +935,6 @@ model::MatteType LottieParserImpl::getMatteType() model::Layer::Type LottieParserImpl::getLayerType() { - RAPIDJSON_ASSERT(PeekType() == kNumberType); switch (GetInt()) { case 0: return model::Layer::Type::Precomp; @@ -982,7 +966,6 @@ model::Layer::Type LottieParserImpl::getLayerType() */ model::Layer *LottieParserImpl::parseLayer() { - RAPIDJSON_ASSERT(PeekType() == kObjectType); model::Layer *layer = allocator().make(); curLayerRef = layer; bool ddd = true; @@ -991,43 +974,33 @@ model::Layer *LottieParserImpl::parseLayer() if (0 == strcmp(key, "ty")) { /* Type of layer*/ layer->mLayerType = getLayerType(); } else if (0 == strcmp(key, "nm")) { /*Layer name*/ - RAPIDJSON_ASSERT(PeekType() == kStringType); layer->setName(GetString()); } else if (0 == strcmp(key, "ind")) { /*Layer index in AE. Used for parenting and expressions.*/ - RAPIDJSON_ASSERT(PeekType() == kNumberType); layer->mId = GetInt(); } else if (0 == strcmp(key, "ddd")) { /*3d layer */ - RAPIDJSON_ASSERT(PeekType() == kNumberType); ddd = GetInt(); } else if (0 == strcmp(key, "parent")) { /*Layer Parent. Uses "ind" of parent.*/ - RAPIDJSON_ASSERT(PeekType() == kNumberType); layer->mParentId = GetInt(); } else if (0 == strcmp(key, "refId")) { /*preComp Layer reference id*/ - RAPIDJSON_ASSERT(PeekType() == kStringType); - layer->extra()->mPreCompRefId = std::string(GetString()); + layer->extra()->mPreCompRefId = GetStringObject(); layer->mHasGradient = true; mLayersToUpdate.push_back(layer); } else if (0 == strcmp(key, "sr")) { // "Layer Time Stretching" - RAPIDJSON_ASSERT(PeekType() == kNumberType); layer->mTimeStreatch = GetDouble(); } else if (0 == strcmp(key, "tm")) { // time remapping parseProperty(layer->extra()->mTimeRemap); } else if (0 == strcmp(key, "ip")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); layer->mInFrame = std::lround(GetDouble()); } else if (0 == strcmp(key, "op")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); layer->mOutFrame = std::lround(GetDouble()); } else if (0 == strcmp(key, "st")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); layer->mStartFrame = GetDouble(); } else if (0 == strcmp(key, "bm")) { layer->mBlendMode = getBlendMode(); } else if (0 == strcmp(key, "ks")) { - RAPIDJSON_ASSERT(PeekType() == kObjectType); EnterObject(); layer->mTransform = parseTransformObject(ddd); } else if (0 == strcmp(key, "shapes")) { @@ -1100,7 +1073,6 @@ model::Layer *LottieParserImpl::parseLayer() void LottieParserImpl::parseMaskProperty(model::Layer *layer) { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { layer->extra()->mMasks.push_back(parseMaskObject()); @@ -1111,7 +1083,6 @@ model::Mask *LottieParserImpl::parseMaskObject() { auto obj = allocator().make(); - RAPIDJSON_ASSERT(PeekType() == kObjectType); EnterObject(); while (const char *key = NextObjectKey()) { if (0 == strcmp(key, "inv")) { @@ -1156,7 +1127,6 @@ model::Mask *LottieParserImpl::parseMaskObject() void LottieParserImpl::parseShapesAttr(model::Layer *layer) { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { parseObject(layer); @@ -1165,7 +1135,6 @@ void LottieParserImpl::parseShapesAttr(model::Layer *layer) model::Object *LottieParserImpl::parseObjectTypeAttr() { - RAPIDJSON_ASSERT(PeekType() == kStringType); const char *type = GetString(); if (0 == strcmp(type, "gr")) { return parseGroupObject(); @@ -1211,7 +1180,6 @@ model::Object *LottieParserImpl::parseObjectTypeAttr() void LottieParserImpl::parseObject(model::Group *parent) { - RAPIDJSON_ASSERT(PeekType() == kObjectType); EnterObject(); while (const char *key = NextObjectKey()) { if (0 == strcmp(key, "ty")) { @@ -1253,10 +1221,8 @@ model::Object *LottieParserImpl::parseGroupObject() if (0 == strcmp(key, "nm")) { group->setName(GetString()); } else if (0 == strcmp(key, "it")) { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { - RAPIDJSON_ASSERT(PeekType() == kObjectType); parseObject(group); } if (group->mChildren.back()->type() == @@ -1437,7 +1403,6 @@ model::Polystar *LottieParserImpl::parsePolystarObject() model::Trim::TrimType LottieParserImpl::getTrimType() { - RAPIDJSON_ASSERT(PeekType() == kNumberType); switch (GetInt()) { case 1: return model::Trim::TrimType::Simultaneously; @@ -1446,7 +1411,7 @@ model::Trim::TrimType LottieParserImpl::getTrimType() return model::Trim::TrimType::Individually; break; default: - RAPIDJSON_ASSERT(0); + Error(); return model::Trim::TrimType::Simultaneously; break; } @@ -1656,7 +1621,6 @@ model::Fill *LottieParserImpl::parseFillObject() */ CapStyle LottieParserImpl::getLineCap() { - RAPIDJSON_ASSERT(PeekType() == kNumberType); switch (GetInt()) { case 1: return CapStyle::Flat; @@ -1672,7 +1636,6 @@ CapStyle LottieParserImpl::getLineCap() FillRule LottieParserImpl::getFillRule() { - RAPIDJSON_ASSERT(PeekType() == kNumberType); switch (GetInt()) { case 1: return FillRule::Winding; @@ -1691,7 +1654,6 @@ FillRule LottieParserImpl::getFillRule() */ JoinStyle LottieParserImpl::getLineJoin() { - RAPIDJSON_ASSERT(PeekType() == kNumberType); switch (GetInt()) { case 1: return JoinStyle::Miter; @@ -1728,7 +1690,6 @@ model::Stroke *LottieParserImpl::parseStrokeObject() } else if (0 == strcmp(key, "lj")) { obj->mJoinStyle = getLineJoin(); } else if (0 == strcmp(key, "ml")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); obj->mMiterLimit = GetDouble(); } else if (0 == strcmp(key, "d")) { parseDashProperty(obj->mDash); @@ -1750,7 +1711,6 @@ void LottieParserImpl::parseGradientProperty(model::Gradient *obj, const char * key) { if (0 == strcmp(key, "t")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); obj->mGradientType = GetInt(); } else if (0 == strcmp(key, "o")) { parseProperty(obj->mOpacity); @@ -1808,10 +1768,8 @@ model::GradientFill *LottieParserImpl::parseGFillObject() void LottieParserImpl::parseDashProperty(model::Dash &dash) { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { - RAPIDJSON_ASSERT(PeekType() == kObjectType); EnterObject(); while (const char *key = NextObjectKey()) { if (0 == strcmp(key, "v")) { @@ -1841,7 +1799,6 @@ model::GradientStroke *LottieParserImpl::parseGStrokeObject() } else if (0 == strcmp(key, "lj")) { obj->mJoinStyle = getLineJoin(); } else if (0 == strcmp(key, "ml")) { - RAPIDJSON_ASSERT(PeekType() == kNumberType); obj->mMiterLimit = GetDouble(); } else if (0 == strcmp(key, "d")) { parseDashProperty(obj->mDash); @@ -1857,10 +1814,8 @@ model::GradientStroke *LottieParserImpl::parseGStrokeObject() void LottieParserImpl::getValue(std::vector &v) { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); VPointF pt; getValue(pt); @@ -1897,7 +1852,7 @@ void LottieParserImpl::getValue(float &val) } else if (PeekType() == kNumberType) { val = GetDouble(); } else { - RAPIDJSON_ASSERT(0); + Error(); } } @@ -1940,7 +1895,7 @@ void LottieParserImpl::getValue(int &val) } else if (PeekType() == kNumberType) { val = GetInt(); } else { - RAPIDJSON_ASSERT(0); + Error(); } } @@ -1955,7 +1910,6 @@ void LottieParserImpl::parsePathInfo() bool arrayWrapper = (PeekType() == kArrayType); if (arrayWrapper) EnterArray(); - RAPIDJSON_ASSERT(PeekType() == kObjectType); EnterObject(); while (const char *key = NextObjectKey()) { if (0 == strcmp(key, "i")) { @@ -1967,7 +1921,7 @@ void LottieParserImpl::parsePathInfo() } else if (0 == strcmp(key, "c")) { mPathInfo.mClosed = GetBool(); } else { - RAPIDJSON_ASSERT(0); + Error(); Skip(nullptr); } } @@ -1987,7 +1941,6 @@ void LottieParserImpl::getValue(model::PathData &obj) VPointF LottieParserImpl::parseInperpolatorPoint() { VPointF cp; - RAPIDJSON_ASSERT(PeekType() == kObjectType); EnterObject(); while (const char *key = NextObjectKey()) { if (0 == strcmp(key, "x")) { @@ -2076,17 +2029,15 @@ void LottieParserImpl::parseKeyFrame(model::KeyFrames &obj) continue; } else if (0 == strcmp(key, "n")) { if (PeekType() == kStringType) { - parsed.interpolatorKey = GetString(); + parsed.interpolatorKey = GetStringObject(); } else { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { - RAPIDJSON_ASSERT(PeekType() == kStringType); if (parsed.interpolatorKey.empty()) { - parsed.interpolatorKey = GetString(); + parsed.interpolatorKey = GetStringObject(); } else { // skip rest of the string - GetString(); + Skip(nullptr); } } } @@ -2142,12 +2093,10 @@ void LottieParserImpl::parseShapeProperty(model::Property &obj) if (PeekType() == kArrayType) { EnterArray(); while (NextArrayValue()) { - RAPIDJSON_ASSERT(PeekType() == kObjectType); parseKeyFrame(obj.animation()); } } else { if (!obj.isStatic()) { - RAPIDJSON_ASSERT(false); st_ = kError; return; } @@ -2168,14 +2117,12 @@ void LottieParserImpl::parsePropertyHelper(model::Property &obj) { if (PeekType() == kNumberType) { if (!obj.isStatic()) { - RAPIDJSON_ASSERT(false); st_ = kError; return; } /*single value property with no animation*/ getValue(obj.value()); } else { - RAPIDJSON_ASSERT(PeekType() == kArrayType); EnterArray(); while (NextArrayValue()) { /* property with keyframe info*/ @@ -2188,9 +2135,7 @@ void LottieParserImpl::parsePropertyHelper(model::Property &obj) * or array of object without entering the array * thats why this hack is there */ - RAPIDJSON_ASSERT(PeekType() == kNumberType); if (!obj.isStatic()) { - RAPIDJSON_ASSERT(false); st_ = kError; return; } -- 2.34.1