From dc84ef00a2dd5810da3f5c61489f84c44c3fab53 Mon Sep 17 00:00:00 2001 From: ALTracer Date: Sun, 5 Nov 2023 06:56:52 +0300 Subject: [PATCH] hosted/jlink: Avoid stripping the copyright string located after a nul byte * According to Wireshark usbmon dumps, the firmware returns 112 bytes of version, including some copyright information after an effectively 0-terminator. * Use strchr() instead of index() for Windows reasons. * Make sure the version buffer contains at least one NULL * And avoid replacing the only NULL with LF, losing null-termination in the process --- src/platforms/hosted/jlink.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/platforms/hosted/jlink.c b/src/platforms/hosted/jlink.c index 72e8a54558f..3ff5cb864c1 100644 --- a/src/platforms/hosted/jlink.c +++ b/src/platforms/hosted/jlink.c @@ -295,9 +295,16 @@ static bool jlink_get_version(void) if (version_length > sizeof(jlink.fw_version)) return false; - /* Read vesion string directly into jlink.version */ + /* Read version string directly into jlink.version */ bmda_usb_transfer(bmda_probe_info.usb_link, NULL, 0, jlink.fw_version, version_length, JLINK_USB_TIMEOUT); - jlink.fw_version[version_length - 1U] = '\0'; /* Ensure null termination */ + /* Ensure null termination */ + char *const null_termination = jlink.fw_version + version_length - 1U; + *null_termination = '\0'; + + /* Replace NULL separating version and copyright string, if it exists */ + char *const null_separator = strchr(jlink.fw_version, '\0'); + if (null_separator != NULL && null_separator != null_termination) + *null_separator = '\n'; DEBUG_INFO("Firmware version: %s\n", jlink.fw_version);