From 64fec88436c4349f276e5dbbfa31ea0593556302 Mon Sep 17 00:00:00 2001 From: joevt Date: Wed, 21 Dec 2022 03:20:39 -0800 Subject: [PATCH] Fix compiler warnings: cast loses precision. Use explicit cast when converting large integer types to smaller integer types when it is known that the most significant bytes are not required. For pcidevice, check the ROM file size before casting to int. We'll allow expansion ROM sizes up to 4MB but usually they are 64K, sometimes 128K, rarely 256K. for machinefactory, change the type to size_t so that it can correctly get the size of files that are larger than 4GB; it already checks the file size is 4MB before we need to cast to uint32_t. For floppyimg, check the image size before casting to int. For raw images, only allow files up to 2MB. For DiskCopy42 images, it already checks the file size, so do the cast after that. --- core/mathutils.h | 4 ++-- cpu/ppc/poweropcodes.cpp | 2 +- cpu/ppc/ppcexec.cpp | 24 ++++++++++++------------ cpu/ppc/ppcmmu.cpp | 2 +- cpu/ppc/ppcopcodes.cpp | 6 +++--- debugger/debugger.cpp | 8 ++++---- devices/common/ofnvram.cpp | 2 +- devices/common/pci/pcidevice.cpp | 7 +++++-- devices/floppy/floppyimg.cpp | 12 +++++++++--- devices/floppy/superdrive.cpp | 2 +- devices/serial/chario.cpp | 6 +++--- devices/sound/soundserver.cpp | 4 ++-- machines/machinefactory.cpp | 5 +++-- machines/machineproperties.cpp | 2 +- 14 files changed, 48 insertions(+), 38 deletions(-) diff --git a/core/mathutils.h b/core/mathutils.h index fa552be..2807d54 100644 --- a/core/mathutils.h +++ b/core/mathutils.h @@ -34,13 +34,13 @@ inline void _u32xu64(uint32_t a, uint64_t b, uint64_t &hi, uint32_t &lo) { uint64_t p0 = (b & 0xffffffff) * a; uint64_t p1 = (b >> 32) * a; - lo = p0; + lo = (uint32_t)p0; hi = (p0 >> 32) + p1; } inline void _u64xu64(uint64_t a, uint64_t b, uint64_t &hi, uint64_t &lo) { - uint32_t p0h; uint64_t p0l; _u32xu64(b, a, p0h, p0l); + uint32_t p0h; uint64_t p0l; _u32xu64((uint32_t)b, a, p0h, p0l); uint64_t p1h; uint32_t p1l; _u32xu64(b >> 32, a, p1h, p1l); lo = p0l + ((uint64_t)p1l << 32); hi = p0h + p1h + (lo < p0l); diff --git a/cpu/ppc/poweropcodes.cpp b/cpu/ppc/poweropcodes.cpp index c3d47ba..1dd5b29 100644 --- a/cpu/ppc/poweropcodes.cpp +++ b/cpu/ppc/poweropcodes.cpp @@ -45,7 +45,7 @@ inline void power_setsoov(uint32_t a, uint32_t b, uint32_t d) { /** mask generator for rotate and shift instructions (§ 4.2.1.4 PowerpC PEM) */ static inline uint32_t power_rot_mask(unsigned rot_mb, unsigned rot_me) { uint32_t m1 = 0xFFFFFFFFUL >> rot_mb; - uint32_t m2 = 0xFFFFFFFFUL << (31 - rot_me); + uint32_t m2 = (uint32_t)(0xFFFFFFFFUL << (31 - rot_me)); return ((rot_mb <= rot_me) ? m2 & m1 : m1 | m2); } diff --git a/cpu/ppc/ppcexec.cpp b/cpu/ppc/ppcexec.cpp index 9236450..08ebb45 100644 --- a/cpu/ppc/ppcexec.cpp +++ b/cpu/ppc/ppcexec.cpp @@ -849,38 +849,38 @@ uint64_t reg_op(string& reg_name, uint64_t val, bool is_write) { try { if (reg_name_u == "PC") { if (is_write) - ppc_state.pc = val; + ppc_state.pc = (uint32_t)val; return ppc_state.pc; } if (reg_name_u == "MSR") { if (is_write) - ppc_state.msr = val; + ppc_state.msr = (uint32_t)val; return ppc_state.msr; } if (reg_name_u == "CR") { if (is_write) - ppc_state.cr = val; + ppc_state.cr = (uint32_t)val; return ppc_state.cr; } if (reg_name_u == "FPSCR") { if (is_write) - ppc_state.fpscr = val; + ppc_state.fpscr = (uint32_t)val; return ppc_state.fpscr; } if (reg_name_u.substr(0, 1) == "R") { reg_num_str = reg_name_u.substr(1); - reg_num = stoul(reg_num_str, NULL, 0); + reg_num = (unsigned)stoul(reg_num_str, NULL, 0); if (reg_num < 32) { if (is_write) - ppc_state.gpr[reg_num] = val; + ppc_state.gpr[reg_num] = (uint32_t)val; return ppc_state.gpr[reg_num]; } } if (reg_name_u.substr(0, 1) == "FR") { reg_num_str = reg_name_u.substr(2); - reg_num = stoul(reg_num_str, NULL, 0); + reg_num = (unsigned)stoul(reg_num_str, NULL, 0); if (reg_num < 32) { if (is_write) ppc_state.fpr[reg_num].int64_r = val; @@ -890,20 +890,20 @@ uint64_t reg_op(string& reg_name, uint64_t val, bool is_write) { if (reg_name_u.substr(0, 3) == "SPR") { reg_num_str = reg_name_u.substr(3); - reg_num = stoul(reg_num_str, NULL, 0); + reg_num = (unsigned)stoul(reg_num_str, NULL, 0); if (reg_num < 1024) { if (is_write) - ppc_state.spr[reg_num] = val; + ppc_state.spr[reg_num] = (uint32_t)val; return ppc_state.spr[reg_num]; } } if (reg_name_u.substr(0, 2) == "SR") { reg_num_str = reg_name_u.substr(2); - reg_num = stoul(reg_num_str, NULL, 0); + reg_num = (unsigned)stoul(reg_num_str, NULL, 0); if (reg_num < 16) { if (is_write) - ppc_state.sr[reg_num] = val; + ppc_state.sr[reg_num] = (uint32_t)val; return ppc_state.sr[reg_num]; } } @@ -911,7 +911,7 @@ uint64_t reg_op(string& reg_name, uint64_t val, bool is_write) { spr = SPRName2Num.find(reg_name_u); if (spr != SPRName2Num.end()) { if (is_write) - ppc_state.spr[spr->second] = val; + ppc_state.spr[spr->second] = (uint32_t)val; return ppc_state.spr[spr->second]; } } catch (...) { diff --git a/cpu/ppc/ppcmmu.cpp b/cpu/ppc/ppcmmu.cpp index af87cdb..006acc4 100644 --- a/cpu/ppc/ppcmmu.cpp +++ b/cpu/ppc/ppcmmu.cpp @@ -1007,7 +1007,7 @@ inline T mmu_read_vmem(uint32_t guest_va) // perform full address translation and refill the secondary TLB tlb2_entry = dtlb2_refill(guest_va, 0); if (tlb2_entry->flags & PAGE_NOPHYS) { - return UnmappedVal; + return (T)UnmappedVal; } } #ifdef TLB_PROFILING diff --git a/cpu/ppc/ppcopcodes.cpp b/cpu/ppc/ppcopcodes.cpp index e899a75..895bc23 100644 --- a/cpu/ppc/ppcopcodes.cpp +++ b/cpu/ppc/ppcopcodes.cpp @@ -729,7 +729,7 @@ void dppc_interpreter::ppc_srawi() { /** mask generator for rotate and shift instructions (§ 4.2.1.4 PowerpC PEM) */ static inline uint32_t rot_mask(unsigned rot_mb, unsigned rot_me) { uint32_t m1 = 0xFFFFFFFFUL >> rot_mb; - uint32_t m2 = 0xFFFFFFFFUL << (31 - rot_me); + uint32_t m2 = (uint32_t)(0xFFFFFFFFUL << (31 - rot_me)); return ((rot_mb <= rot_me) ? m2 & m1 : m1 | m2); } @@ -861,11 +861,11 @@ static inline void calc_rtcl_value() uint64_t new_ts = get_virt_time_ns(); uint64_t rtc_l = new_ts - rtc_timestamp + rtc_lo; if (rtc_l >= ONE_BILLION_NS) { // check RTCL overflow - rtc_hi += rtc_l / ONE_BILLION_NS; + rtc_hi += (uint32_t)(rtc_l / ONE_BILLION_NS); rtc_lo = rtc_l % ONE_BILLION_NS; } else { - rtc_lo = rtc_l; + rtc_lo = (uint32_t)rtc_l; } rtc_timestamp = new_ts; } diff --git a/debugger/debugger.cpp b/debugger/debugger.cpp index acdb9f8..9e0ef9a 100644 --- a/debugger/debugger.cpp +++ b/debugger/debugger.cpp @@ -52,7 +52,7 @@ using namespace std; static uint32_t str2addr(string& addr_str) { try { - return stoul(addr_str, NULL, 0); + return (uint32_t)stoul(addr_str, NULL, 0); } catch (invalid_argument& exc) { throw invalid_argument(string("Cannot convert ") + addr_str); } @@ -60,7 +60,7 @@ static uint32_t str2addr(string& addr_str) { static uint32_t str2num(string& num_str) { try { - return stol(num_str, NULL, 0); + return (uint32_t)stol(num_str, NULL, 0); } catch (invalid_argument& exc) { throw invalid_argument(string("Cannot convert ") + num_str); } @@ -512,7 +512,7 @@ void enter_debugger() { } } else if (cmd == "next" || cmd == "ni") { addr_str = "PC"; - addr = get_reg(addr_str) + 4; + addr = (uint32_t)get_reg(addr_str) + 4; ppc_exec_until(addr); } else if (cmd == "until") { ss >> addr_str; @@ -588,7 +588,7 @@ void enter_debugger() { #endif } else { addr_str = "PC"; - addr = get_reg(addr_str); + addr = (uint32_t)get_reg(addr_str); disasm(1, addr); } } catch (invalid_argument& exc) { diff --git a/devices/common/ofnvram.cpp b/devices/common/ofnvram.cpp index 9d9aacb..8b65ce8 100644 --- a/devices/common/ofnvram.cpp +++ b/devices/common/ofnvram.cpp @@ -39,7 +39,7 @@ using namespace std; static uint32_t str2env(string& num_str) { try { - return stoul(num_str, NULL, 0); + return (uint32_t)stoul(num_str, NULL, 0); } catch (invalid_argument& exc) { try { string num_str2 = string("0x") + num_str; diff --git a/devices/common/pci/pcidevice.cpp b/devices/common/pci/pcidevice.cpp index 6ad4e25..39b3e23 100644 --- a/devices/common/pci/pcidevice.cpp +++ b/devices/common/pci/pcidevice.cpp @@ -189,7 +189,10 @@ int PCIDevice::attach_exp_rom_image(const std::string img_path) // determine image size img_file.seekg(0, std::ios::end); - uint32_t exp_rom_image_size = img_file.tellg(); + size_t exp_rom_image_size = img_file.tellg(); + if (exp_rom_image_size > 4*1024*1024) { + throw std::runtime_error("expansion ROM file too large"); + } // verify PCI struct offset uint16_t pci_struct_offset = 0; @@ -223,7 +226,7 @@ int PCIDevice::attach_exp_rom_image(const std::string img_path) } else { LOG_F(WARNING, "%s: loaded expansion rom (%d bytes adjusted to %d bytes).", - this->pci_name.c_str(), exp_rom_image_size, this->exp_rom_size); + this->pci_name.c_str(), (int)exp_rom_image_size, this->exp_rom_size); } this->exp_bar_cfg = ~(this->exp_rom_size - 1); diff --git a/devices/floppy/floppyimg.cpp b/devices/floppy/floppyimg.cpp index 619ccd0..69c0343 100644 --- a/devices/floppy/floppyimg.cpp +++ b/devices/floppy/floppyimg.cpp @@ -90,7 +90,12 @@ int RawFloppyImg::calc_phys_params() // determine image size img_file.seekg(0, img_file.end); - this->img_size = img_file.tellg(); + size_t img_size = img_file.tellg(); + if (img_size > 2*1024*1024) { + LOG_F(ERROR, "RawFloppyImg: image size is too large to determine disk format from image size!"); + return -1; + } + this->img_size = (int)img_size; img_file.seekg(0, img_file.beg); img_file.close(); @@ -212,7 +217,7 @@ int DiskCopy42Img::calc_phys_params() { // determine image size img_file.seekg(0, img_file.end); - this->img_size = img_file.tellg(); + size_t img_size = img_file.tellg(); img_file.seekg(0, img_file.beg); // get data size from image @@ -221,11 +226,12 @@ int DiskCopy42Img::calc_phys_params() { img_file.read((char *)&buf, 4); this->data_size = READ_DWORD_BE_U(buf); - if (this->data_size > this->img_size) { + if (this->data_size > img_size) { img_file.close(); LOG_F(ERROR, "DiskCopy42Img: invalid data size %d", this->data_size); return -1; } + this->img_size = (int)img_size; uint8_t disk_format = 0xFFU; diff --git a/devices/floppy/superdrive.cpp b/devices/floppy/superdrive.cpp index 0205c96..b57b40b 100644 --- a/devices/floppy/superdrive.cpp +++ b/devices/floppy/superdrive.cpp @@ -295,7 +295,7 @@ uint64_t MacSuperDrive::sync_to_disk() track_time_ns -= this->index_delay; // calculate current sector number from timestamp - int cur_sect_num = this->cur_sector = track_time_ns / this->sector_delay; + int cur_sect_num = this->cur_sector = (int)(track_time_ns / this->sector_delay); this->sector_start_time = this->track_start_time + cur_sect_num * this->sector_delay + this->index_delay; diff --git a/devices/serial/chario.cpp b/devices/serial/chario.cpp index 1ed1907..c9892b9 100644 --- a/devices/serial/chario.cpp +++ b/devices/serial/chario.cpp @@ -377,7 +377,7 @@ bool CharIoSocket::rcv_char_available() if (sockfd != -1) { if (FD_ISSET(sockfd, &readfds)) { uint8_t c; - int received = recv(sockfd, &c, 1, 0); + int received = (int)recv(sockfd, &c, 1, 0); if (received == -1) { if (acceptfd == -1) { //LOG_F(INFO, "socket sock read (not accepted yet) err: %s", strerror(errno)); // this happens once before accept @@ -443,7 +443,7 @@ int CharIoSocket::xmit_char(uint8_t c) CharIoSocket::rcv_char_available(); if (acceptfd != -1) { - int sent = send(acceptfd, &c, 1, 0); + int sent = (int)send(acceptfd, &c, 1, 0); if (sent == -1) { LOG_F(INFO, "socket accept write err: %s", strerror(errno)); } @@ -463,7 +463,7 @@ int CharIoSocket::rcv_char(uint8_t *c) CharIoSocket::rcv_char_available(); if (acceptfd != -1) { - int received = recv(acceptfd, c, 1, 0); + int received = (int)recv(acceptfd, c, 1, 0); if (received == -1) { LOG_F(INFO, "socket accept read err: %s", strerror(errno)); } diff --git a/devices/sound/soundserver.cpp b/devices/sound/soundserver.cpp index 66091be..327a663 100644 --- a/devices/sound/soundserver.cpp +++ b/devices/sound/soundserver.cpp @@ -147,12 +147,12 @@ long sound_out_callback(cubeb_stream *stream, void *user_data, out_frames = 0; while (req_frames > 0) { - if (!dma_ch->pull_data(req_frames << 2, &got_len, &p_in)) { + if (!dma_ch->pull_data((uint32_t)req_frames << 2, &got_len, &p_in)) { frames = got_len >> 2; in_buf = (int16_t*)p_in; - for (int i = frames; i > 0; i--) { + for (int i = (int)frames; i > 0; i--) { out_buf[0] = BYTESWAP_16(in_buf[0]); out_buf[1] = BYTESWAP_16(in_buf[1]); in_buf += 2; diff --git a/machines/machinefactory.cpp b/machines/machinefactory.cpp index e9971cd..468bd32 100644 --- a/machines/machinefactory.cpp +++ b/machines/machinefactory.cpp @@ -269,7 +269,8 @@ void MachineFactory::set_machine_settings(map &settings) { string MachineFactory::machine_name_from_rom(string& rom_filepath) { ifstream rom_file; - uint32_t file_size, config_info_offset, rom_id; + size_t file_size; + uint32_t config_info_offset, rom_id; char rom_id_str[17]; string machine_name = ""; @@ -354,7 +355,7 @@ int MachineFactory::load_boot_rom(string& rom_filepath) { gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL)); if ((rom_reg = mem_ctrl->find_rom_region())) { - mem_ctrl->set_data(rom_reg->start, sysrom_mem, file_size); + mem_ctrl->set_data(rom_reg->start, sysrom_mem, (uint32_t)file_size); } else { ABORT_F("Could not locate physical ROM region!"); } diff --git a/machines/machineproperties.cpp b/machines/machineproperties.cpp index c82cf42..e4bfe6d 100644 --- a/machines/machineproperties.cpp +++ b/machines/machineproperties.cpp @@ -73,7 +73,7 @@ bool StrProperty::check_val(std::string str) uint32_t IntProperty::get_int() { try { - uint32_t result = strtoul(this->get_string().c_str(), 0, 0); + uint32_t result = (uint32_t)strtoul(this->get_string().c_str(), 0, 0); /* perform value check */ if (!this->check_val(result)) {