Skip to content

Commit

Permalink
CodeQL fixes
Browse files Browse the repository at this point in the history
Fixes various errors automaticallly detected by CodeQL.

* Too few arguments to formatting function
* Wrong type of arguments to formatting function
  * `size_t` uses format specifier `z`
  * `uint32_t` uses format specifier `PRIx32`

Also fix various build warnings.
* Fix: comparison of integer expressions of different signedness [-Wsign-compare]
* Fix: operation on 'i' may be undefined [-Wsequence-point]
* Fix: 'expected_redundancy' may be used uninitialized [-Wmaybe-uninitialized]
  • Loading branch information
henrygab authored Dec 31, 2024
1 parent df21059 commit 4c5c015
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
6 changes: 3 additions & 3 deletions bintool/bintool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <fstream>
#include <algorithm>
#include <random>

#include <cinttypes>
#include <tuple>

#include "boot/picobin.h"
Expand Down Expand Up @@ -646,7 +646,7 @@ std::vector<uint8_t> get_lm_hash_data(elf_file *elf, block *new_block, bool clea
const auto data = elf->content(*seg);
// std::cout << "virt = " << std::hex << seg->virtual_address() << " + " << std::hex << seg->virtual_size() << ", phys = " << std::hex << seg->physical_address() << " + " << std::hex << seg->physical_size() << std::endl;
if (data.size() != seg->physical_size()) {
fail(ERROR_INCOMPATIBLE, "Elf segment physical size (%x) does not match data size in file (%x)", seg->physical_size(), data.size());
fail(ERROR_INCOMPATIBLE, "Elf segment physical size (%" PRIx32 ") does not match data size in file (%zx)", seg->physical_size(), data.size());
}
if (seg->physical_size() && seg->physical_address() < new_block->physical_addr) {
std::copy(data.begin(), data.end(), std::back_inserter(to_hash));
Expand Down Expand Up @@ -899,7 +899,7 @@ int encrypt(elf_file *elf, block *new_block, const private_t aes_key, const publ
std::vector<uint8_t> data(enc_data.begin() + i, enc_data.begin() + i + seg->physical_size());
// std::cout << "virt = " << std::hex << seg->virtual_address() << " + " << std::hex << seg->virtual_size() << ", phys = " << std::hex << seg->physical_address() << " + " << std::hex << seg->physical_size() << std::endl;
if (data.size() != seg->physical_size()) {
fail(ERROR_INCOMPATIBLE, "Elf segment physical size (%x) does not match data size in file (%x)", seg->physical_size(), data.size());
fail(ERROR_INCOMPATIBLE, "Elf segment physical size (%" PRIx32 ") does not match data size in file (%zx)", seg->physical_size(), data.size());
}
if (seg->physical_size() && seg->physical_address() < new_block->physical_addr) {
DEBUG_LOG("ENCRYPTED %08x + %08x\n", (int)seg->physical_address(), (int)seg->physical_size());
Expand Down
5 changes: 3 additions & 2 deletions bintool/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ struct partition_table_item : public single_byte_size_item {
for (unsigned int i=2; i < size; i++) {
data.push_back(*it++);
}
int i=0;
size_t i=0;
while (i < data.size()) {
partition new_p;
uint32_t permissions_locations = data[i++];
Expand All @@ -255,7 +255,8 @@ struct partition_table_item : public single_byte_size_item {
new_p.flags = permissions_flags & (~PICOBIN_PARTITION_PERMISSIONS_BITS);

if (new_p.flags & PICOBIN_PARTITION_FLAGS_HAS_ID_BITS) {
new_p.id = (uint64_t)data[i++] | ((uint64_t)data[i++] << 32);
new_p.id = (uint64_t)data[i] | ((uint64_t)data[i+1] << 32);
i += 2;
}

uint8_t num_extra_families = (new_p.flags & PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_BITS) >> PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_LSB;
Expand Down
2 changes: 1 addition & 1 deletion elf/elf_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ int rp_determine_binary_type(const elf32_header &eh, const std::vector<elf32_ph_

void elf_file::read_bytes(unsigned offset, unsigned length, void *dest) {
if (offset + length > elf_bytes.size()) {
fail(ERROR_FORMAT, "ELF File Read from 0x%x with size 0x%x exceeds the file size 0x%x", offset, length, elf_bytes.size());
fail(ERROR_FORMAT, "ELF File Read from 0x%x with size 0x%x exceeds the file size 0x%zx", offset, length, elf_bytes.size());
}
memcpy(dest, &elf_bytes[offset], length);
}
Expand Down
3 changes: 2 additions & 1 deletion elf2uf2/elf2uf2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <algorithm>
#include <cstring>
#include <memory>
#include <cinttypes>

#include "elf2uf2.h"
#include "errors.h"
Expand Down Expand Up @@ -40,7 +41,7 @@ int check_address_range(const address_ranges& valid_ranges, uint32_t addr, uint3
for(const auto& range : valid_ranges) {
if (range.from <= addr && range.to >= addr + size) {
if (range.type == address_range::type::NO_CONTENTS && !uninitialized) {
fail(ERROR_INCOMPATIBLE, "ELF contains memory contents for uninitialized memory at %p", addr);
fail(ERROR_INCOMPATIBLE, "ELF contains memory contents for uninitialized memory at 0x%08" PRIx32, addr);
}
ar = range;
if (verbose) {
Expand Down
20 changes: 15 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3050,7 +3050,7 @@ void info_guts(memory_access &raw_access, void *con) {
unpartitioned << ", uf2 { " << cli::join(family_ids, ", ") << " }";
info_pair("un-partitioned space", unpartitioned.str());

for (int i=0; i < partition_table->partitions.size(); i++) {
for (size_t i=0; i < partition_table->partitions.size(); i++) {
std::stringstream pstring;
std::stringstream pname;
auto partition = partition_table->partitions[i];
Expand Down Expand Up @@ -4363,8 +4363,18 @@ bool erase_command::execute(device_map &devices) {
if (settings.load.partition >= partitions->size()) {
fail(ERROR_NOT_POSSIBLE, "There are only %d partitions on the device", partitions->size());
}
start = std::get<0>((*partitions)[settings.load.partition]);
end = std::get<1>((*partitions)[settings.load.partition]);
size_t tmp;
tmp = std::get<0>((*partitions)[settings.load.partition]);
if (tmp > UINT32_MAX) {
fail(ERROR_NOT_POSSIBLE, "Partition start address is too large");
}
start = tmp;
tmp = std::get<1>((*partitions)[settings.load.partition]);
if (tmp > UINT32_MAX) {
fail(ERROR_NOT_POSSIBLE, "Partition end address is too large");
}
end = tmp;

printf("Erasing partition %d:\n", settings.load.partition);
printf(" %08x->%08x\n", start, end);
start += FLASH_START;
Expand Down Expand Up @@ -5415,7 +5425,7 @@ bool get_mask(const std::string& sel, uint32_t &mask, int max_bit) {
bool ok = get_int(sel.substr(0, dash), from) &&
get_int(sel.substr(dash+1), to);
if (!ok || to < from || from < 0 || to >= max_bit) {
fail(ERROR_ARGS, "Invalid bit-range in selector: %s; expect 'm-n' where m >= 0, n >= m and n <= %d", max_bit-1);
fail(ERROR_ARGS, "Invalid bit-range in selector: %s; expect 'm-n' where m >= 0, n >= m and n <= %d", sel.c_str(), max_bit-1);
}
mask = (2u << to) - (1u << from);
return true;
Expand Down Expand Up @@ -5991,7 +6001,7 @@ bool partition_create_command::execute(device_map &devices) {
cur_pos = start + size;
#if SUPPORT_A2
if (start <= (settings.uf2.abs_block_loc - FLASH_START)/0x1000 && start + size > (settings.uf2.abs_block_loc - FLASH_START)/0x1000) {
fail(ERROR_INCOMPATIBLE, "The address %lx cannot be in a partition for the RP2350-E10 fix to work", settings.uf2.abs_block_loc);
fail(ERROR_INCOMPATIBLE, "The address %" PRIx32 " cannot be in a partition for the RP2350-E10 fix to work", settings.uf2.abs_block_loc);
}
#endif
new_p.first_sector = start;
Expand Down
2 changes: 1 addition & 1 deletion otp_header_parser/otp_header_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ int main(int argc, char **argv) {
std::string reg_name;
std::string field_name;
std::string comment;
int expected_redundancy;
int expected_redundancy = 1;
otp_reg reg;
otp_field field;
while (std::getline(infile, line)) {
Expand Down

0 comments on commit 4c5c015

Please sign in to comment.