Skip to content

Commit

Permalink
refactor(cyclonedx): Avoid exceptions to be swallowed
Browse files Browse the repository at this point in the history
The BOM generator's `toString()` implementations swallow exceptions [1].
Work around that by calling the underlying functions directly.

[1]: https://github.com/CycloneDX/cyclonedx-core-java/blob/bee058458caed9e966021a1e09c56dd3bb331f46/src/main/java/org/cyclonedx/generators/xml/BomXmlGenerator.java#L141-L149

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Jul 16, 2024
1 parent 4aad014 commit d0ed6ca
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ class CycloneDxReporter : Reporter {
outputFileExtensions.forEach { fileExtension ->
val outputFile = outputDir.resolve("$outputName.$fileExtension")

val bomGenerator = when (fileExtension) {
"xml" -> BomGeneratorFactory.createXml(schemaVersion, bom)
val bom = when (fileExtension) {
"xml" -> BomGeneratorFactory.createXml(schemaVersion, bom).toXmlString()
"json" -> {
// JSON output cannot handle extensible types (see [1]), so simply remove them. As JSON output is
// guaranteed to be the last format serialized, it is okay to modify the BOM here without doing a
Expand All @@ -391,13 +391,13 @@ class CycloneDxReporter : Reporter {
}
}

BomGeneratorFactory.createJson(schemaVersion, bomWithoutExtensibleTypes)
BomGeneratorFactory.createJson(schemaVersion, bomWithoutExtensibleTypes).toJsonString()
}

else -> throw IllegalArgumentException("Unsupported CycloneDX file extension '$fileExtension'.")
}

outputFile.bufferedWriter().use { it.write(bomGenerator.toString()) }
outputFile.bufferedWriter().use { it.write(bom) }
writtenFiles += outputFile
}

Expand Down

0 comments on commit d0ed6ca

Please sign in to comment.