From 42f1f73b44e806a88bbb83e28586069ed4bd038b Mon Sep 17 00:00:00 2001 From: Arshan Dabirsiaghi Date: Thu, 1 Aug 2024 11:06:31 -0400 Subject: [PATCH] More graceful failure when expected files are missing in Semgrep (#436) Although some use cases are very strict about SARIF-referenced files being present, others aren't (like testing) -- we should handle this more gracefully. --- .../sarif/semgrep/SingleSemgrepRuleSarif.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SingleSemgrepRuleSarif.java b/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SingleSemgrepRuleSarif.java index 21774de40..2dc463f4f 100644 --- a/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SingleSemgrepRuleSarif.java +++ b/plugins/codemodder-plugin-semgrep/src/main/java/io/codemodder/providers/sarif/semgrep/SingleSemgrepRuleSarif.java @@ -5,10 +5,11 @@ import com.contrastsecurity.sarif.SarifSchema210; import io.codemodder.RuleSarif; import java.io.IOException; -import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * {@inheritDoc} @@ -77,8 +78,10 @@ public List getResultsByLocationPath(final Path path) { .getUri(); try { return Files.isSameFile(path, repositoryRoot.resolve(uri)); - } catch (IOException e) { // this should never happen - throw new UncheckedIOException(e); + } catch (IOException e) { + // this can happen if the file referenced in SARIF doesn't exist (like in tests) + log.debug("Couldn't find file referenced in SARIF", e); + return false; } }) .toList(); @@ -91,5 +94,5 @@ public String getDriver() { return sarif.getRuns().get(0).getTool().getDriver().getName(); } - static final String toolName = "semgrep"; + private static final Logger log = LoggerFactory.getLogger(SingleSemgrepRuleSarif.class); }