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.
This commit is contained in:
joevt 2022-12-21 03:20:39 -08:00
parent bd5a88f02f
commit 64fec88436
14 changed files with 48 additions and 38 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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 (...) {

View File

@ -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

View File

@ -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;
}

View File

@ -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) {

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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));
}

View File

@ -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;

View File

@ -269,7 +269,8 @@ void MachineFactory::set_machine_settings(map<string, string> &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!");
}

View File

@ -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)) {