From 7fc67ce2deac09fa6af1c47c5908ff602d34223b Mon Sep 17 00:00:00 2001 From: jief Date: Fri, 31 Aug 2018 12:43:12 +0200 Subject: [PATCH 1/2] Warning removal. I used assertions in case of downcasting. Changed some var type (like size_t instead of int). Changed DecompressZLib and DecompressBZ2 parameters because the underlying lib cannot handle decompressing size_t bytes. --- ApfsLib/AesXts.cpp | 6 +++++- ApfsLib/ApfsContainer.cpp | 4 ++-- ApfsLib/ApfsContainer.h | 6 +++--- ApfsLib/ApfsDir.cpp | 3 +-- ApfsLib/BTree.cpp | 8 +++----- ApfsLib/BlockDumper.cpp | 2 +- ApfsLib/Crypto.cpp | 8 ++++++-- ApfsLib/Decmpfs.cpp | 15 ++++++++++++--- ApfsLib/DeviceDMG.cpp | 10 ++++++++-- ApfsLib/KeyMgmt.cpp | 11 ++++++++--- ApfsLib/Util.cpp | 4 ++-- ApfsLib/Util.h | 4 ++-- 12 files changed, 53 insertions(+), 28 deletions(-) diff --git a/ApfsLib/AesXts.cpp b/ApfsLib/AesXts.cpp index b7e5dca..bbf8643 100644 --- a/ApfsLib/AesXts.cpp +++ b/ApfsLib/AesXts.cpp @@ -82,6 +82,10 @@ void AesXts::Xor(uint8_t *out, const uint8_t *op1, const uint8_t *op2) val64[1] = op1_64[1] ^ op2_64[1]; } +#ifdef __clang__ +#pragma clang diagnostic ignored "-Wconditional-uninitialized" +#endif + void AesXts::MultiplyTweak(uint8_t* tweak) { uint8_t cin; @@ -95,6 +99,6 @@ void AesXts::MultiplyTweak(uint8_t* tweak) tweak[k] = (tweak[k] << 1) | cin; cin = cout; } - if (cout) + if (cout) // 2018-08 : wrongly reported uninitialized by clang tweak[0] ^= 0x87; } diff --git a/ApfsLib/ApfsContainer.cpp b/ApfsLib/ApfsContainer.cpp index 39ebe10..00e14a7 100644 --- a/ApfsLib/ApfsContainer.cpp +++ b/ApfsLib/ApfsContainer.cpp @@ -30,10 +30,10 @@ int g_debug = 0; bool g_lax = false; -ApfsContainer::ApfsContainer(Device &disk, uint64_t start, uint64_t len) : +ApfsContainer::ApfsContainer(Device &disk, uint64_t start/*, uint64_t len*/) : m_disk(disk), m_part_start(start), - m_part_len(len), +// m_part_len(len), m_nodemap_vol(*this), m_nidmap_bt(*this), m_oldmgr_bt(*this), diff --git a/ApfsLib/ApfsContainer.h b/ApfsLib/ApfsContainer.h index 95743c9..648acfd 100644 --- a/ApfsLib/ApfsContainer.h +++ b/ApfsLib/ApfsContainer.h @@ -34,7 +34,7 @@ class BlockDumper; class ApfsContainer { public: - ApfsContainer(Device &disk, uint64_t start, uint64_t len); + ApfsContainer(Device &disk, uint64_t start/*, uint64_t len*/); ~ApfsContainer(); bool Init(); @@ -55,13 +55,13 @@ class ApfsContainer private: Device &m_disk; const uint64_t m_part_start; - const uint64_t m_part_len; +// const uint64_t m_part_len; // Currently not used. std::string m_passphrase; APFS_NX_Superblock m_sb; - APFS_Block_8_5_Spaceman m_spaceman_hdr; +// APFS_Block_8_5_Spaceman m_spaceman_hdr; // Block_8_11 ? ApfsNodeMapperBTree m_nodemap_vol; diff --git a/ApfsLib/ApfsDir.cpp b/ApfsLib/ApfsDir.cpp index 3bda8dc..956782a 100644 --- a/ApfsLib/ApfsDir.cpp +++ b/ApfsLib/ApfsDir.cpp @@ -295,7 +295,6 @@ bool ApfsDir::ReadFile(void* data, uint64_t inode, uint64_t offs, size_t size) uint8_t *bdata = reinterpret_cast(data); size_t cur_size; - unsigned idx; while (size > 0) { @@ -323,7 +322,7 @@ bool ApfsDir::ReadFile(void* data, uint64_t inode, uint64_t offs, size_t size) return false; // TODO: 12 is dependent on block size ... - idx = (offs - ext_key->logical_addr) >> 12; + auto idx = (offs - ext_key->logical_addr) >> 12; // ext_val->size has a mysterious upper byte set. At least sometimes. // Let us clear it. uint64_t extent_size = ext_val->flags_length & 0x00FFFFFFFFFFFFFFULL; diff --git a/ApfsLib/BTree.cpp b/ApfsLib/BTree.cpp index 608da65..067c9fe 100644 --- a/ApfsLib/BTree.cpp +++ b/ApfsLib/BTree.cpp @@ -320,8 +320,6 @@ void BTree::dump(BlockDumper& out) void BTree::DumpTreeInternal(BlockDumper& out, const std::shared_ptr &node) { - size_t k; - size_t cnt; BTreeEntry e; std::shared_ptr child; uint64_t nodeid_child; @@ -334,9 +332,9 @@ void BTree::DumpTreeInternal(BlockDumper& out, const std::shared_ptr if (node->level() > 0) { - cnt = node->entries_cnt(); + auto cnt = node->entries_cnt(); - for (k = 0; k < cnt; k++) + for (decltype(cnt) k = 0; k < cnt; k++) { node->GetEntry(e, k); @@ -490,7 +488,7 @@ int BTree::FindBin(const std::shared_ptr& node, const void* key, size int res; BTreeEntry e; - int rc; + int rc = -1; // to silence warning about ch being used uninitialized. if (cnt <= 0) return -1; diff --git a/ApfsLib/BlockDumper.cpp b/ApfsLib/BlockDumper.cpp index 7a67b72..8c2382f 100644 --- a/ApfsLib/BlockDumper.cpp +++ b/ApfsLib/BlockDumper.cpp @@ -432,7 +432,7 @@ void BlockDumper::DumpBTEntry_APFS_Root(const byte_t *key_data, size_t key_lengt { const APFS_Xattr_Val *attr = reinterpret_cast(value_data); - assert(attr->size + 4 == value_length); + assert( decltype(value_length)(attr->size + 4) == value_length); // attr->size + 4 is promoted to int, giving a warning (comparison between signed and unsigned integer expressions). Hence the cast. m_os << " => " << attr->type << " " << attr->size; diff --git a/ApfsLib/Crypto.cpp b/ApfsLib/Crypto.cpp index 3004998..2a2e28c 100644 --- a/ApfsLib/Crypto.cpp +++ b/ApfsLib/Crypto.cpp @@ -9,6 +9,8 @@ #include #include +#include + #include "Endian.h" union Rfc3394_Unit { @@ -26,7 +28,8 @@ void Rfc3394_KeyWrap(uint8_t *crypto, const uint8_t *plain, size_t size, const u uint64_t r[6]; int i; int j; - int n = size / sizeof(uint64_t); + assert(size / sizeof(uint64_t) <= INT_MAX); + int n = int(size / sizeof(uint64_t)); uint64_t t; const uint64_t *p = reinterpret_cast(plain); uint64_t *c = reinterpret_cast(crypto); @@ -65,7 +68,8 @@ bool Rfc3394_KeyUnwrap(uint8_t *plain, const uint8_t *crypto, size_t size, const uint64_t r[6]; int i; int j; - int n = size / sizeof(uint64_t); + assert(size / sizeof(uint64_t) <= INT_MAX); + int n = int(size / sizeof(uint64_t)); uint64_t t; const uint64_t *c = reinterpret_cast(crypto); uint64_t *p = reinterpret_cast(plain); diff --git a/ApfsLib/Decmpfs.cpp b/ApfsLib/Decmpfs.cpp index c4377d2..b2c7979 100644 --- a/ApfsLib/Decmpfs.cpp +++ b/ApfsLib/Decmpfs.cpp @@ -22,6 +22,8 @@ #include #include +#include + #include "Decmpfs.h" #include "Endian.h" @@ -84,7 +86,7 @@ bool DecompressFile(ApfsDir &dir, uint64_t ino, std::vector &decompress const CompressionHeader *hdr = reinterpret_cast(compressed.data()); const uint8_t *cdata = compressed.data() + sizeof(CompressionHeader); size_t csize = compressed.size() - sizeof(CompressionHeader); - size_t decoded_bytes = 0; + size_t decoded_bytes; #if 1 // Disable to get compressed data if (g_debug & Dbg_Cmpfs) @@ -162,7 +164,8 @@ bool DecompressFile(ApfsDir &dir, uint64_t ino, std::vector &decompress if (src[0] == 0x78) { - decoded_bytes = DecompressZLib(dst, 0x10000, src, src_len); + assert(src_len <= UINT_MAX); + decoded_bytes = DecompressZLib(dst, 0x10000, src, (unsigned int)src_len); } else if ((src[0] & 0x0F) == 0x0F) { @@ -236,7 +239,9 @@ bool DecompressFile(ApfsDir &dir, uint64_t ino, std::vector &decompress { if (cdata[0] == 0x78) { - decoded_bytes = DecompressZLib(decompressed.data(), decompressed.size(), cdata, csize); + assert(decompressed.size() <= UINT_MAX); + assert(csize <= UINT_MAX); + decoded_bytes = DecompressZLib(decompressed.data(), (unsigned int)decompressed.size(), cdata, (unsigned int)csize); } else if (cdata[0] == 0xFF) // cdata[0] & 0x0F == 0x0F ? { @@ -261,6 +266,10 @@ bool DecompressFile(ApfsDir &dir, uint64_t ino, std::vector &decompress decoded_bytes = DecompressLZVN(decompressed.data(), decompressed.size(), cdata, csize); } } + else + { + decoded_bytes = 0; + } if (decoded_bytes != hdr->size) { diff --git a/ApfsLib/DeviceDMG.cpp b/ApfsLib/DeviceDMG.cpp index e58c614..af34a86 100644 --- a/ApfsLib/DeviceDMG.cpp +++ b/ApfsLib/DeviceDMG.cpp @@ -21,6 +21,8 @@ along with apfs-fuse. If not, see . #include #include #include +#include +#include #include "DeviceDMG.h" #include "Util.h" @@ -435,10 +437,14 @@ bool DeviceDMG::Read(void * data, uint64_t offs, uint64_t len) DecompressADC(sect.cache, sect.disk_length, compr_buf, sect.dmg_length); break; case 0x80000005: - DecompressZLib(sect.cache, sect.disk_length, compr_buf, sect.dmg_length); + assert(sect.disk_length <= UINT_MAX); + assert(sect.dmg_length <= UINT_MAX); + DecompressZLib(sect.cache, (unsigned int)sect.disk_length, compr_buf, (unsigned int)sect.dmg_length); break; case 0x80000006: - DecompressBZ2(sect.cache, sect.disk_length, compr_buf, sect.dmg_length); + assert(sect.disk_length <= UINT_MAX); + assert(sect.dmg_length <= UINT_MAX); + DecompressBZ2(sect.cache, (unsigned int)sect.disk_length, compr_buf, (unsigned int)sect.dmg_length); break; case 0x80000007: DecompressLZFSE(sect.cache, sect.disk_length, compr_buf, sect.dmg_length); diff --git a/ApfsLib/KeyMgmt.cpp b/ApfsLib/KeyMgmt.cpp index a83c4d2..4c93ea6 100644 --- a/ApfsLib/KeyMgmt.cpp +++ b/ApfsLib/KeyMgmt.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "ApfsContainer.h" #include "AesXts.h" @@ -641,8 +643,8 @@ bool KeyManager::GetVolumeKey(uint8_t* vek, const apfs_uuid_t& volume_uuid, cons uint64_t iv; bool rc = false; - int cnt = recs_bag.GetKeyCnt(); - int k; + auto cnt = recs_bag.GetKeyCnt(); + decltype(cnt) k; // Check all KEKs for any valid KEK. for (k = 0; k < cnt; k++) @@ -659,7 +661,8 @@ bool KeyManager::GetVolumeKey(uint8_t* vek, const apfs_uuid_t& volume_uuid, cons if (!DecodeKEKBlob(kek_blob, kek_data)) continue; - PBKDF2_HMAC_SHA256(reinterpret_cast(password), strlen(password), kek_blob.salt, sizeof(kek_blob.salt), kek_blob.iterations, dk, sizeof(dk)); + assert(kek_blob.iterations <= INT_MAX); + PBKDF2_HMAC_SHA256(reinterpret_cast(password), strlen(password), kek_blob.salt, sizeof(kek_blob.salt), int(kek_blob.iterations), dk, sizeof(dk)); switch (kek_blob.unk_82.unk_00) { @@ -673,6 +676,7 @@ bool KeyManager::GetVolumeKey(uint8_t* vek, const apfs_uuid_t& volume_uuid, cons default: std::cerr << "Unknown KEK key flags 82/00 = " << std::hex << kek_blob.unk_82.unk_00 << ". Please file a bug report." << std::endl; rc = false; + iv = 0; // to silence warning about iv being used uninitialized. break; } @@ -737,6 +741,7 @@ bool KeyManager::GetVolumeKey(uint8_t* vek, const apfs_uuid_t& volume_uuid, cons // Unknown method. std::cerr << "Unknown VEK key flags 82/00 = " << std::hex << vek_blob.unk_82.unk_00 << ". Please file a bug report." << std::endl; rc = false; + iv = 0; // to silence the uninitialized warning } if (g_debug & Dbg_Crypto) diff --git a/ApfsLib/Util.cpp b/ApfsLib/Util.cpp index e929425..78f1f2f 100644 --- a/ApfsLib/Util.cpp +++ b/ApfsLib/Util.cpp @@ -395,7 +395,7 @@ bool GetPassword(std::string &pw) #endif } -size_t DecompressZLib(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size) +size_t DecompressZLib(uint8_t *dst, unsigned int dst_size, const uint8_t *src, unsigned int src_size) // zstream can't take more than unsigned int for src and dst size { // size_t nwr = 0; int ret; @@ -503,7 +503,7 @@ size_t DecompressLZVN(uint8_t * dst, size_t dst_size, const uint8_t * src, size_ return static_cast(state.dst - dst); } -size_t DecompressBZ2(uint8_t * dst, size_t dst_size, const uint8_t * src, size_t src_size) +size_t DecompressBZ2(uint8_t * dst, unsigned int dst_size, const uint8_t * src, unsigned int src_size) // zstream can't take more than unsigned int for src and dst size { bz_stream strm; diff --git a/ApfsLib/Util.h b/ApfsLib/Util.h index 0e405b7..74c3774 100644 --- a/ApfsLib/Util.h +++ b/ApfsLib/Util.h @@ -43,10 +43,10 @@ int StrCmpUtf8NormalizedFolded(const char *s1, const char *s2, bool case_fold); bool Utf8toUtf32(std::vector &str32, const char * str); -size_t DecompressZLib(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); +size_t DecompressZLib(uint8_t *dst, unsigned int dst_size, const uint8_t *src, unsigned int src_size); // zstream can't take more than unsigned int for src and dst size size_t DecompressADC(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); size_t DecompressLZVN(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); -size_t DecompressBZ2(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); +size_t DecompressBZ2(uint8_t *dst, unsigned int dst_size, const uint8_t *src, unsigned int src_size); // bz_stream can't take more than unsigned int for src and dst size size_t DecompressLZFSE(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); bool GetPassword(std::string &pw); From 19f2e58ba9f62fcc3838733e8914c2fd259b3cdd Mon Sep 17 00:00:00 2001 From: jief Date: Fri, 31 Aug 2018 13:28:17 +0200 Subject: [PATCH 2/2] Use auto instead of size_t to save conversion. --- ApfsLib/Decmpfs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ApfsLib/Decmpfs.cpp b/ApfsLib/Decmpfs.cpp index b2c7979..b23a6dd 100644 --- a/ApfsLib/Decmpfs.cpp +++ b/ApfsLib/Decmpfs.cpp @@ -149,7 +149,7 @@ bool DecompressFile(ApfsDir &dir, uint64_t ino, std::vector &decompress { size_t src_offset = cmpf_rsrc->entry[k].off; const uint8_t *src = cmpf_rsrc_base + src_offset; - size_t src_len = cmpf_rsrc->entry[k].size; + auto src_len = cmpf_rsrc->entry[k].size; //cmpf_rsrc->entry[k].size is uint32_t uint8_t *dst = decompressed.data() + 0x10000 * k; size_t expected_len = hdr->size - (0x10000 * k); if (expected_len > 0x10000)