Fix resource leak in SysfsUsbSerialScanner (#3513)

Signed-off-by: Jan N. Klug <github@klug.nrw>
pull/3519/head
J-N-K 2023-04-01 11:32:53 +02:00 committed by GitHub
parent 74052e7bcd
commit 8aa9e11342
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 10 deletions

View File

@ -154,16 +154,18 @@ public class SysfsUsbSerialScanner implements UsbSerialScanner {
Path devSerialDir = Path.of(DEV_SERIAL_BY_ID_DIRECTORY);
if (exists(devSerialDir) && isDirectory(devSerialDir) && isReadable(devSerialDir)) {
// browse serial/by-id directory :
for (Path devLinkPath : newDirectoryStream(devSerialDir)) {
if (Files.isSymbolicLink(devLinkPath)) {
Path devicePath = getRealDevicePath(devLinkPath);
if (devicePath != null) {
String serialPortName = devicePath.getFileName().toString();
// get the corresponding real sysinfo special dir :
Path sysfsDevicePath = getRealDevicePath(
Paths.get(sysfsTtyDevicesDirectory).resolve(serialPortName));
if (sysfsDevicePath != null && isReadable(devicePath) && isWritable(devicePath)) {
result.add(new SerialPortInfo(devLinkPath, sysfsDevicePath));
try (DirectoryStream<Path> directoryStream = newDirectoryStream(devSerialDir)) {
for (Path devLinkPath : directoryStream) {
if (Files.isSymbolicLink(devLinkPath)) {
Path devicePath = getRealDevicePath(devLinkPath);
if (devicePath != null) {
String serialPortName = devicePath.getFileName().toString();
// get the corresponding real sysinfo special dir :
Path sysfsDevicePath = getRealDevicePath(
Paths.get(sysfsTtyDevicesDirectory).resolve(serialPortName));
if (sysfsDevicePath != null && isReadable(devicePath) && isWritable(devicePath)) {
result.add(new SerialPortInfo(devLinkPath, sysfsDevicePath));
}
}
}
}