diff --git a/CMakeLists.txt b/CMakeLists.txt index 170f5e6..640d85b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ endif() # Add the executable add_executable(bmap-writer bmap-writer.cpp) +target_compile_options(bmap-writer PUBLIC -Wformat-security -Wconversion -Wsign-conversion -pedantic -Werror) # Link the libraries target_link_libraries(bmap-writer ${LIBXML2_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBLZMA_LIBRARIES}) diff --git a/bmap-writer.cpp b/bmap-writer.cpp index 3e9e545..051feeb 100644 --- a/bmap-writer.cpp +++ b/bmap-writer.cpp @@ -53,7 +53,7 @@ struct range_t { struct bmap_t { std::vector ranges; - int blockSize; + size_t blockSize; }; bmap_t parseBMap(const std::string &filename) { @@ -71,7 +71,7 @@ bmap_t parseBMap(const std::string &filename) { if (node->type == XML_ELEMENT_NODE) { if (strcmp((const char *)node->name, "BlockSize") == 0) { xmlChar *blockSizeStr = xmlNodeGetContent(node); - bmapData.blockSize = std::stoi((const char *)blockSizeStr); + bmapData.blockSize = static_cast(std::stoul((const char *)blockSizeStr)); xmlFree(blockSizeStr); std::cout << "Parsed BlockSize: " << bmapData.blockSize << std::endl; } else if (strcmp((const char *)node->name, "BlockMap") == 0) { @@ -214,8 +214,8 @@ void BmapWriteImage(const std::string &imageFile, const bmap_t &bmap, const std: } for (const auto &range : bmap.ranges) { - int startBlock, endBlock; - if (sscanf(range.range.c_str(), "%d-%d", &startBlock, &endBlock) == 1) { + size_t startBlock, endBlock; + if (sscanf(range.range.c_str(), "%zu-%zu", &startBlock, &endBlock) == 1) { endBlock = startBlock; // Handle single block range } std::cout << "Processing Range: startBlock=" << startBlock << ", endBlock=" << endBlock << std::endl; @@ -225,18 +225,19 @@ void BmapWriteImage(const std::string &imageFile, const bmap_t &bmap, const std: size_t bytesRead = 0; if (compressionType == "gzip") { - gzseek(gzImg, startBlock * bmap.blockSize, SEEK_SET); - bytesRead = gzread(gzImg, buffer.data(), bufferSize); - if (bytesRead <= 0) { + gzseek(gzImg, static_cast(startBlock * bmap.blockSize), SEEK_SET); + int readBytes = gzread(gzImg, buffer.data(), static_cast(bufferSize)); + if (readBytes < 0) { perror("Failed to read from gzip image file"); close(dev_fd); gzclose(gzImg); return; } + bytesRead = static_cast(readBytes); } else if (compressionType == "xz") { - imgFile.seekg(startBlock * bmap.blockSize, std::ios::beg); - imgFile.read(buffer.data(), bufferSize); - bytesRead = imgFile.gcount(); + imgFile.seekg(static_cast(startBlock * bmap.blockSize), std::ios::beg); + imgFile.read(buffer.data(), static_cast(bufferSize)); + bytesRead = static_cast(imgFile.gcount()); if (bytesRead == 0 && imgFile.fail()) { perror("Failed to read from xz image file"); close(dev_fd); @@ -256,9 +257,9 @@ void BmapWriteImage(const std::string &imageFile, const bmap_t &bmap, const std: return; } } else if (compressionType == "none") { - imgFile.seekg(startBlock * bmap.blockSize, std::ios::beg); - imgFile.read(buffer.data(), bufferSize); - bytesRead = imgFile.gcount(); + imgFile.seekg(static_cast(startBlock * bmap.blockSize), std::ios::beg); + imgFile.read(buffer.data(), static_cast(bufferSize)); + bytesRead = static_cast(imgFile.gcount()); if (bytesRead == 0 && imgFile.fail()) { perror("Failed to read from image file"); close(dev_fd); @@ -285,7 +286,7 @@ void BmapWriteImage(const std::string &imageFile, const bmap_t &bmap, const std: exit(EXIT_FAILURE); } - if (pwrite(dev_fd, buffer.data(), bytesRead, startBlock * bmap.blockSize) < 0) { + if (pwrite(dev_fd, buffer.data(), bytesRead, static_cast(startBlock * bmap.blockSize)) < 0) { perror("Write to device failed"); close(dev_fd); if (compressionType == "gzip") {