Skip to content

Commit

Permalink
Fix inexplicable printf crash bug on 32-bit Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed May 15, 2024
1 parent 0d18669 commit a8a307d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/main/c/Windows/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
19 changes: 7 additions & 12 deletions src/main/c/Windows/SerialPort_Windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a8a307d

Please sign in to comment.