From 5efd652d62492e06cf5c39d604f8dd218d089da6 Mon Sep 17 00:00:00 2001 From: Flyman <1308646+zhangmx@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:29:53 +0800 Subject: [PATCH 1/2] misc: Fixed windows compile error by -Wcast-qual.Correct pointer type for probe_skip and improve memory management in FTDI functions --- src/platforms/hosted/bmp_libusb.c | 6 +++--- src/platforms/hosted/windows/ftdi.c | 21 ++++++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/platforms/hosted/bmp_libusb.c b/src/platforms/hosted/bmp_libusb.c index c0d060cbb13..df424ea308f 100644 --- a/src/platforms/hosted/bmp_libusb.c +++ b/src/platforms/hosted/bmp_libusb.c @@ -264,7 +264,7 @@ static probe_info_s *process_ftdi_probe(void) } probe_info_s *probe_list = NULL; - const char *probe_skip = NULL; + char *probe_skip = NULL; bool use_serial = true; /* Device list is loaded, iterate over the found probes */ for (size_t index = 0; index < ftdi_dev_count; ++index) { @@ -285,7 +285,7 @@ static probe_info_s *process_ftdi_probe(void) if (probe_skip) { // Clean up any previous serial number to skip use_serial = true; - free((void *)probe_skip); + free(probe_skip); probe_skip = NULL; } @@ -323,7 +323,7 @@ static probe_info_s *process_ftdi_probe(void) } } if (probe_skip) - free((void *)probe_skip); + free(probe_skip); free(dev_info); return probe_list; } diff --git a/src/platforms/hosted/windows/ftdi.c b/src/platforms/hosted/windows/ftdi.c index 5254c3411df..60e5bae892a 100644 --- a/src/platforms/hosted/windows/ftdi.c +++ b/src/platforms/hosted/windows/ftdi.c @@ -149,11 +149,22 @@ int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, const int size int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size) { - (void)ftdi; - DWORD bytes_written; - if (FT_Write(ftdi_handle, (unsigned char *)buf, size, &bytes_written) != FT_OK) - return 0; - return bytes_written; + (void)ftdi; + DWORD bytes_written; + + unsigned char *temp_buf = (unsigned char *)malloc(size); + if (temp_buf == NULL) { + return 0; + } + memcpy(temp_buf, buf, size); + + if (FT_Write(ftdi_handle, temp_buf, size, &bytes_written) != FT_OK) { + free(temp_buf); + return 0; + } + + free(temp_buf); + return bytes_written; } int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) From 23fdf08bbcbbd1bce4b58c9dfad9641c91f0b309 Mon Sep 17 00:00:00 2001 From: Flyman <1308646+zhangmx@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:59:45 +0800 Subject: [PATCH 2/2] misc: Fixed the cast qualification warnings --- src/platforms/hosted/bmp_libusb.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/platforms/hosted/bmp_libusb.c b/src/platforms/hosted/bmp_libusb.c index df424ea308f..60b4274f68d 100644 --- a/src/platforms/hosted/bmp_libusb.c +++ b/src/platforms/hosted/bmp_libusb.c @@ -245,6 +245,8 @@ void stlinkv2_read_serial(libusb_device_descriptor_s *device_descriptor, libusb_ } #if defined(_WIN32) || defined(__CYGWIN__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-qual" static probe_info_s *process_ftdi_probe(void) { DWORD ftdi_dev_count = 0; @@ -264,7 +266,7 @@ static probe_info_s *process_ftdi_probe(void) } probe_info_s *probe_list = NULL; - char *probe_skip = NULL; + const char *probe_skip = NULL; bool use_serial = true; /* Device list is loaded, iterate over the found probes */ for (size_t index = 0; index < ftdi_dev_count; ++index) { @@ -285,7 +287,7 @@ static probe_info_s *process_ftdi_probe(void) if (probe_skip) { // Clean up any previous serial number to skip use_serial = true; - free(probe_skip); + free((char*)probe_skip); probe_skip = NULL; } @@ -322,11 +324,14 @@ static probe_info_s *process_ftdi_probe(void) free(product); } } - if (probe_skip) - free(probe_skip); + if (probe_skip) { + free((char*)probe_skip); + probe_skip = NULL; + } free(dev_info); return probe_list; } +#pragma GCC diagnostic pop #endif void orbtrace_read_version(libusb_device *device, libusb_device_handle *handle, char *version, size_t buffer_size)