Skip to content

Commit a8a307d

Browse files
committed
Fix inexplicable printf crash bug on 32-bit Windows
1 parent 0d18669 commit a8a307d

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

src/main/c/Windows/Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ LINK_X64 := x86_64-pc-win32-msvc-ld
88
LINK_ARM := arm-pc-win32-msvc-ld
99
LINK_ARM64 := aarch64-pc-win32-msvc-ld
1010
CFLAGS := -Os -flto
11+
CFLAGS_X86 := -O0
1112
LDFLAGS := -flto -Wl,/DLL
13+
LDFLAGS_X86 := -Wl,/DLL
1214
JDK_HOME := $(shell echo "`dirname $$(dirname $$(readlink -f $$(which javac)))`")
1315
INCLUDES := -I"$(JDK_HOME)/include" -Iwin32
1416
LIBRARIES := -lAdvAPI32 -lSetupAPI -lshell32
@@ -79,7 +81,7 @@ $(JAVA_CLASS_DIR) :
7981

8082
# Build rules for all libraries
8183
$(BUILD_DIR)/x86/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSx86)
82-
$(LINK_X86) $(LDFLAGS) -o $@ $(OBJECTSx86) $(LIBRARIES)
84+
$(LINK_X86) $(LDFLAGS_X86) -o $@ $(OBJECTSx86) $(LIBRARIES)
8385
$(BUILD_DIR)/x86_64/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSx64)
8486
$(LINK_X64) $(LDFLAGS) -o $@ $(OBJECTSx64) $(LIBRARIES)
8587
$(BUILD_DIR)/armv7/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarm)
@@ -89,7 +91,7 @@ $(BUILD_DIR)/aarch64/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarm64)
8991

9092
# Suffix rules to get from *.c -> *.o
9193
$(BUILD_DIR)/x86/%.o : %.c
92-
$(COMPILE_X86) $(INCLUDES) $(CFLAGS) -c $< -o $@
94+
$(COMPILE_X86) $(INCLUDES) $(CFLAGS_X86) -c $< -o $@
9395
$(BUILD_DIR)/x86_64/%.o : %.c
9496
$(COMPILE_X64) $(INCLUDES) $(CFLAGS) -c $< -o $@
9597
$(BUILD_DIR)/armv7/%.o : %.c

src/main/c/Windows/SerialPort_Windows.c

+7-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SerialPort_Windows.c
33
*
44
* Created on: Feb 25, 2012
5-
* Last Updated on: Apr 22, 2024
5+
* Last Updated on: May 15, 2024
66
* Author: Will Hedgecock
77
*
88
* Copyright (C) 2012-2024 Fazecast, Inc.
@@ -268,12 +268,13 @@ static void enumeratePorts(JNIEnv *env)
268268
}
269269

270270
// Fetch the physical location for this device
271+
DWORD locationLength = 0;
271272
wchar_t *locationString = NULL;
272-
DWORD locationLength = 0, busNumber = -1, hubNumber = -1, portNumber = -1;
273+
unsigned long busNumber = 0, hubNumber = 0, portNumber = 0;
273274
if (!SetupDiGetDeviceRegistryPropertyW(devList, &devInfoData, SPDRP_BUSNUMBER, NULL, (BYTE*)&busNumber, sizeof(busNumber), NULL))
274-
busNumber = -1;
275+
busNumber = 0;
275276
if (!SetupDiGetDeviceRegistryPropertyW(devList, &devInfoData, SPDRP_ADDRESS, NULL, (BYTE*)&portNumber, sizeof(portNumber), NULL))
276-
portNumber = -1;
277+
portNumber = 0;
277278
SetupDiGetDeviceRegistryPropertyW(devList, &devInfoData, SPDRP_LOCATION_INFORMATION, NULL, NULL, 0, &locationLength);
278279
if (locationLength && (locationLength < 256))
279280
{
@@ -336,15 +337,9 @@ static void enumeratePorts(JNIEnv *env)
336337
if (locationString)
337338
free(locationString);
338339
}
339-
if (busNumber == -1)
340-
busNumber = 0;
341-
if (hubNumber == -1)
342-
hubNumber = 0;
343-
if (portNumber == -1)
344-
portNumber = 0;
345-
locationString = (wchar_t*)malloc(16*sizeof(wchar_t));
340+
locationString = (wchar_t*)malloc(32*sizeof(wchar_t));
346341
if (locationString)
347-
_snwprintf_s(locationString, 16, 16, L"%d-%d.%d", busNumber, hubNumber, portNumber);
342+
_snwprintf_s(locationString, 32, _TRUNCATE, L"%lu-%lu.%lu", busNumber, hubNumber, portNumber);
348343
else
349344
{
350345
free(comPort);

0 commit comments

Comments
 (0)