Skip to content

windows: deduplicate bounded string getters - #865

Merged
Youw merged 1 commit into
libusb:masterfrom
ebraminio:dedup-string-copy
Dec 2, 2026
Merged

Youw merged 1 commit into
libusb:masterfrom
ebraminio:dedup-string-copy

Conversation

@ebraminio

@ebraminio ebraminio commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Replace the duplicated Windows string getters and HIDAPI_WCSNCPY with one bounded copy helper.

There is one intended behavior change with this however, a NULL individual device-info string now returns an empty string with success, instead of relying on undefined wcsncpy_s/lstrcpynW behavior. NULL device-info can happen while *dup function or other path and currently isn't guarded for failure indifferently in CRT or no-CRT, ideally later an alloc failure in device object creation should be turned into failed object creation which is out of scope for this change. NULL device_info still returns -1, and truncation behavior is unchanged.

Tested before/after against a connected DualSense for normal and no-CRT builds. Return codes and output strings were byte-identical across maxlen values 0, 1, 2, 4, 8, 16, 32, 64, 128, and 256.

The code that is used for testing
#include "hidapi/hidapi.h"
#include <windows.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>

static void print_result(const char *name, int rc, const wchar_t *buf, size_t cap)
{
	char utf8[2048];
	int n = 0;
	if (cap > 0)
		n = WideCharToMultiByte(CP_UTF8, 0, buf, -1, utf8, (int)sizeof(utf8), NULL, NULL);
	if (n > 0)
		printf("%s maxlen=%u rc=%d len=%u text=[%s]\n", name, (unsigned)cap, rc,
			(unsigned)wcslen(buf), utf8);
	else
		printf("%s maxlen=%u rc=%d text=<unreadable>\n", name, (unsigned)cap, rc);
}

static void test_device(const char *tag, hid_device *dev)
{
	static const size_t caps[] = { 0, 1, 2, 4, 8, 16, 32, 64, 128, 256 };
	wchar_t buf[512];
	size_t i;

	printf("==== %s ====\n", tag);
	for (i = 0; i < sizeof(caps) / sizeof(caps[0]); ++i) {
		size_t cap = caps[i];
		int rc;
		memset(buf, 0xCD, sizeof(buf));
		rc = hid_get_manufacturer_string(dev, cap ? buf : NULL, cap);
		print_result("manufacturer", rc, buf, cap);
		memset(buf, 0xCD, sizeof(buf));
		rc = hid_get_product_string(dev, cap ? buf : NULL, cap);
		print_result("product", rc, buf, cap);
		memset(buf, 0xCD, sizeof(buf));
		rc = hid_get_serial_number_string(dev, cap ? buf : NULL, cap);
		print_result("serial", rc, buf, cap);
	}
}

int main(void)
{
	struct hid_device_info *list, *cur, *pick = NULL;
	hid_device *dev = NULL;

	if (hid_init() != 0) {
		printf("hid_init failed\n");
		return 2;
	}
	list = hid_enumerate(0, 0);
	for (cur = list; cur; cur = cur->next) {
		printf("device vendor=%04x product=%04x path=%s\n",
			cur->vendor_id, cur->product_id, cur->path ? cur->path : "(null)");
		if (!pick && cur->vendor_id == 0x054C)
			pick = cur;
	}
	if (!pick) {
		printf("DualSense not found\n");
		hid_free_enumeration(list);
		hid_exit();
		return 3;
	}
	dev = hid_open_path(pick->path);
	if (!dev) {
		printf("hid_open_path failed\n");
		hid_free_enumeration(list);
		hid_exit();
		return 4;
	}
	test_device("DualSense", dev);
	hid_close(dev);
	hid_free_enumeration(list);
	hid_exit();
	return 0;
}

Replace HIDAPI_WCSNCPY and the three repeated getter implementations with
one bounded copy helper.

The old wcsncpy_s/lstrcpynW paths did not define behavior for a NULL
individual string. The shared helper returns an empty string in that case
while preserving the existing NULL device-info error. It copies at most
maxlen - 1 WCHARs with HIDAPI_MEMCPY, always terminates the result, and
keeps the existing truncation behavior.

Assisted-by: deepseek-harness:deepseek-flash

@Youw Youw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@Youw
Youw merged commit c580ede into libusb:master Dec 2, 2026
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants