From 82fe08ea8e2806825edf669a4250a7a4219f42e5 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Fri, 21 Jan 2022 11:40:06 +0100 Subject: [PATCH] Improve exception handling --- .../semmle/extractor/java/OdasaOutput.java | 2 +- .../src/main/kotlin/ExternalClassExtractor.kt | 13 ++++-- .../main/kotlin/KotlinExtractorExtension.kt | 41 +++++++++---------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/java/kotlin-extractor/src/main/java/com/semmle/extractor/java/OdasaOutput.java b/java/kotlin-extractor/src/main/java/com/semmle/extractor/java/OdasaOutput.java index 4780ade2179..e5acba3ca68 100644 --- a/java/kotlin-extractor/src/main/java/com/semmle/extractor/java/OdasaOutput.java +++ b/java/kotlin-extractor/src/main/java/com/semmle/extractor/java/OdasaOutput.java @@ -352,7 +352,7 @@ public class OdasaOutput { currentSpecFileEntry.getTrapFolder().toPath().resolve(dep)); } - public void closeWithoutAdditionalFiles() { + public void setHasError() { hasError = true; } } diff --git a/java/kotlin-extractor/src/main/kotlin/ExternalClassExtractor.kt b/java/kotlin-extractor/src/main/kotlin/ExternalClassExtractor.kt index af49ddecede..760d089eb43 100644 --- a/java/kotlin-extractor/src/main/kotlin/ExternalClassExtractor.kt +++ b/java/kotlin-extractor/src/main/kotlin/ExternalClassExtractor.kt @@ -33,9 +33,11 @@ class ExternalClassExtractor(val logger: FileLogger, val invocationTrapFile: Str logger.info("Skipping extracting class ${irClass.name}") } else { val trapFile = manager.file + val trapTmpFile = File.createTempFile("${trapFile.nameWithoutExtension}.", ".${trapFile.extension}.tmp", trapFile.parentFile) + val binaryPath = getIrClassBinaryPath(irClass) try { - GZIPOutputStream(trapFile.outputStream()).bufferedWriter().use { trapFileBW -> + GZIPOutputStream(trapTmpFile.outputStream()).bufferedWriter().use { trapFileBW -> // We want our comments to be the first thing in the file, // so start off with a mere TrapWriter val tw = TrapWriter(TrapLabelManager(), trapFileBW) @@ -57,9 +59,14 @@ class ExternalClassExtractor(val logger: FileLogger, val invocationTrapFile: Str fileExtractor.extractClassSource(irClass) } + + if (!trapTmpFile.renameTo(trapFile)) { + logger.warn(Severity.Error, "Failed to rename $trapTmpFile to $trapFile") + } + } catch (e: Exception) { - manager.closeWithoutAdditionalFiles() - trapFile.delete() + manager.setHasError() + trapTmpFile.delete() logger.error("Failed to extract '$binaryPath'", e) } } diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt b/java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt index 20f0cb8e682..2e181909730 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt @@ -114,32 +114,24 @@ fun doFile(invocationTrapFile: String, if (checkTrapIdentical || !trapFile.exists()) { val trapTmpFile = File.createTempFile("$srcFilePath.", ".trap.tmp", trapFileDir) - var hasError = false - trapTmpFile.bufferedWriter().use { trapFileBW -> - // We want our comments to be the first thing in the file, - // so start off with a mere TrapWriter - val tw = TrapWriter(TrapLabelManager(), trapFileBW) - tw.writeComment("Generated by the CodeQL Kotlin extractor for kotlin source code") - tw.writeComment("Part of invocation $invocationTrapFile") - // Now elevate to a SourceFileTrapWriter, and populate the - // file information - val sftw = tw.makeSourceFileTrapWriter(srcFile, true) - val externalClassExtractor = ExternalClassExtractor(logger, invocationTrapFile, srcFilePath, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted) - val fileExtractor = KotlinFileExtractor(logger, sftw, srcFilePath, null, externalClassExtractor, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted) - try { + + try { + trapTmpFile.bufferedWriter().use { trapFileBW -> + // We want our comments to be the first thing in the file, + // so start off with a mere TrapWriter + val tw = TrapWriter(TrapLabelManager(), trapFileBW) + tw.writeComment("Generated by the CodeQL Kotlin extractor for kotlin source code") + tw.writeComment("Part of invocation $invocationTrapFile") + // Now elevate to a SourceFileTrapWriter, and populate the + // file information + val sftw = tw.makeSourceFileTrapWriter(srcFile, true) + val externalClassExtractor = ExternalClassExtractor(logger, invocationTrapFile, srcFilePath, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted) + val fileExtractor = KotlinFileExtractor(logger, sftw, srcFilePath, null, externalClassExtractor, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted) + fileExtractor.extractFileContents(srcFile, sftw.fileId) externalClassExtractor.extractExternalClasses() - } catch (e: Exception) { - hasError = true - logger.error("Failed to extract '$srcFilePath'", e) } - } - if (hasError) { - if (!trapTmpFile.delete()) { - logger.warn(Severity.WarnLow, "Failed to delete $trapTmpFile") - } - } else { if (checkTrapIdentical && trapFile.exists()) { if (equivalentTrap(trapTmpFile, trapFile)) { if (!trapTmpFile.delete()) { @@ -158,6 +150,11 @@ fun doFile(invocationTrapFile: String, logger.warn(Severity.WarnLow, "Failed to rename $trapTmpFile to $trapFile") } } + } catch (e: Exception) { + logger.error("Failed to extract '$srcFilePath'", e) + if (!trapTmpFile.delete()) { + logger.warn(Severity.WarnLow, "Failed to delete $trapTmpFile") + } } } }