From 48483eab86f52a1eac64b56e767e88b86f665988 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 22 May 2018 17:58:59 +0200 Subject: Fix crash on windows when dependency is not found --- src/Package.cpp | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/Package.cpp b/src/Package.cpp index d59c6a9..5969ac5 100644 --- a/src/Package.cpp +++ b/src/Package.cpp @@ -27,20 +27,20 @@ namespace sibs Result getPackageMetadata(Value::ConstObject jsonObj) { - const auto &description = jsonObj["description"]; - if(!description.IsString()) return Result::Err("Expected description to be a string"); - const auto &version = jsonObj["version"]; - if(!version.IsString()) return Result::Err("Expected version to be a string"); - const auto &platforms = jsonObj["platforms"]; - if(!platforms.IsArray() || platforms.GetArray().Empty()) return Result::Err("Expected platforms to be an array of strings"); - const auto &urls = jsonObj["urls"]; - if(!urls.IsArray() || urls.GetArray().Empty()) return Result::Err("Expected urls to be an array of string"); + const auto &description = jsonObj.FindMember("description"); + if(description == jsonObj.MemberEnd() || !description->value.IsString()) return Result::Err("Expected description to be a string"); + const auto &version = jsonObj.FindMember("version"); + if(version == jsonObj.MemberEnd() || !version->value.IsString()) return Result::Err("Expected version to be a string"); + const auto &platforms = jsonObj.FindMember("platforms"); + if(platforms == jsonObj.MemberEnd() || !platforms->value.IsArray() || platforms->value.GetArray().Empty()) return Result::Err("Expected platforms to be an array of strings"); + const auto &urls = jsonObj.FindMember("urls"); + if(urls == jsonObj.MemberEnd() || !urls->value.IsArray() || urls->value.GetArray().Empty()) return Result::Err("Expected urls to be an array of string"); PackageMetadata packageMetadata; - packageMetadata.description.assign(description.GetString(), description.GetStringLength()); - packageMetadata.version.assign(version.GetString(), version.GetStringLength()); + packageMetadata.description.assign(description->value.GetString(), description->value.GetStringLength()); + packageMetadata.version.assign(version->value.GetString(), version->value.GetStringLength()); - const auto &platformsArray = platforms.GetArray(); + const auto &platformsArray = platforms->value.GetArray(); packageMetadata.platforms.reserve(platformsArray.Size()); for(int i = 0; i < platformsArray.Size(); ++i) { @@ -50,7 +50,7 @@ namespace sibs packageMetadata.platforms.push_back(platformElement.GetString()); } - const auto &urlsArray = urls.GetArray(); + const auto &urlsArray = urls->value.GetArray(); packageMetadata.urls.reserve(urlsArray.Size()); for(int i = 0; i < urlsArray.Size(); ++i) { @@ -129,17 +129,25 @@ namespace sibs const Document &packageDoc = *packageList.unwrap(); - const Value &packageMetaDataJson = packageDoc[packageName]; - if(packageMetaDataJson.IsObject()) + auto packageMetaDataJsonIt = packageDoc.FindMember(packageName); + if (packageMetaDataJsonIt == packageDoc.MemberEnd()) + { + string errMsg = "No package with the name \""; + errMsg += packageName; + errMsg += "\" was found"; + return Result::Err(errMsg); + } + + if(packageMetaDataJsonIt->value.IsObject()) { - Result packageMetadataResult = getPackageMetadata(packageMetaDataJson.GetObject()); + Result packageMetadataResult = getPackageMetadata(packageMetaDataJsonIt->value.GetObject()); if(!packageMetadataResult) return Result::Err(packageMetadataResult); return ::sibs::getPackageUrl(packageMetadataResult.unwrap(), packageName, packageVersion, platform); } - else if(packageMetaDataJson.IsArray()) + else if(packageMetaDataJsonIt->value.IsArray()) { int i = 0; - for(const auto &packageData : packageMetaDataJson.GetArray()) + for(const auto &packageData : packageMetaDataJsonIt->value.GetArray()) { if(!packageData.IsObject()) { -- cgit v1.2.3