Skip to content

Commit

Permalink
sysfs.c: fix an theoretical issue with snprintf()
Browse files Browse the repository at this point in the history
Again, code scanners really don't like C string functions, and warn
about the potential for a buffer to possibly be too big for a snprintf()
call as the checking wasn't quite correct.  Fix the check for a buffer
overflow up better and handle any potential issues that checking tools
could come up with.

No real functional change as the sysfs path should be just fine.  And
really, this is all just userspace code...

Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
gregkh committed Oct 22, 2024
1 parent bb1e4b7 commit 59f1fe1
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ int get_sysfs_name(char *buf, size_t size, libusb_device *dev)
}

len += snprintf(buf, size, "%d-", bnum);
for (int i = 0; i < num_pnums; i++)
len += snprintf(buf + len, size - len, i ? ".%d" : "%d", pnums[i]);
for (int i = 0; i < num_pnums; i++) {
int n = snprintf(buf + len, size - len, i ? ".%d" : "%d", pnums[i]);
if ((n < 0) || (n >= (int)(size - len)))
break;
len += n;
}

return len;
}
Expand Down

0 comments on commit 59f1fe1

Please sign in to comment.