only: fix potential IOStream leak: Method `DfuHelper.sendDfuRebootCommand` shouldn't be responsible for closing a stream, because there are cases, when overloaded `DfuHelper.sendDfuRebootCommand` method doesn't call its namesake (that was closing the stream)

This commit is contained in:
kifir 2024-08-13 15:13:17 +03:00 committed by rusefillc
parent c7fefcef25
commit 07fe982aa5
2 changed files with 21 additions and 22 deletions

View File

@ -5,7 +5,6 @@ import com.rusefi.core.RusEfiSignature;
import com.rusefi.core.SignatureHelper; import com.rusefi.core.SignatureHelper;
import com.rusefi.core.io.BundleUtil; import com.rusefi.core.io.BundleUtil;
import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.ui.StatusConsumer;
import javax.swing.*; import javax.swing.*;
import java.io.IOException; import java.io.IOException;
@ -18,11 +17,10 @@ public class DfuHelper {
private static final Logging log = getLogging(DfuHelper.class); private static final Logging log = getLogging(DfuHelper.class);
private static final String PREFIX = "rusefi_bundle"; private static final String PREFIX = "rusefi_bundle";
public static void sendDfuRebootCommand(IoStream stream, UpdateOperationCallbacks callbacks, String cmd) { private static void sendDfuRebootCommand(IoStream stream, UpdateOperationCallbacks callbacks, String cmd) {
byte[] command = BinaryProtocol.getTextCommandBytes(cmd); byte[] command = BinaryProtocol.getTextCommandBytes(cmd);
try { try {
stream.sendPacket(command); stream.sendPacket(command);
stream.close();
callbacks.logLine(String.format("Reboot command [%s] sent into %s!\n", cmd, stream)); callbacks.logLine(String.format("Reboot command [%s] sent into %s!\n", cmd, stream));
} catch (IOException e) { } catch (IOException e) {
callbacks.logLine("Error " + e); callbacks.logLine("Error " + e);

View File

@ -75,27 +75,28 @@ public class DfuFlasher {
AtomicBoolean isSignatureValidated = new AtomicBoolean(true); AtomicBoolean isSignatureValidated = new AtomicBoolean(true);
if (!PortDetector.isAutoPort(port)) { if (!PortDetector.isAutoPort(port)) {
callbacks.logLine("Using selected " + port + "\n"); callbacks.logLine("Using selected " + port + "\n");
IoStream stream = BufferedSerialIoStream.openPort(port); try (final IoStream stream = BufferedSerialIoStream.openPort(port)) {
AtomicReference<String> signature = new AtomicReference<>(); AtomicReference<String> signature = new AtomicReference<>();
SerialAutoChecker.checkResponse(stream, callbackContext -> { SerialAutoChecker.checkResponse(stream, callbackContext -> {
signature.set(callbackContext.getSignature()); signature.set(callbackContext.getSignature());
return null; return null;
}); });
if (signature.get() == null) { if (signature.get() == null) {
callbacks.appendLine(""); callbacks.appendLine("");
callbacks.appendLine(""); callbacks.appendLine("");
callbacks.appendLine(""); callbacks.appendLine("");
callbacks.appendLine("Make sure TUNERSTUDIO IS DISCONNECTED FROM ECU"); callbacks.appendLine("Make sure TUNERSTUDIO IS DISCONNECTED FROM ECU");
callbacks.appendLine(""); callbacks.appendLine("");
callbacks.appendLine(""); callbacks.appendLine("");
callbacks.appendLine(""); callbacks.appendLine("");
callbacks.appendLine("*** ERROR *** rusEFI has not responded on selected " + port + "\n" + callbacks.appendLine("*** ERROR *** rusEFI has not responded on selected " + port + "\n" +
"Maybe try automatic serial port detection?"); "Maybe try automatic serial port detection?");
callbacks.error(); callbacks.error();
return null; return null;
}
boolean isSignatureValidatedLocal = DfuHelper.sendDfuRebootCommand(parent, signature.get(), stream, callbacks, command);
isSignatureValidated.set(isSignatureValidatedLocal);
} }
boolean isSignatureValidatedLocal = DfuHelper.sendDfuRebootCommand(parent, signature.get(), stream, callbacks, command);
isSignatureValidated.set(isSignatureValidatedLocal);
} else { } else {
callbacks.logLine("Auto-detecting port...\n"); callbacks.logLine("Auto-detecting port...\n");
// instead of opening the just-detected port we execute the command using the same stream we used to discover port // instead of opening the just-detected port we execute the command using the same stream we used to discover port