From ec1ca60f72b154d1a090714e2f493fd962ce9c32 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 9 Jul 2023 10:43:39 +0200 Subject: [PATCH 1/7] Fix warnings related to `sprintf` usage --- src/Unix/darwin/ethernet_darwin.cpp | 4 ++-- src/cfgopts.cpp | 22 +++++++++++----------- src/disasm/disasm-builtin.cpp | 16 ++++++++-------- src/gui-sdl/dlgDisk.cpp | 10 +++++----- src/gui-sdl/dlgPartition.cpp | 4 ++-- src/gui-sdl/dlgVideo.cpp | 4 ++-- src/gui-sdl/file.cpp | 2 +- src/hostscreen.cpp | 2 +- src/natfeat/hostfs.cpp | 2 +- src/natfeat/xhdi.cpp | 2 +- src/uae_cpu/fpu/fpu_uae.cpp | 2 +- 11 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Unix/darwin/ethernet_darwin.cpp b/src/Unix/darwin/ethernet_darwin.cpp index 698faeec9..d9691224e 100644 --- a/src/Unix/darwin/ethernet_darwin.cpp +++ b/src/Unix/darwin/ethernet_darwin.cpp @@ -269,10 +269,10 @@ int TunTapEthernetHandler::tapOpenOld(char *dev) } for(i=0; i < 255; i++) { - sprintf(tapname, "/dev/tap%d", i); + snprintf(tapname, 21, "/dev/tap%d", i); /* Open device */ if( (fd=::open(tapname, O_RDWR)) > 0 ) { - sprintf(dev, "tap%d",i); + snprintf(dev, 7, "tap%d",i); D(bug("TunTap(%d): tapOpenOld %s", ethX, dev)); return fd; } diff --git a/src/cfgopts.cpp b/src/cfgopts.cpp index bc9be5a05..d7b2fa7e9 100644 --- a/src/cfgopts.cpp +++ b/src/cfgopts.cpp @@ -419,77 +419,77 @@ char *ConfigOptions::get_config_value(const struct Config_Tag *ptr, bool type) if (type) strcpy(value, "byte"); else - sprintf(value, "%d", *((char *)(ptr->buf))); + snprintf(value, 40, "%d", *((char *)(ptr->buf))); break; case Word_Tag: if (type) strcpy(value, "word"); else - sprintf(value, "%d", *((short *)(ptr->buf))); + snprintf(value, 40, "%d", *((short *)(ptr->buf))); break; case Int_Tag: if (type) strcpy(value, "int"); else - sprintf(value, "%d", *((int *)(ptr->buf))); + snprintf(value, 40, "%d", *((int *)(ptr->buf))); break; case Long_Tag: if (type) strcpy(value, "long"); else - sprintf(value, "%ld", *((long *)(ptr->buf))); + snprintf(value, 40, "%ld", *((long *)(ptr->buf))); break; case OctWord_Tag: if (type) strcpy(value, "octword"); else - sprintf(value, "%o", *((short *)(ptr->buf))); + snprintf(value, 40, "%o", *((short *)(ptr->buf))); break; case OctLong_Tag: if (type) strcpy(value, "octlong"); else - sprintf(value, "%lo", *((long *)(ptr->buf))); + snprintf(value, 40, "%lo", *((long *)(ptr->buf))); break; case HexWord_Tag: if (type) strcpy(value, "hexword"); else - sprintf(value, "%x", *((short *)(ptr->buf))); + snprintf(value, 40, "%x", *((short *)(ptr->buf))); break; case HexLong_Tag: if (type) strcpy(value, "hexlong"); else - sprintf(value, "%lx", *((long *)(ptr->buf))); + snprintf(value, 40, "%lx", *((long *)(ptr->buf))); break; case Float_Tag: if (type) strcpy(value, "float"); else - sprintf(value, "%g", *((float *)(ptr->buf))); + snprintf(value, 40, "%g", *((float *)(ptr->buf))); break; case Double_Tag: if (type) strcpy(value, "double"); else - sprintf(value, "%g", *((double *)(ptr->buf))); + snprintf(value, 40, "%g", *((double *)(ptr->buf))); break; case Char_Tag: if (type) strcpy(value, "char"); else - sprintf(value, "%c", *((char *)(ptr->buf))); + snprintf(value, 40, "%c", *((char *)(ptr->buf))); break; case Path_Tag: diff --git a/src/disasm/disasm-builtin.cpp b/src/disasm/disasm-builtin.cpp index d0775d3ff..43c39c85b 100644 --- a/src/disasm/disasm-builtin.cpp +++ b/src/disasm/disasm-builtin.cpp @@ -717,24 +717,24 @@ static void oi(m68k_disasm_info *info, int size, bool immed) info->memory_vma += 4; break; case FPU_SIZE_SINGLE: - sprintf(str, "%.17e", make_single(GETUL(info->memory_vma))); + snprintf(str, 100, "%.17e", make_single(GETUL(info->memory_vma))); ps(info, str); info->memory_vma += 4; break; case FPU_SIZE_EXTENDED: #ifdef NO_LONGDOUBLE_PRINTF - sprintf(str, "%.17e", (double) make_extended(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, 100, "%.17e", (double) make_extended(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #else - sprintf(str, "%.17Le", make_extended(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, 100, "%.17Le", make_extended(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #endif ps(info, str); info->memory_vma += 12; break; case FPU_SIZE_PACKED: #ifdef NO_LONGDOUBLE_PRINTF - sprintf(str, "%.17e", (double) make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, 100, "%.17e", (double) make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #else - sprintf(str, "%.17Le", make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, 100, "%.17Le", make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #endif ps(info, str); info->memory_vma += 12; @@ -744,7 +744,7 @@ static void oi(m68k_disasm_info *info, int size, bool immed) info->memory_vma += 2; break; case FPU_SIZE_DOUBLE: - sprintf(str, "%.17e", make_double(GETUL(info->memory_vma), GETUL(info->memory_vma + 4))); + snprintf(str, 100, "%.17e", make_double(GETUL(info->memory_vma), GETUL(info->memory_vma + 4))); ps(info, str); info->memory_vma += 8; break; @@ -754,9 +754,9 @@ static void oi(m68k_disasm_info *info, int size, bool immed) break; case FPU_SIZE_PACKED_VARIABLE: #ifdef NO_LONGDOUBLE_PRINTF - sprintf(str, "%.17e", (double) make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, 100, "%.17e", (double) make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #else - sprintf(str, "%.17Le", make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, 100, "%.17Le", make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #endif ps(info, str); info->memory_vma += 12; diff --git a/src/gui-sdl/dlgDisk.cpp b/src/gui-sdl/dlgDisk.cpp index 2ced4c230..7f3e5e830 100644 --- a/src/gui-sdl/dlgDisk.cpp +++ b/src/gui-sdl/dlgDisk.cpp @@ -172,10 +172,10 @@ static void UpdateDiskParameters(int disk, bool updateCHS) } // output - sprintf(disk == 0 ? ide0_size : ide1_size, "%6d", sizeMB); - sprintf(disk == 0 ? ide0_cyl : ide1_cyl, "%5d", cyl); - sprintf(disk == 0 ? ide0_head : ide1_head, "%2d", head); - sprintf(disk == 0 ? ide0_spt : ide1_spt, "%3d", spt); + snprintf(disk == 0 ? ide0_size : ide1_size, 7, "%6d", sizeMB); + snprintf(disk == 0 ? ide0_cyl : ide1_cyl, 6, "%5d", cyl); + snprintf(disk == 0 ? ide0_head : ide1_head, 3, "%2d", head); + snprintf(disk == 0 ? ide0_spt : ide1_spt, 4, "%3d", spt); } /* produce the image file */ @@ -225,7 +225,7 @@ void DlgDisk::init_create_disk_image(int disk) sizeMB = BAR130G; } char text[250]; - sprintf(text, "Create disk image '%s' with size %ld MB?", cdi_path, sizeMB); + snprintf(text, 250, "Create disk image '%s' with size %ld MB?", cdi_path, sizeMB); dlgAlert = (DlgAlert *) DlgAlertOpen(text, ALERT_OKCANCEL); SDLGui_Open(dlgAlert); state = STATE_CDI0; diff --git a/src/gui-sdl/dlgPartition.cpp b/src/gui-sdl/dlgPartition.cpp index 5d174f10c..155d6cd26 100644 --- a/src/gui-sdl/dlgPartition.cpp +++ b/src/gui-sdl/dlgPartition.cpp @@ -108,7 +108,7 @@ static void UpdateDiskParameters(int disk) int sizeMB = ((size / 1024) + 512) / 1024; // output - sprintf(part_size[disk], "%6d", sizeMB); + snprintf(part_size[disk], 7, "%6d", sizeMB); } void DlgPartition::init_create_disk_image(int disk) @@ -117,7 +117,7 @@ void DlgPartition::init_create_disk_image(int disk) cdi_disk = disk; sizeMB = atoi(part_size[disk]); char text[250]; - sprintf(text, "Create partition image '%s' with size %ld MB?", cdi_path, sizeMB); + snprintf(text, 250, "Create partition image '%s' with size %ld MB?", cdi_path, sizeMB); dlgAlert = (DlgAlert *) DlgAlertOpen(text, ALERT_OKCANCEL); SDLGui_Open(dlgAlert); state = STATE_CDI0; diff --git a/src/gui-sdl/dlgVideo.cpp b/src/gui-sdl/dlgVideo.cpp index b644b80f4..ef45dd217 100644 --- a/src/gui-sdl/dlgVideo.cpp +++ b/src/gui-sdl/dlgVideo.cpp @@ -117,8 +117,8 @@ DlgVideo::DlgVideo(SGOBJ *dlg) else { videodlg[RES_CUSTOM].state |= SG_SELECTED; } - sprintf(video_width, "%4d", autozoom->width); - sprintf(video_height, "%4d", autozoom->height); + snprintf(video_width, 5, "%4d", autozoom->width); + snprintf(video_height, 5, "%4d", autozoom->height); } DlgVideo::~DlgVideo() diff --git a/src/gui-sdl/file.cpp b/src/gui-sdl/file.cpp index 6b9330a92..e2cc876e4 100644 --- a/src/gui-sdl/file.cpp +++ b/src/gui-sdl/file.cpp @@ -206,7 +206,7 @@ bool File_QueryOverwrite(char *pszFileName) /* Try and find if file exists */ if (File_Exists(pszFileName)) { /* File does exist, are we OK to overwrite? */ - sprintf(szString,"File '%s' exists, overwrite?",pszFileName); + snprintf(szString, MAX_FILENAME_LENGTH, "File '%s' exists, overwrite?",pszFileName); /* FIXME: */ // if (MessageBox(hWnd,szString,PROG_NAME,MB_YESNO | MB_DEFBUTTON2 | MB_ICONSTOP)==IDNO) // return(false); diff --git a/src/hostscreen.cpp b/src/hostscreen.cpp index a7244d157..55dee5a9d 100644 --- a/src/hostscreen.cpp +++ b/src/hostscreen.cpp @@ -320,7 +320,7 @@ void HostScreen::writeSnapshot(SDL_Surface *surf) if (snapCounter == 0) snapCounter = get_screenshot_counter(); - sprintf(filename, "snap%03d.bmp", snapCounter++ ); + snprintf(filename, 15, "snap%03d.bmp", snapCounter++ ); safe_strncpy(path, bx_options.snapshot_dir, sizeof(path)); addFilename(path, filename, sizeof(path)); #ifdef SDL_HINT_BMP_SAVE_LEGACY_FORMAT diff --git a/src/natfeat/hostfs.cpp b/src/natfeat/hostfs.cpp index 1952dd354..041e78828 100644 --- a/src/natfeat/hostfs.cpp +++ b/src/natfeat/hostfs.cpp @@ -954,7 +954,7 @@ void HostFs::transformFileName( char* dest, const char* source ) // hash value hex string as the unique shortenning char hashString[10]; - sprintf( hashString, "%08x", hashValue ); + snprintf(hashString, 10, "%08x", hashValue ); hashString[5] = '~'; char *hashStr = &hashString[5]; diff --git a/src/natfeat/xhdi.cpp b/src/natfeat/xhdi.cpp index 5e3fcd336..d9dfce3fe 100644 --- a/src/natfeat/xhdi.cpp +++ b/src/natfeat/xhdi.cpp @@ -114,7 +114,7 @@ void XHDIDriver::copy_atadevice_settings(const bx_atadevice_options_t *src, disk void XHDIDriver::copy_scsidevice_settings(int index, const bx_scsidevice_options_t *src, disk_t *dest) { safe_strncpy(dest->path, src->path, sizeof(dest->path)); - sprintf(dest->name, "PARTITION%d", index); + snprintf(dest->name, 41, "PARTITION%d", index); dest->present = src->present; dest->readonly = src->readonly; dest->byteswap = src->byteswap; diff --git a/src/uae_cpu/fpu/fpu_uae.cpp b/src/uae_cpu/fpu/fpu_uae.cpp index 19eb94a32..757600b01 100644 --- a/src/uae_cpu/fpu/fpu_uae.cpp +++ b/src/uae_cpu/fpu/fpu_uae.cpp @@ -821,7 +821,7 @@ PRIVATE inline void FFPU extract_packed(fpu_register const & src, uae_u32 * wrd1 char *cp; char str[100]; - sprintf(str, "%.16e", src); + snprintf(str, 100, "%.16e", src) fpu_debug(("extract_packed(%.04f,%s)\n",(double)src,str)); From 600ebc7b43d869b9ce48d7f4e1a5c68107aba486 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 9 Jul 2023 11:02:59 +0200 Subject: [PATCH 2/7] Disable warnings about implicit 64-bit to 32-bit conversion --- src/Unix/MacOSX/MacAranym-Latest.xcodeproj/project.pbxproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Unix/MacOSX/MacAranym-Latest.xcodeproj/project.pbxproj b/src/Unix/MacOSX/MacAranym-Latest.xcodeproj/project.pbxproj index 0c6057d8b..ef330aea5 100644 --- a/src/Unix/MacOSX/MacAranym-Latest.xcodeproj/project.pbxproj +++ b/src/Unix/MacOSX/MacAranym-Latest.xcodeproj/project.pbxproj @@ -2827,6 +2827,7 @@ GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 3; GCC_UNROLL_LOOPS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = NO; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES; @@ -3087,6 +3088,7 @@ GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_UNROLL_LOOPS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = NO; GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES; From d69074d2060e14dfd77af504c5af61ad968c2e46 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 10 Jul 2023 09:08:58 +0200 Subject: [PATCH 3/7] Use `sizeof` where possible --- src/Unix/darwin/ethernet_darwin.cpp | 2 +- src/cfgopts.cpp | 22 +++++++++++----------- src/disasm/disasm-builtin.cpp | 16 ++++++++-------- src/gui-sdl/dlgDisk.cpp | 10 +++++----- src/gui-sdl/dlgPartition.cpp | 2 +- src/gui-sdl/dlgVideo.cpp | 4 ++-- src/hostscreen.cpp | 2 +- src/natfeat/hostfs.cpp | 2 +- src/natfeat/xhdi.cpp | 2 +- src/uae_cpu/fpu/fpu_uae.cpp | 2 +- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/Unix/darwin/ethernet_darwin.cpp b/src/Unix/darwin/ethernet_darwin.cpp index d9691224e..4f40b346c 100644 --- a/src/Unix/darwin/ethernet_darwin.cpp +++ b/src/Unix/darwin/ethernet_darwin.cpp @@ -269,7 +269,7 @@ int TunTapEthernetHandler::tapOpenOld(char *dev) } for(i=0; i < 255; i++) { - snprintf(tapname, 21, "/dev/tap%d", i); + snprintf(tapname, sizeof(tapname), "/dev/tap%d", i); /* Open device */ if( (fd=::open(tapname, O_RDWR)) > 0 ) { snprintf(dev, 7, "tap%d",i); diff --git a/src/cfgopts.cpp b/src/cfgopts.cpp index d7b2fa7e9..29bc0be5f 100644 --- a/src/cfgopts.cpp +++ b/src/cfgopts.cpp @@ -419,77 +419,77 @@ char *ConfigOptions::get_config_value(const struct Config_Tag *ptr, bool type) if (type) strcpy(value, "byte"); else - snprintf(value, 40, "%d", *((char *)(ptr->buf))); + snprintf(value, sizeof(value), "%d", *((char *)(ptr->buf))); break; case Word_Tag: if (type) strcpy(value, "word"); else - snprintf(value, 40, "%d", *((short *)(ptr->buf))); + snprintf(value, sizeof(value), "%d", *((short *)(ptr->buf))); break; case Int_Tag: if (type) strcpy(value, "int"); else - snprintf(value, 40, "%d", *((int *)(ptr->buf))); + snprintf(value, sizeof(value), "%d", *((int *)(ptr->buf))); break; case Long_Tag: if (type) strcpy(value, "long"); else - snprintf(value, 40, "%ld", *((long *)(ptr->buf))); + snprintf(value, sizeof(value), "%ld", *((long *)(ptr->buf))); break; case OctWord_Tag: if (type) strcpy(value, "octword"); else - snprintf(value, 40, "%o", *((short *)(ptr->buf))); + snprintf(value, sizeof(value), "%o", *((short *)(ptr->buf))); break; case OctLong_Tag: if (type) strcpy(value, "octlong"); else - snprintf(value, 40, "%lo", *((long *)(ptr->buf))); + snprintf(value, sizeof(value), "%lo", *((long *)(ptr->buf))); break; case HexWord_Tag: if (type) strcpy(value, "hexword"); else - snprintf(value, 40, "%x", *((short *)(ptr->buf))); + snprintf(value, sizeof(value), "%x", *((short *)(ptr->buf))); break; case HexLong_Tag: if (type) strcpy(value, "hexlong"); else - snprintf(value, 40, "%lx", *((long *)(ptr->buf))); + snprintf(value, sizeof(value), "%lx", *((long *)(ptr->buf))); break; case Float_Tag: if (type) strcpy(value, "float"); else - snprintf(value, 40, "%g", *((float *)(ptr->buf))); + snprintf(value, sizeof(value), "%g", *((float *)(ptr->buf))); break; case Double_Tag: if (type) strcpy(value, "double"); else - snprintf(value, 40, "%g", *((double *)(ptr->buf))); + snprintf(value, sizeof(value), "%g", *((double *)(ptr->buf))); break; case Char_Tag: if (type) strcpy(value, "char"); else - snprintf(value, 40, "%c", *((char *)(ptr->buf))); + snprintf(value, sizeof(value), "%c", *((char *)(ptr->buf))); break; case Path_Tag: diff --git a/src/disasm/disasm-builtin.cpp b/src/disasm/disasm-builtin.cpp index 43c39c85b..34ea4d052 100644 --- a/src/disasm/disasm-builtin.cpp +++ b/src/disasm/disasm-builtin.cpp @@ -717,24 +717,24 @@ static void oi(m68k_disasm_info *info, int size, bool immed) info->memory_vma += 4; break; case FPU_SIZE_SINGLE: - snprintf(str, 100, "%.17e", make_single(GETUL(info->memory_vma))); + snprintf(str, sizeof(str), "%.17e", make_single(GETUL(info->memory_vma))); ps(info, str); info->memory_vma += 4; break; case FPU_SIZE_EXTENDED: #ifdef NO_LONGDOUBLE_PRINTF - snprintf(str, 100, "%.17e", (double) make_extended(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, sizeof(str), "%.17e", (double) make_extended(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #else - snprintf(str, 100, "%.17Le", make_extended(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, sizeof(str), "%.17Le", make_extended(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #endif ps(info, str); info->memory_vma += 12; break; case FPU_SIZE_PACKED: #ifdef NO_LONGDOUBLE_PRINTF - snprintf(str, 100, "%.17e", (double) make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, sizeof(str), "%.17e", (double) make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #else - snprintf(str, 100, "%.17Le", make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, sizeof(str), "%.17Le", make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #endif ps(info, str); info->memory_vma += 12; @@ -744,7 +744,7 @@ static void oi(m68k_disasm_info *info, int size, bool immed) info->memory_vma += 2; break; case FPU_SIZE_DOUBLE: - snprintf(str, 100, "%.17e", make_double(GETUL(info->memory_vma), GETUL(info->memory_vma + 4))); + snprintf(str, sizeof(str), "%.17e", make_double(GETUL(info->memory_vma), GETUL(info->memory_vma + 4))); ps(info, str); info->memory_vma += 8; break; @@ -754,9 +754,9 @@ static void oi(m68k_disasm_info *info, int size, bool immed) break; case FPU_SIZE_PACKED_VARIABLE: #ifdef NO_LONGDOUBLE_PRINTF - snprintf(str, 100, "%.17e", (double) make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, sizeof(str), "%.17e", (double) make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #else - snprintf(str, 100, "%.17Le", make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); + snprintf(str, sizeof(str), "%.17Le", make_packed(GETUL(info->memory_vma), GETUL(info->memory_vma + 4), GETUL(info->memory_vma + 8))); #endif ps(info, str); info->memory_vma += 12; diff --git a/src/gui-sdl/dlgDisk.cpp b/src/gui-sdl/dlgDisk.cpp index 7f3e5e830..6a628b648 100644 --- a/src/gui-sdl/dlgDisk.cpp +++ b/src/gui-sdl/dlgDisk.cpp @@ -172,10 +172,10 @@ static void UpdateDiskParameters(int disk, bool updateCHS) } // output - snprintf(disk == 0 ? ide0_size : ide1_size, 7, "%6d", sizeMB); - snprintf(disk == 0 ? ide0_cyl : ide1_cyl, 6, "%5d", cyl); - snprintf(disk == 0 ? ide0_head : ide1_head, 3, "%2d", head); - snprintf(disk == 0 ? ide0_spt : ide1_spt, 4, "%3d", spt); + snprintf(disk == 0 ? ide0_size : ide1_size, sizeof(ide0_size), "%6d", sizeMB); + snprintf(disk == 0 ? ide0_cyl : ide1_cyl, sizeof(ide0_cyl), "%5d", cyl); + snprintf(disk == 0 ? ide0_head : ide1_head, sizeof(ide0_head), "%2d", head); + snprintf(disk == 0 ? ide0_spt : ide1_spt, sizeof(ide0_spt), "%3d", spt); } /* produce the image file */ @@ -225,7 +225,7 @@ void DlgDisk::init_create_disk_image(int disk) sizeMB = BAR130G; } char text[250]; - snprintf(text, 250, "Create disk image '%s' with size %ld MB?", cdi_path, sizeMB); + snprintf(text, sizeof(text), "Create disk image '%s' with size %ld MB?", cdi_path, sizeMB); dlgAlert = (DlgAlert *) DlgAlertOpen(text, ALERT_OKCANCEL); SDLGui_Open(dlgAlert); state = STATE_CDI0; diff --git a/src/gui-sdl/dlgPartition.cpp b/src/gui-sdl/dlgPartition.cpp index 155d6cd26..a05439a31 100644 --- a/src/gui-sdl/dlgPartition.cpp +++ b/src/gui-sdl/dlgPartition.cpp @@ -117,7 +117,7 @@ void DlgPartition::init_create_disk_image(int disk) cdi_disk = disk; sizeMB = atoi(part_size[disk]); char text[250]; - snprintf(text, 250, "Create partition image '%s' with size %ld MB?", cdi_path, sizeMB); + snprintf(text, sizeof(text), "Create partition image '%s' with size %ld MB?", cdi_path, sizeMB); dlgAlert = (DlgAlert *) DlgAlertOpen(text, ALERT_OKCANCEL); SDLGui_Open(dlgAlert); state = STATE_CDI0; diff --git a/src/gui-sdl/dlgVideo.cpp b/src/gui-sdl/dlgVideo.cpp index ef45dd217..d739cc98e 100644 --- a/src/gui-sdl/dlgVideo.cpp +++ b/src/gui-sdl/dlgVideo.cpp @@ -117,8 +117,8 @@ DlgVideo::DlgVideo(SGOBJ *dlg) else { videodlg[RES_CUSTOM].state |= SG_SELECTED; } - snprintf(video_width, 5, "%4d", autozoom->width); - snprintf(video_height, 5, "%4d", autozoom->height); + snprintf(video_width, sizeof(video_width), "%4d", autozoom->width); + snprintf(video_height, sizeof(video_height), "%4d", autozoom->height); } DlgVideo::~DlgVideo() diff --git a/src/hostscreen.cpp b/src/hostscreen.cpp index 55dee5a9d..b335a82bf 100644 --- a/src/hostscreen.cpp +++ b/src/hostscreen.cpp @@ -320,7 +320,7 @@ void HostScreen::writeSnapshot(SDL_Surface *surf) if (snapCounter == 0) snapCounter = get_screenshot_counter(); - snprintf(filename, 15, "snap%03d.bmp", snapCounter++ ); + snprintf(filename, sizeof(filename), "snap%03d.bmp", snapCounter++ ); safe_strncpy(path, bx_options.snapshot_dir, sizeof(path)); addFilename(path, filename, sizeof(path)); #ifdef SDL_HINT_BMP_SAVE_LEGACY_FORMAT diff --git a/src/natfeat/hostfs.cpp b/src/natfeat/hostfs.cpp index 041e78828..a4e73a0c1 100644 --- a/src/natfeat/hostfs.cpp +++ b/src/natfeat/hostfs.cpp @@ -954,7 +954,7 @@ void HostFs::transformFileName( char* dest, const char* source ) // hash value hex string as the unique shortenning char hashString[10]; - snprintf(hashString, 10, "%08x", hashValue ); + snprintf(hashString, sizeof(hashString), "%08x", hashValue ); hashString[5] = '~'; char *hashStr = &hashString[5]; diff --git a/src/natfeat/xhdi.cpp b/src/natfeat/xhdi.cpp index d9dfce3fe..b8207bb20 100644 --- a/src/natfeat/xhdi.cpp +++ b/src/natfeat/xhdi.cpp @@ -114,7 +114,7 @@ void XHDIDriver::copy_atadevice_settings(const bx_atadevice_options_t *src, disk void XHDIDriver::copy_scsidevice_settings(int index, const bx_scsidevice_options_t *src, disk_t *dest) { safe_strncpy(dest->path, src->path, sizeof(dest->path)); - snprintf(dest->name, 41, "PARTITION%d", index); + snprintf(dest->name, sizeof(dest->name), "PARTITION%d", index); dest->present = src->present; dest->readonly = src->readonly; dest->byteswap = src->byteswap; diff --git a/src/uae_cpu/fpu/fpu_uae.cpp b/src/uae_cpu/fpu/fpu_uae.cpp index 757600b01..6e811bae7 100644 --- a/src/uae_cpu/fpu/fpu_uae.cpp +++ b/src/uae_cpu/fpu/fpu_uae.cpp @@ -821,7 +821,7 @@ PRIVATE inline void FFPU extract_packed(fpu_register const & src, uae_u32 * wrd1 char *cp; char str[100]; - snprintf(str, 100, "%.16e", src) + snprintf(str, sizeof(str), "%.16e", src); fpu_debug(("extract_packed(%.04f,%s)\n",(double)src,str)); From b989346dc964c6d00e35c88c6f1a675ff3f5bfc5 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 17 Jul 2023 13:58:19 +0200 Subject: [PATCH 4/7] Use SDL2 scancodes to map to Atari scancodes No more symbolic key mapping for SDL 2! --- src/input.cpp | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index bd4389604..76acce215 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -513,6 +513,7 @@ static int keysymToAtari(SDL_Keysym *keysym) case SDL_SCANCODE_GRAVE: scancode = 0x29; break; case SDL_SCANCODE_LSHIFT: scancode = 0x2a; break; case SDL_SCANCODE_BACKSLASH: scancode = 0x2b; break; + case SDL_SCANCODE_NONUSHASH: scancode = 0x2b; break; case SDL_SCANCODE_Z: scancode = 0x2c; break; case SDL_SCANCODE_X: scancode = 0x2d; break; case SDL_SCANCODE_C: scancode = 0x2e; break; @@ -524,7 +525,7 @@ static int keysymToAtari(SDL_Keysym *keysym) case SDL_SCANCODE_PERIOD: scancode = 0x34; break; case SDL_SCANCODE_SLASH: scancode = 0x35; break; case SDL_SCANCODE_RSHIFT: scancode = 0x36; break; - case SDL_SCANCODE_PRINTSCREEN: scancode = 0x37; break; + //case SDL_SCANCODE_PRINTSCREEN: scancode = 0x37; break; // Not used on Atari, why map it? case SDL_SCANCODE_LALT: scancode = 0x38; break; case SDL_SCANCODE_SPACE: scancode = 0x39; break; case SDL_SCANCODE_CAPSLOCK: scancode = 0x3a; break; @@ -539,19 +540,49 @@ static int keysymToAtari(SDL_Keysym *keysym) case SDL_SCANCODE_F9: scancode = 0x43; break; case SDL_SCANCODE_F10: scancode = 0x44; break; + case SDL_SCANCODE_HOME: scancode = 0x47; break; + case SDL_SCANCODE_UP: scancode = 0x48; break; + case SDL_SCANCODE_PAGEUP: scancode = 0x49; break; + case SDL_SCANCODE_KP_MINUS: scancode = 0x4a; break; + case SDL_SCANCODE_LEFT: scancode = 0x4b; break; + case SDL_SCANCODE_RALT: scancode = 0x4c; break; + case SDL_SCANCODE_RIGHT: scancode = 0x4d; break; + case SDL_SCANCODE_KP_PLUS: scancode = 0x4e; break; + case SDL_SCANCODE_END: scancode = 0x4f; break; /* Milan's scancode for End */ + case SDL_SCANCODE_DOWN: scancode = 0x50; break; + case SDL_SCANCODE_PAGEDOWN: scancode = 0x51; break; + case SDL_SCANCODE_INSERT: scancode = 0x52; break; + case SDL_SCANCODE_DELETE: scancode = 0x53; break; + case SDL_SCANCODE_NONUSBACKSLASH: scancode = 0x60; break; - case SDL_SCANCODE_KP_LEFTPAREN: scancode = 0x63; break; + case SDL_SCANCODE_UNDO: scancode = 0x61; break; + case SDL_SCANCODE_HELP: scancode = 0x62; break; + case SDL_SCANCODE_KP_LEFTPAREN: scancode = 0x63; break; case SDL_SCANCODE_KP_RIGHTPAREN: scancode = 0x64; break; - - case SDL_SCANCODE_SCROLLLOCK: scancode = 0x00; break; - case SDL_SCANCODE_PAUSE: scancode = 0x00; break; + case SDL_SCANCODE_KP_DIVIDE: scancode = 0x65; break; + case SDL_SCANCODE_KP_MULTIPLY: scancode = 0x66; break; + case SDL_SCANCODE_KP_7: scancode = 0x67; break; + case SDL_SCANCODE_KP_8: scancode = 0x68; break; + case SDL_SCANCODE_KP_9: scancode = 0x69; break; + case SDL_SCANCODE_KP_4: scancode = 0x6A; break; + case SDL_SCANCODE_KP_5: scancode = 0x6B; break; + case SDL_SCANCODE_KP_6: scancode = 0x6C; break; + case SDL_SCANCODE_KP_1: scancode = 0x6D; break; + case SDL_SCANCODE_KP_2: scancode = 0x6E; break; + case SDL_SCANCODE_KP_3: scancode = 0x6F; break; + case SDL_SCANCODE_KP_0: scancode = 0x70; break; + case SDL_SCANCODE_KP_PERIOD: scancode = 0x71; break; + case SDL_SCANCODE_KP_ENTER: scancode = 0x72; break; } if (scancode != 0) { keysym->scancode = SDL_Scancode(scancode); return scancode; } -#endif + bug("Unmapped keycode: 0x%x, scancode %d (0x%x), keysym '%s'", + keysym->sym, keysym->scancode, keysym->scancode, SDL_GetKeyName(keysym->sym)); + return scancode; +#else switch ((unsigned int) keysym->sym) { /* Numeric Pad */ @@ -597,7 +628,6 @@ static int keysymToAtari(SDL_Keysym *keysym) case SDLK_RALT: scancode = RALT_ATARI_SCANCODE; break; } -#if !SDL_VERSION_ATLEAST(2, 0, 0) static unsigned int offset = UNDEFINED_OFFSET; if (scancode == 0) { @@ -918,7 +948,9 @@ static void process_keyboard_event(const SDL_Event &event) // send all pressed keys to IKBD if (send2Atari) { int scanAtari = keysymToAtari(&keysym); - D(bug("Host scancode = %d ($%02x), Atari scancode = %d ($%02x), keycode = '%s' ($%02x)", keysym.scancode, keysym.scancode, pressed ? scanAtari : scanAtari|0x80, pressed ? scanAtari : scanAtari|0x80, SDL_GetKeyName(sym), sym)); + if (pressed) { + bug("Host scancode = %d ($%02x), Atari scancode = %d ($%02x), keycode = '%s' ($%02x)", keysym.scancode, keysym.scancode, pressed ? scanAtari : scanAtari|0x80, pressed ? scanAtari : scanAtari|0x80, SDL_GetKeyName(sym), sym); + } if (scanAtari > 0) { if (!pressed) scanAtari |= 0x80; From c802303a248592740aa625ec7312d04d138b57c5 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 17 Jul 2023 14:00:16 +0200 Subject: [PATCH 5/7] Disable special keys which simulate page up/down by sending shift+up/down. This should be user configurable! --- src/input.cpp | 52 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 76acce215..d608209b0 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -917,32 +917,32 @@ static void process_keyboard_event(const SDL_Event &event) } // map special keys to Atari range of scancodes - if (sym == SDLK_PAGEUP) { - if (pressed) { - if (! shifted) - getIKBD()->SendKey(0x2a); // press and hold LShift - getIKBD()->SendKey(0x48); // press keyUp - } - else { - getIKBD()->SendKey(0xc8); // release keyUp - if (! shifted) - getIKBD()->SendKey(0xaa); // release LShift - } - send2Atari = false; - } - else if (sym == SDLK_PAGEDOWN) { - if (pressed) { - if (! shifted) - getIKBD()->SendKey(0x2a); // press and hold LShift - getIKBD()->SendKey(0x50); // press keyDown - } - else { - getIKBD()->SendKey(0xd0); // release keyDown - if (! shifted) - getIKBD()->SendKey(0xaa); // release LShift - } - send2Atari = false; - } +// if (sym == SDLK_PAGEUP) { +// if (pressed) { +// if (! shifted) +// getIKBD()->SendKey(0x2a); // press and hold LShift +// getIKBD()->SendKey(0x48); // press keyUp +// } +// else { +// getIKBD()->SendKey(0xc8); // release keyUp +// if (! shifted) +// getIKBD()->SendKey(0xaa); // release LShift +// } +// send2Atari = false; +// } +// else if (sym == SDLK_PAGEDOWN) { +// if (pressed) { +// if (! shifted) +// getIKBD()->SendKey(0x2a); // press and hold LShift +// getIKBD()->SendKey(0x50); // press keyDown +// } +// else { +// getIKBD()->SendKey(0xd0); // release keyDown +// if (! shifted) +// getIKBD()->SendKey(0xaa); // release LShift +// } +// send2Atari = false; +// } // send all pressed keys to IKBD From c1068588f821db94d9e06d0847993fbe4c546e32 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 17 Jul 2023 18:29:50 +0200 Subject: [PATCH 6/7] Introduce 'keymap' configuration file This file allows defining a custom scancode mapping from SDL to Atari scancodes. --- src/input.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/src/input.cpp b/src/input.cpp index d608209b0..383bec567 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -196,6 +196,102 @@ static SDL_Cursor *init_empty_cursor() return SDL_CreateCursor(data, mask, 16, 16, 0, 0); } +static bool hasKeymap = false; +static int scancodeSDL2Atari[SDL_NUM_SCANCODES]; + +static bool init_keymap() { + char keymap_filename[512]; + getConfFilename(ARANYMKEYMAP, keymap_filename, sizeof(keymap_filename)); + + memset(scancodeSDL2Atari, 0, sizeof(scancodeSDL2Atari)); + + struct stat buf; + if (stat(keymap_filename, &buf) == -1) { + infoprint("Config file '%s' not found.", keymap_filename); + return false; + } + + FILE *in = fopen(keymap_filename, "rw"); + if (!in) { + bug("Unable to open file '%s': %s", keymap_filename, strerror(errno)); + return false; + } + + char line[1024]; + int numMappings = 0; + while (!feof(in)) { + if (!fgets(line, sizeof(line), in)) { + break; + } + + char *start = NULL; + char *ptr = line; + int len = 0; + while(*ptr) { + if (!start) { + // find start of data: skip leading whitespaces + if (*ptr == ' ' || *ptr == '\t') { + ptr++; + } else { + start = ptr; + } + } else { + // reached end of relevant data + if (*ptr == ';' || *ptr == '#' || *ptr == '\r' || *ptr == '\n') { + // trim trailing whitespaces + do { + *ptr = 0; + if (ptr > start && (ptr[-1] == ' ' || ptr[-1] == '\t')) { + --ptr; + } + } while(*ptr); + + len = ptr-start; + } else { + ptr++; + } + } + } + + D(bug("Extracted line: <%s> with length %d\n", start, len)); + int sdl_scancode = 0, atari_scancode = 0; + + // read hex or decimal value + ptr = start; + if (ptr[0] == '0' && ptr[1] == 'x') { + sscanf(ptr+2, "%x", &sdl_scancode); + } else { + sscanf(ptr, "%d", &sdl_scancode); + } + + // skip to comma, then skip whitespaces + do { + ptr++; + } while(*ptr && *ptr != ','); + do { + ptr++; + } while(*ptr && (*ptr == ' ' || *ptr == '\t')); + + // read hex or decimal value + if (ptr[0] == '0' && ptr[1] == 'x') { + sscanf(ptr+2, "%x", &atari_scancode); + } else { + sscanf(ptr, "%d", &atari_scancode); + } + + // Process a valid mapping + D(bug("Read %d %d\n", sdl_scancode, atari_scancode)) ; + if (sdl_scancode > 0 && atari_scancode > 0 && sdl_scancode < SDL_NUM_SCANCODES) { + scancodeSDL2Atari[sdl_scancode] = atari_scancode; + numMappings++; + } + } + + printf("Read %d scancode mappings from '%s'\n", numMappings, keymap_filename); + + return numMappings > 0; +} + #if SDL_VERSION_ATLEAST(2, 0, 0) static int SDLCALL event_filter(void * /* userdata */, SDL_Event *event) @@ -247,6 +343,7 @@ void InputReset() // FIXME: add? InputInit(); // FIXME: how??? capslockState (detect) host->video->CapslockState((SDL_GetModState() & KMOD_CAPS) != 0); + hasKeymap = init_keymap(); } void InputExit() @@ -467,6 +564,14 @@ static int keysymToAtari(SDL_Keysym *keysym) { unsigned int scancode = 0; + if (hasKeymap) { + scancode = scancodeSDL2Atari[(unsigned int) keysym->scancode]; + if (scancode > 0) { + bug("Custom scancode mapping applied"); + return scancode; + } + } + #if SDL_VERSION_ATLEAST(2, 0, 0) switch( (unsigned int) keysym->scancode) { @@ -618,7 +723,7 @@ static int keysymToAtari(SDL_Keysym *keysym) case SDLK_DELETE: scancode = 0x53; break; /* Delete */ case SDLK_NUMLOCKCLEAR: scancode = 0x63; break; - + case SDLK_BACKQUOTE: case SDLK_LESS: scancode = 0x60; break; /* a '<>' key next to short left Shift */ From a969b260ddfdad053a038e08a91b63726e225327 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 28 Feb 2025 22:08:49 +0100 Subject: [PATCH 7/7] Adjust init_keymap to process SDL scancode names --- src/input.cpp | 58 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 7a750dcfc..9e45aeea5 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -255,33 +255,57 @@ static bool init_keymap() { } D(bug("Extracted line: <%s> with length %d\n", start, len)); - int sdl_scancode = 0, atari_scancode = 0; - - // read hex or decimal value - ptr = start; - if (ptr[0] == '0' && ptr[1] == 'x') { - sscanf(ptr+2, "%x", &sdl_scancode); - } else { - sscanf(ptr, "%d", &sdl_scancode); + if (len == 0) { + continue; } - // skip to comma, then skip whitespaces + // identify string start of SDL and Atari key string + char *sdl_key_str = start, *atari_key_str = NULL; + + // skip to comma (terminating current string at whitespaces) + ptr = start; do { + if (*ptr == ' ' || *ptr == '\t') { + *ptr = 0; + } ptr++; } while(*ptr && *ptr != ','); + if (*ptr == ',') { + *ptr = 0; + } + // skip whitespaces after comma do { ptr++; } while(*ptr && (*ptr == ' ' || *ptr == '\t')); + atari_key_str = ptr; + + + // Now convert the strings to scan codes + int sdl_scancode = 0, atari_scancode = 0; + // read hex or decimal value - if (ptr[0] == '0' && ptr[1] == 'x') { - sscanf(ptr+2, "%x", &atari_scancode); + if (sdl_key_str[0] == '0' && sdl_key_str[1] == 'x') { + sscanf(sdl_key_str+2, "%x", &sdl_scancode); + } else if (sdl_key_str[0] >= '0' && sdl_key_str[0] <= '9') { + sscanf(sdl_key_str, "%d", &sdl_scancode); } else { - sscanf(ptr, "%d", &atari_scancode); + // Try to parse scan code from SDL scancode name + sdl_scancode = SDL_GetScancodeFromName(sdl_key_str); + if (sdl_scancode == SDL_SCANCODE_UNKNOWN) { + bug("Error processing textual scancode '%s': %s", sdl_key_str, SDL_GetError()); + } + } + + // read hex or decimal value + if (atari_key_str[0] == '0' && atari_key_str[1] == 'x') { + sscanf(atari_key_str+2, "%x", &atari_scancode); + } else { + sscanf(atari_key_str, "%d", &atari_scancode); } // Process a valid mapping - D(bug("Read %d %d\n", sdl_scancode, atari_scancode)) ; + bug(" keymap SDL %d (%s) to Atari %d (%s)", sdl_scancode, sdl_key_str, atari_scancode, atari_key_str); if (sdl_scancode > 0 && atari_scancode > 0 && sdl_scancode < SDL_NUM_SCANCODES) { scancodeSDL2Atari[sdl_scancode] = atari_scancode; numMappings++; @@ -682,12 +706,8 @@ static int keysymToAtari(SDL_Keysym *keysym) } if (scancode != 0) { - keysym->scancode = SDL_Scancode(scancode); return scancode; } - bug("Unmapped keycode: 0x%x, scancode %d (0x%x), keysym '%s'", - keysym->sym, keysym->scancode, keysym->scancode, SDL_GetKeyName(keysym->sym)); - return scancode; #else switch ((unsigned int) keysym->sym) { @@ -760,9 +780,9 @@ static int keysymToAtari(SDL_Keysym *keysym) #endif if (scancode == 0 && keysym->scancode != 0) - bug("keycode: %d (0x%x), scancode %d (0x%x), keysym '%s' is not mapped", + bug("keycode: %d (0x%x), scancode %d (0x%x '%s'), keysym '%s' is not mapped", keysym->sym, keysym->sym, - keysym->scancode, keysym->scancode, + keysym->scancode, keysym->scancode, SDL_GetScancodeName(keysym->scancode), SDL_GetKeyName(keysym->sym)); keysym->scancode = SDL_Scancode(scancode); return scancode;