From e43366e1d4d0ff55fb60f066e82e0e185561ed29 Mon Sep 17 00:00:00 2001 From: Kondal Kolipaka Date: Mon, 29 Jul 2024 11:03:28 +0530 Subject: [PATCH 1/2] fix: close inputStream to avoid resource leaks --- .../core/util/ClangdConfigFileHandler.java | 62 ++++++++++++------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java index bb46e87d1..436102984 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java @@ -25,6 +25,7 @@ import com.espressif.idf.core.IDFConstants; import com.espressif.idf.core.IDFCorePlugin; import com.espressif.idf.core.ILSPConstants; +import com.espressif.idf.core.logging.Logger; /** * @author Kondal Kolipaka @@ -35,33 +36,50 @@ public class ClangdConfigFileHandler public void update(IProject project) throws CoreException, IOException { File file = getClangdConfigFile(project); + FileInputStream inputStream = null; - // Load existing clangd file - FileInputStream inputStream = new FileInputStream(file); - Yaml yaml = new Yaml(); - Object obj = yaml.load(inputStream); + try + { + // Load existing clangd file + inputStream = new FileInputStream(file); + Yaml yaml = new Yaml(); + Object obj = yaml.load(inputStream); - // Create new YAML structure if file is empty - Map data = createOrGetExistingYamlStructure(obj); + // Create new YAML structure if file is empty + Map data = createOrGetExistingYamlStructure(obj); - // Add or update CompileFlags section - Map compileFlags = (Map) data.get("CompileFlags"); //$NON-NLS-1$ - if (compileFlags == null) - { - compileFlags = new LinkedHashMap<>(); - data.put("CompileFlags", compileFlags); //$NON-NLS-1$ - } - updateCompileFlagsSection(compileFlags, project - .getPersistentProperty(new QualifiedName(IDFCorePlugin.PLUGIN_ID, IDFConstants.BUILD_DIR_PROPERTY))); + // Add or update CompileFlags section + Map compileFlags = (Map) data.get("CompileFlags"); //$NON-NLS-1$ + if (compileFlags == null) + { + compileFlags = new LinkedHashMap<>(); + data.put("CompileFlags", compileFlags); //$NON-NLS-1$ + } + updateCompileFlagsSection(compileFlags, project.getPersistentProperty( + new QualifiedName(IDFCorePlugin.PLUGIN_ID, IDFConstants.BUILD_DIR_PROPERTY))); - // Write updated clangd back to file - try (Writer writer = new FileWriter(file)) - { - yaml.dump(data, writer); - } - catch (IOException e) + // Write updated clangd back to file + try (Writer writer = new FileWriter(file)) + { + yaml.dump(data, writer); + } + catch (IOException e) + { + throw new IOException("Error writing .clangd file: " + e.getMessage(), e); //$NON-NLS-1$ + } + } finally { - throw new IOException("Error writing .clangd file: " + e.getMessage(), e); //$NON-NLS-1$ + if (inputStream != null) + { + try + { + inputStream.close(); + } + catch (IOException e) + { + Logger.log(e); + } + } } } From 7506a6236b798e5b6393d03d81796d606027c68d Mon Sep 17 00:00:00 2001 From: Kondal Kolipaka Date: Mon, 29 Jul 2024 12:50:59 +0530 Subject: [PATCH 2/2] fix: Address the PR comments --- .../core/util/ClangdConfigFileHandler.java | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java index 436102984..4a4b44c1f 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/ClangdConfigFileHandler.java @@ -9,6 +9,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; @@ -25,7 +26,6 @@ import com.espressif.idf.core.IDFConstants; import com.espressif.idf.core.IDFCorePlugin; import com.espressif.idf.core.ILSPConstants; -import com.espressif.idf.core.logging.Logger; /** * @author Kondal Kolipaka @@ -36,12 +36,10 @@ public class ClangdConfigFileHandler public void update(IProject project) throws CoreException, IOException { File file = getClangdConfigFile(project); - FileInputStream inputStream = null; - try + // Load existing clangd file + try (FileInputStream inputStream = new FileInputStream(file)) { - // Load existing clangd file - inputStream = new FileInputStream(file); Yaml yaml = new Yaml(); Object obj = yaml.load(inputStream); @@ -59,7 +57,7 @@ public void update(IProject project) throws CoreException, IOException new QualifiedName(IDFCorePlugin.PLUGIN_ID, IDFConstants.BUILD_DIR_PROPERTY))); // Write updated clangd back to file - try (Writer writer = new FileWriter(file)) + try (Writer writer = new FileWriter(file, StandardCharsets.UTF_8)) { yaml.dump(data, writer); } @@ -67,19 +65,10 @@ public void update(IProject project) throws CoreException, IOException { throw new IOException("Error writing .clangd file: " + e.getMessage(), e); //$NON-NLS-1$ } - } finally + } + catch (IOException e) { - if (inputStream != null) - { - try - { - inputStream.close(); - } - catch (IOException e) - { - Logger.log(e); - } - } + throw new IOException("Error reading .clangd file: " + e.getMessage(), e); //$NON-NLS-1$ } }