From a8a307deed68bca2dda3048c1ecc5481eb5783fb Mon Sep 17 00:00:00 2001 From: Will Hedgecock Date: Wed, 15 May 2024 18:30:29 -0500 Subject: [PATCH] Fix inexplicable printf crash bug on 32-bit Windows --- src/main/c/Windows/Makefile | 6 ++++-- src/main/c/Windows/SerialPort_Windows.c | 19 +++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/c/Windows/Makefile b/src/main/c/Windows/Makefile index c2ba6a3b..5d3f7c9b 100644 --- a/src/main/c/Windows/Makefile +++ b/src/main/c/Windows/Makefile @@ -8,7 +8,9 @@ LINK_X64 := x86_64-pc-win32-msvc-ld LINK_ARM := arm-pc-win32-msvc-ld LINK_ARM64 := aarch64-pc-win32-msvc-ld CFLAGS := -Os -flto +CFLAGS_X86 := -O0 LDFLAGS := -flto -Wl,/DLL +LDFLAGS_X86 := -Wl,/DLL JDK_HOME := $(shell echo "`dirname $$(dirname $$(readlink -f $$(which javac)))`") INCLUDES := -I"$(JDK_HOME)/include" -Iwin32 LIBRARIES := -lAdvAPI32 -lSetupAPI -lshell32 @@ -79,7 +81,7 @@ $(JAVA_CLASS_DIR) : # Build rules for all libraries $(BUILD_DIR)/x86/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSx86) - $(LINK_X86) $(LDFLAGS) -o $@ $(OBJECTSx86) $(LIBRARIES) + $(LINK_X86) $(LDFLAGS_X86) -o $@ $(OBJECTSx86) $(LIBRARIES) $(BUILD_DIR)/x86_64/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSx64) $(LINK_X64) $(LDFLAGS) -o $@ $(OBJECTSx64) $(LIBRARIES) $(BUILD_DIR)/armv7/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarm) @@ -89,7 +91,7 @@ $(BUILD_DIR)/aarch64/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarm64) # Suffix rules to get from *.c -> *.o $(BUILD_DIR)/x86/%.o : %.c - $(COMPILE_X86) $(INCLUDES) $(CFLAGS) -c $< -o $@ + $(COMPILE_X86) $(INCLUDES) $(CFLAGS_X86) -c $< -o $@ $(BUILD_DIR)/x86_64/%.o : %.c $(COMPILE_X64) $(INCLUDES) $(CFLAGS) -c $< -o $@ $(BUILD_DIR)/armv7/%.o : %.c diff --git a/src/main/c/Windows/SerialPort_Windows.c b/src/main/c/Windows/SerialPort_Windows.c index 7e43b9a2..606b8c30 100644 --- a/src/main/c/Windows/SerialPort_Windows.c +++ b/src/main/c/Windows/SerialPort_Windows.c @@ -2,7 +2,7 @@ * SerialPort_Windows.c * * Created on: Feb 25, 2012 - * Last Updated on: Apr 22, 2024 + * Last Updated on: May 15, 2024 * Author: Will Hedgecock * * Copyright (C) 2012-2024 Fazecast, Inc. @@ -268,12 +268,13 @@ static void enumeratePorts(JNIEnv *env) } // Fetch the physical location for this device + DWORD locationLength = 0; wchar_t *locationString = NULL; - DWORD locationLength = 0, busNumber = -1, hubNumber = -1, portNumber = -1; + unsigned long busNumber = 0, hubNumber = 0, portNumber = 0; if (!SetupDiGetDeviceRegistryPropertyW(devList, &devInfoData, SPDRP_BUSNUMBER, NULL, (BYTE*)&busNumber, sizeof(busNumber), NULL)) - busNumber = -1; + busNumber = 0; if (!SetupDiGetDeviceRegistryPropertyW(devList, &devInfoData, SPDRP_ADDRESS, NULL, (BYTE*)&portNumber, sizeof(portNumber), NULL)) - portNumber = -1; + portNumber = 0; SetupDiGetDeviceRegistryPropertyW(devList, &devInfoData, SPDRP_LOCATION_INFORMATION, NULL, NULL, 0, &locationLength); if (locationLength && (locationLength < 256)) { @@ -336,15 +337,9 @@ static void enumeratePorts(JNIEnv *env) if (locationString) free(locationString); } - if (busNumber == -1) - busNumber = 0; - if (hubNumber == -1) - hubNumber = 0; - if (portNumber == -1) - portNumber = 0; - locationString = (wchar_t*)malloc(16*sizeof(wchar_t)); + locationString = (wchar_t*)malloc(32*sizeof(wchar_t)); if (locationString) - _snwprintf_s(locationString, 16, 16, L"%d-%d.%d", busNumber, hubNumber, portNumber); + _snwprintf_s(locationString, 32, _TRUNCATE, L"%lu-%lu.%lu", busNumber, hubNumber, portNumber); else { free(comPort);