tools: fix swapped fread arguments causing DMI modalias to always be "unknown"

fread(buf, sizeof(buf), 1, dmi) reads one block of 2048 bytes,
returning the number of complete blocks (0 or 1). Since DMI modalias
files are always shorter than 2048 bytes, fread returns 0 even when
data was successfully read into buf. The 'if (n > 0)' check then
always fails and the DMI string stays as "unknown".

Swap the size and nmemb arguments so fread returns the number of
bytes read instead.

Fixes: 0ecd08c134 ("tools: use __attribute__(cleanup)")

Co-Authored-by: Claude Code <noreply@anthropic.com>
(cherry picked from commit c8c1c07a2a)

Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1468>
This commit is contained in:
Peter Hutterer 2026-03-11 20:53:32 +10:00
parent 5ce952eb25
commit 5be0a16dd0

View file

@ -1404,7 +1404,7 @@ print_system_header(FILE *fp)
_autofree_ char *dmistr = strdup("unknown"); //
if (dmi) {
char buf[2048] = "unknown";
size_t n = fread(buf, sizeof(buf), 1, dmi); // NOLINT: unix.Stream
size_t n = fread(buf, 1, sizeof(buf), dmi); // NOLINT: unix.Stream
if (n > 0) {
free(dmistr);
dmistr = strndup(buf, n - 1);