Skip to content

Commit

Permalink
fix: ensure clangd file exists before writing config attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kolipakakondal committed Apr 4, 2024
1 parent e1bf806 commit 432367f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.yaml.snakeyaml.Yaml;

import com.espressif.idf.core.logging.Logger;
Expand All @@ -24,42 +29,64 @@
public class ClangdConfigFileHandler
{
@SuppressWarnings("unchecked")
public void update(IProject project) throws FileNotFoundException
public void update(IProject project) throws CoreException, IOException
{
File file = getClangdConfigFile(project);

// Load existing clangd file
FileInputStream inputStream = new FileInputStream(file);

Check warning on line 37 in bundles/com.espressif.idf.lsp/src/com/espressif/idf/lsp/ClangdConfigFileHandler.java

View workflow job for this annotation

GitHub Actions / spotbugs

OBL_UNSATISFIED_OBLIGATION

com.espressif.idf.lsp.ClangdConfigFileHandler.update(IProject) may fail to clean up java.io.InputStream
Raw output
This method may fail to clean up (close, dispose of) a stream, database object, or other resource requiring an explicit cleanup operation.

In general, if a method opens a stream or other resource, the method should use a try/finally block to ensure that the stream or resource is cleaned up before the method returns.

This bug pattern is essentially the same as the OS_OPEN_STREAM and ODR_OPEN_DATABASE_RESOURCE bug patterns, but is based on a different (and hopefully better) static analysis technique. We are interested is getting feedback about the usefulness of this bug pattern. For sending feedback, check:

 * contributing guideline [https://github.com/spotbugs/spotbugs/blob/master/.github/CONTRIBUTING.md]
 * mailinglist [https://github.com/spotbugs/discuss/issues?q=]

In particular, the false-positive suppression heuristics for this bug pattern have not been extensively tuned, so reports about false positives are helpful to us.

See Weimer and Necula, Finding and Preventing Run-Time Error Handling Mistakes (PDF [https://people.eecs.berkeley.edu/~necula/Papers/rte_oopsla04.pdf]), for a description of the analysis technique.
Yaml yaml = new Yaml();
Object obj = yaml.load(inputStream);

// Create new YAML structure if file is empty
Map<String, Object> data;
if (obj instanceof Map)
{
Map<String, Object> data = (Map<String, Object>) obj;
data = (Map<String, Object>) obj;
}
else
{
data = new LinkedHashMap<>();
}

// Add or update CompileFlags section
Map<String, Object> compileFlags = (Map<String, Object>) data.get("CompileFlags");
if (compileFlags == null)
{
compileFlags = new LinkedHashMap<>();
data.put("CompileFlags", compileFlags); //$NON-NLS-1$
}
compileFlags.put("CompilationDatabase", "build"); //$NON-NLS-1$ //$NON-NLS-2$
compileFlags.put("Remove", Arrays.asList("-m*", "-f*")); //$NON-NLS-1$ //$NON-NLS-2$

// Add new attribute to CompileFlags
Map<String, Object> compileFlags = (Map<String, Object>) data.get("CompileFlags"); //$NON-NLS-1$
compileFlags.put("Remove", new String[] { "-m*", "-f*" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// Write updated clangd back to file
try (Writer writer = new FileWriter(file))

Check warning on line 63 in bundles/com.espressif.idf.lsp/src/com/espressif/idf/lsp/ClangdConfigFileHandler.java

View workflow job for this annotation

GitHub Actions / spotbugs

DM_DEFAULT_ENCODING

Found reliance on default encoding in com.espressif.idf.lsp.ClangdConfigFileHandler.update(IProject): new java.io.FileWriter(File)
Raw output
Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behavior to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.
{
yaml.dump(data, writer);
}
catch (IOException e)
{
Logger.log("Error writing .clangd file: " + e.getMessage()); //$NON-NLS-1$
}
}

// Write updated clangd back to file
try (Writer writer = new FileWriter(file))
private File getClangdConfigFile(IProject project) throws IOException, CoreException
{
// Resolve the path of the clangd config file within the project directory
Path clangdPath = project.getLocation().toPath().resolve(ILSPConstants.CLANGD_CONFIG_FILE);
if (!Files.exists(clangdPath))
{
try
{
yaml.dump(data, writer);
Files.createFile(clangdPath);
}
catch (IOException e)
{
Logger.log("Error writing .clangd file: " + e.getMessage()); //$NON-NLS-1$
throw new IOException("Failed to create clangd config file: " + e.getMessage(), e); //$NON-NLS-1$
}
project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
}
else
{
Logger.log("Invalid .clangd file format."); //$NON-NLS-1$
}
return clangdPath.toFile();
}

private File getClangdConfigFile(IProject project)
{
// Path to the existing clangd file
IFile file = project.getFile(ILSPConstants.CLANGD_CONFIG_FILE);
return file.getLocation().toFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package com.espressif.idf.ui.wizard;

import java.io.File;
import java.io.FileNotFoundException;

import org.eclipse.cdt.debug.internal.core.InternalDebugCoreMessages;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -147,7 +146,7 @@ private void updateClangdFile(IProject project)
{
try {
new ClangdConfigFileHandler().update(project);
} catch (FileNotFoundException e) {
} catch (Exception e) {
Logger.log(e);
}
}
Expand Down

0 comments on commit 432367f

Please sign in to comment.