From b67a0a8f2bbef990794c941a82eab9014424a5a5 Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 1 May 2022 14:46:47 +0200 Subject: [PATCH] Fix uninitialized coverity warnings --- apps/esmtool/arguments.hpp | 6 +++--- apps/esmtool/esmtool.cpp | 20 ++++---------------- components/esm3/esmwriter.hpp | 1 + components/esm3/loadnpc.cpp | 3 +++ components/esm4/loadhdpt.cpp | 16 ++++++++++++---- components/esm4/loadland.cpp | 1 + components/esm4/loadqust.cpp | 8 ++++---- components/esm4/loadtes4.cpp | 2 +- 8 files changed, 29 insertions(+), 28 deletions(-) diff --git a/apps/esmtool/arguments.hpp b/apps/esmtool/arguments.hpp index 96b4bb8f04..edd8c9b06c 100644 --- a/apps/esmtool/arguments.hpp +++ b/apps/esmtool/arguments.hpp @@ -11,9 +11,9 @@ namespace EsmTool struct Arguments { std::optional mRawFormat; - bool quiet_given; - bool loadcells_given; - bool plain_given; + bool quiet_given = false; + bool loadcells_given = false; + bool plain_given = false; std::string mode; std::string encoding; diff --git a/apps/esmtool/esmtool.cpp b/apps/esmtool/esmtool.cpp index 3eb78f3e41..2e64168046 100644 --- a/apps/esmtool/esmtool.cpp +++ b/apps/esmtool/esmtool.cpp @@ -36,11 +36,7 @@ namespace bpo = boost::program_options; struct ESMData { - std::string author; - std::string description; - unsigned int version; - std::vector masters; - + ESM::Header mHeader; std::deque> mRecords; // Value: (Reference, Deleted flag) std::map > > mCellRefs; @@ -331,11 +327,7 @@ int loadTes3(const Arguments& info, std::unique_ptr&& stream, ESM esm.open(std::move(stream), info.filename); if (data != nullptr) - { - data->author = esm.getAuthor(); - data->description = esm.getDesc(); - data->masters = esm.getGameFiles(); - } + data->mHeader = esm.getHeader(); if (!quiet) { @@ -503,14 +495,10 @@ int clone(const Arguments& info) ESM::ESMWriter esm; ToUTF8::Utf8Encoder encoder (ToUTF8::calculateEncoding(info.encoding)); esm.setEncoder(&encoder); - esm.setAuthor(data.author); - esm.setDescription(data.description); - esm.setVersion(data.version); + esm.setHeader(data.mHeader); + esm.setVersion(ESM::VER_13); esm.setRecordCount (recordCount); - for (const ESM::Header::MasterData &master : data.masters) - esm.addMaster(master.name, master.size); - std::fstream save(info.outname.c_str(), std::fstream::out | std::fstream::binary); esm.save(save); diff --git a/components/esm3/esmwriter.hpp b/components/esm3/esmwriter.hpp index 264dc1c877..473e948f98 100644 --- a/components/esm3/esmwriter.hpp +++ b/components/esm3/esmwriter.hpp @@ -38,6 +38,7 @@ class ESMWriter void setEncoder(ToUTF8::Utf8Encoder *encoding); void setAuthor(const std::string& author); void setDescription(const std::string& desc); + void setHeader(const Header& value) { mHeader = value; } // Set the record count for writing it in the file header void setRecordCount (int count); diff --git a/components/esm3/loadnpc.cpp b/components/esm3/loadnpc.cpp index 6c0b8d0a68..dc91c5ea0c 100644 --- a/components/esm3/loadnpc.cpp +++ b/components/esm3/loadnpc.cpp @@ -158,6 +158,9 @@ namespace ESM npdt12.mDisposition = mNpdt.mDisposition; npdt12.mReputation = mNpdt.mReputation; npdt12.mRank = mNpdt.mRank; + npdt12.mUnknown1 = 0; + npdt12.mUnknown2 = 0; + npdt12.mUnknown3 = 0; npdt12.mGold = mNpdt.mGold; esm.writeHNT("NPDT", npdt12, 12); } diff --git a/components/esm4/loadhdpt.cpp b/components/esm4/loadhdpt.cpp index 9e5d4c9ffd..ee1d3dd526 100644 --- a/components/esm4/loadhdpt.cpp +++ b/components/esm4/loadhdpt.cpp @@ -27,6 +27,7 @@ #include "loadhdpt.hpp" #include +#include //#include // FIXME: testing only #include "reader.hpp" @@ -38,7 +39,7 @@ void ESM4::HeadPart::load(ESM4::Reader& reader) reader.adjustFormId(mFormId); mFlags = reader.hdr().record.flags; - std::uint32_t type; + std::optional type; while (reader.getSubRecordHeader()) { @@ -52,7 +53,9 @@ void ESM4::HeadPart::load(ESM4::Reader& reader) case ESM4::SUB_HNAM: reader.getFormId(mAdditionalPart); break; case ESM4::SUB_NAM0: // TES5 { - reader.get(type); + std::uint32_t value; + reader.get(value); + type = value; break; } @@ -61,8 +64,13 @@ void ESM4::HeadPart::load(ESM4::Reader& reader) std::string file; reader.getZString(file); - // FIXME: check type >= 0 && type < 3 - mTriFile[type] = std::move(file); + if (!type.has_value()) + throw std::runtime_error("Failed to read ESM4 HDPT record: subrecord NAM0 does not precede subrecord NAM1: file type is unknown"); + + if (*type >= mTriFile.size()) + throw std::runtime_error("Failed to read ESM4 HDPT record: invalid file type: " + std::to_string(*type)); + + mTriFile[*type] = std::move(file); break; } diff --git a/components/esm4/loadland.cpp b/components/esm4/loadland.cpp index 651a285a72..4cbbfd69eb 100644 --- a/components/esm4/loadland.cpp +++ b/components/esm4/loadland.cpp @@ -58,6 +58,7 @@ void ESM4::Land::load(ESM4::Reader& reader) mFormId = reader.hdr().record.id; reader.adjustFormId(mFormId); mFlags = reader.hdr().record.flags; + mDataTypes = 0; TxtLayer layer; std::int8_t currentAddQuad = -1; // for VTXT following ATXT diff --git a/components/esm4/loadqust.cpp b/components/esm4/loadqust.cpp index 39eb592514..14aa51e17a 100644 --- a/components/esm4/loadqust.cpp +++ b/components/esm4/loadqust.cpp @@ -65,18 +65,20 @@ void ESM4::Quest::load(ESM4::Reader& reader) case ESM4::SUB_SCRI: reader.get(mQuestScript); break; case ESM4::SUB_CTDA: // FIXME: how to detect if 1st/2nd param is a formid? { - TargetCondition cond; - if (subHdr.dataSize == 24) // TES4 { + TargetCondition cond; reader.get(&cond, 24); cond.reference = 0; // unused in TES4 but keep it clean + mTargetConditions.push_back(cond); } else if (subHdr.dataSize == 28) { + TargetCondition cond; reader.get(cond); // FO3/FONV if (cond.reference) reader.adjustFormId(cond.reference); + mTargetConditions.push_back(cond); } else { @@ -85,8 +87,6 @@ void ESM4::Quest::load(ESM4::Reader& reader) } // FIXME: support TES5 - mTargetConditions.push_back(cond); - break; } case ESM4::SUB_SCHR: reader.get(mScript.scriptHeader); break; diff --git a/components/esm4/loadtes4.cpp b/components/esm4/loadtes4.cpp index 42c4acde4e..3e81492825 100644 --- a/components/esm4/loadtes4.cpp +++ b/components/esm4/loadtes4.cpp @@ -67,7 +67,7 @@ void ESM4::Header::load(ESM4::Reader& reader) throw std::runtime_error("TES4 MAST data read error"); // NOTE: some mods do not have DATA following MAST so can't read DATA here - + m.size = 0; mMaster.push_back (m); break; }