Merge branch 'coverity.ba2' into 'master'

Address Coverity issues in BSA code

See merge request OpenMW/openmw!5042
This commit is contained in:
Alexei Kotov 2025-12-13 02:42:55 +03:00
commit 6801ebec0d
5 changed files with 12 additions and 23 deletions

View File

@ -157,7 +157,7 @@ namespace Bsa
return std::nullopt; // folder not found
uint32_t fileHash = generateHash(fileName);
uint32_t extHash = generateExtensionHash(path.filename());
uint32_t extHash = generateExtensionHash(path.extension().value());
auto iter = it->second.find({ fileHash, extHash });
if (iter == it->second.end())
return std::nullopt; // file not found

View File

@ -1,7 +1,5 @@
#include "ba2file.hpp"
#include <components/misc/pathhelpers.hpp>
namespace Bsa
{
constexpr const uint32_t crc32table[256] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
@ -37,29 +35,22 @@ namespace Bsa
uint32_t generateHash(std::string_view name)
{
uint32_t result = 0;
for (auto c : name)
for (unsigned char c : name)
{
if (uint8_t(c) > 127)
if (c > 127)
continue;
if (c == '/')
c = '\\';
result = (result >> 8) ^ crc32table[(result ^ (unsigned)(c)) & 0xFF];
result = (result >> 8) ^ crc32table[(result ^ c) & 0xFF];
}
return result;
}
uint32_t generateExtensionHash(VFS::Path::NormalizedView file)
uint32_t generateExtensionHash(std::string_view extension)
{
std::string_view extension;
if (const std::size_t pos = Misc::findExtension(file.value()); pos != std::string_view::npos)
{
// ext including .
extension = file.value();
extension.remove_prefix(pos);
}
uint32_t result = 0;
for (size_t i = 0; i < 4 && i < extension.size() - 1; i++)
result |= static_cast<uint8_t>(extension[i + 1]) << (8 * i);
for (size_t i = 0; i < 4 && i < extension.size(); i++)
result |= static_cast<uint8_t>(extension[i]) << (8 * i);
return result;
}

View File

@ -2,14 +2,12 @@
#define OPENMW_COMPONENTS_BSA_BA2FILE_HPP
#include <cstdint>
#include <string>
#include <components/vfs/pathutil.hpp>
#include <string_view>
namespace Bsa
{
uint32_t generateHash(std::string_view name);
uint32_t generateExtensionHash(VFS::Path::NormalizedView file);
uint32_t generateExtensionHash(std::string_view extension);
enum class BA2Version : std::uint32_t
{

View File

@ -148,7 +148,7 @@ namespace Bsa
return FileRecord(); // folder not found, return default which has offset of sInvalidOffset
uint32_t fileHash = generateHash(fileName);
uint32_t extHash = generateExtensionHash(path.filename());
uint32_t extHash = generateExtensionHash(path.extension().value());
auto iter = it->second.find({ fileHash, extHash });
if (iter == it->second.end())
return FileRecord(); // file not found, return default which has offset of sInvalidOffset

View File

@ -331,7 +331,7 @@ namespace Bsa
{
if (str.empty())
return 0;
const auto at = [&](std::size_t i) -> char {
const auto at = [&](std::size_t i) -> unsigned char {
const char c = str[i];
if (c == '/')
return '\\';
@ -361,7 +361,7 @@ namespace Bsa
else if (extension == ".wav")
result |= 0x80000000;
uint32_t hash = 0;
for (const auto& c : extension)
for (unsigned char c : extension)
hash = hash * 0x1003f + c;
result += static_cast<uint64_t>(hash) << 32;
return result;