linux_usbfs: Clean up inconsistencies and optimize memory usage

The formatting and coding style varied across the whole file. Adopt the
following consistent style:

  - Align function arguments to the opening parenthesis
  - Do not check for NULL before calling free()
  - Reduce indentation where possible in loops by continuing in the
    success case
  - Remove space between function name and opening parenthesis
  - Remove pointless pointer casts from void *
  - Replace comparisons with NULL or 0 by a negation operator
  - When comparing against rvalues, place the rvalue on the right side
    of the expression
  - Where possible, have the debug message string on the same line as
    the usbi_* call. This makes it easier to grep for specific strings.

Also update the definitions in linux_usbfs.h to exactly match that of
the kernel and remove definitions that are not needed.

A number of functions declared stack buffers of size PATH_MAX. This is
generally 4K, which is very much overkill for a lot of the strings and
is not friendly for embedded environments. Replace many of these buffers
with reasonably-sized ones, in many cases using exactly the size needed.

When reading the descriptors during device enumeration, we were starting
with a 1K buffer and doubling as needed. The vast majority of devices
will not have a large set of descriptors, so change the allocation logic
to grow the buffer in steps of 256 bytes.

Introduce a new parsing function for reading sysfs attributes. Using the
fdopen() function to use fscanf() results in excessive memory
allocation, one for the FILE object and another for the buffer into
which the C library will read the data. The sysfs attributes of interest
are generally just a few characters, so use a small stack buffer and
some rigorous parsing to read these attributes. This also consolidates
error checking (e.g. negative values or larger than expected values).

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
3 files changed
tree: 8b3a498944772040058f52d5292d7820eeeb150b
  1. .private/
  2. android/
  3. doc/
  4. examples/
  5. libusb/
  6. msvc/
  7. tests/
  8. Xcode/
  9. .gitattributes
  10. .gitignore
  11. .travis.yml
  12. appveyor.yml
  13. appveyor_cygwin.bat
  14. appveyor_minGW.bat
  15. AUTHORS
  16. autogen.sh
  17. bootstrap.sh
  18. Brewfile
  19. ChangeLog
  20. configure.ac
  21. COPYING
  22. INSTALL_WIN.txt
  23. libusb-1.0.pc.in
  24. Makefile.am
  25. NEWS
  26. PORTING
  27. README.git
  28. README.md
  29. TODO
  30. travis-autogen.sh
README.md

libusb

Build Status Build status Coverity Scan Build Status

libusb is a library for USB device access from Linux, macOS, Windows, OpenBSD/NetBSD and Haiku userspace. It is written in C (Haiku backend in C++) and licensed under the GNU Lesser General Public License version 2.1 or, at your option, any later version (see COPYING).

libusb is abstracted internally in such a way that it can hopefully be ported to other operating systems. Please see the PORTING file for more information.

libusb homepage: http://libusb.info/

Developers will wish to consult the API documentation: http://api.libusb.info

Use the mailing list for questions, comments, etc: http://mailing-list.libusb.info

(Please use the mailing list rather than mailing developers directly)