From 5dd92f5faee5abfaa32d0b180bea2439fd96544b Mon Sep 17 00:00:00 2001 From: Davide Angelocola Date: Fri, 26 Jun 2026 20:04:55 +0200 Subject: [PATCH] chore: clear a batch of Sonar findings - Discard unused size_t status from native free() calls and swallowed destructor throwables via unnamed variables `_` instead of `ignored` (S1854 / S1481 / S7467). - Exclude the benchmark module from Sonar analysis: it is a JMH harness, not shipped runtime, and its @Setup/@TearDown lifecycle trips a false "resource not closed" report (S2095). - Reword the C-struct reference comments to prose so they are not parsed as commented-out code (S125). - Extract the repeated ZDICT field-name literals into constants (S1192). Co-Authored-By: Claude Opus 4.8 --- pom.xml | 10 ++++++--- .../java/io/github/dfa1/zstd/Bindings.java | 2 +- .../io/github/dfa1/zstd/NativeObject.java | 2 +- .../io/github/dfa1/zstd/ZstdCompressCtx.java | 2 +- .../io/github/dfa1/zstd/ZstdCompressDict.java | 2 +- .../github/dfa1/zstd/ZstdCompressStream.java | 2 +- .../github/dfa1/zstd/ZstdDecompressCtx.java | 2 +- .../github/dfa1/zstd/ZstdDecompressDict.java | 2 +- .../dfa1/zstd/ZstdDecompressStream.java | 2 +- .../io/github/dfa1/zstd/ZstdDictionary.java | 21 +++++++++++-------- .../io/github/dfa1/zstd/ZstdInputStream.java | 4 ++-- .../io/github/dfa1/zstd/ZstdOutputStream.java | 4 ++-- .../io/github/dfa1/zstd/ZstdStreamBuffer.java | 6 +++--- 13 files changed, 34 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index bc00562..93b8b69 100644 --- a/pom.xml +++ b/pom.xml @@ -63,9 +63,13 @@ https://sonarcloud.io dfa11 dfa1_zstd-java - + + + **/benchmark/** + **/benchmark/** diff --git a/zstd/src/main/java/io/github/dfa1/zstd/Bindings.java b/zstd/src/main/java/io/github/dfa1/zstd/Bindings.java index 03b567c..9d1b45b 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/Bindings.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/Bindings.java @@ -166,7 +166,7 @@ final class Bindings { static final MethodHandle DPARAM_GET_BOUNDS = NativeLibrary.lookup("ZSTD_dParam_getBounds", FunctionDescriptor.of(BOUNDS_LAYOUT, JAVA_INT)); - // ZSTD_frameProgression { u64 ingested, consumed, produced, flushed; u32 currentJobID, nbActiveWorkers; } + // ZSTD_frameProgression layout: u64 ingested, consumed, produced, flushed, then u32 currentJobID, nbActiveWorkers. private static final MemoryLayout FRAME_PROGRESSION_LAYOUT = MemoryLayout.structLayout(JAVA_LONG, JAVA_LONG, JAVA_LONG, JAVA_LONG, JAVA_INT, JAVA_INT); // ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx*) — returned by value diff --git a/zstd/src/main/java/io/github/dfa1/zstd/NativeObject.java b/zstd/src/main/java/io/github/dfa1/zstd/NativeObject.java index 49edf4a..99f4578 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/NativeObject.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/NativeObject.java @@ -37,7 +37,7 @@ public final void close() { if (!MemorySegment.NULL.equals(p)) { try { tryClose(p); - } catch (Throwable ignored) { + } catch (Throwable _) { // destructors must not throw } } diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressCtx.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressCtx.java index 1ce8c82..0df19a8 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressCtx.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressCtx.java @@ -222,7 +222,7 @@ public long sizeOf() { @Override protected void tryClose(MemorySegment ptr) throws Throwable { - long ignored = (long) Bindings.FREE_CCTX.invokeExact(ptr); + var _ = (long) Bindings.FREE_CCTX.invokeExact(ptr); } @SuppressWarnings("unchecked") diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDict.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDict.java index d776a5c..30b5ec5 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDict.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDict.java @@ -111,7 +111,7 @@ public long sizeOf() { @Override protected void tryClose(MemorySegment ptr) throws Throwable { - long ignored = (long) Bindings.FREE_CDICT.invokeExact(ptr); + var _ = (long) Bindings.FREE_CDICT.invokeExact(ptr); } @SuppressWarnings("unchecked") diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressStream.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressStream.java index 3216e04..44b028c 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressStream.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressStream.java @@ -129,7 +129,7 @@ public long sizeOf() { @Override protected void tryClose(MemorySegment ptr) throws Throwable { try { - long ignored = (long) Bindings.FREE_CCTX.invokeExact(ptr); + var _ = (long) Bindings.FREE_CCTX.invokeExact(ptr); } finally { arena.close(); } diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java index 23bde76..e2c2b80 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java @@ -182,7 +182,7 @@ public long sizeOf() { @Override protected void tryClose(MemorySegment ptr) throws Throwable { - long ignored = (long) Bindings.FREE_DCTX.invokeExact(ptr); + var _ = (long) Bindings.FREE_DCTX.invokeExact(ptr); } @SuppressWarnings("unchecked") diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDict.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDict.java index 5e23c32..159166c 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDict.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDict.java @@ -81,7 +81,7 @@ public long sizeOf() { @Override protected void tryClose(MemorySegment ptr) throws Throwable { - long ignored = (long) Bindings.FREE_DDICT.invokeExact(ptr); + var _ = (long) Bindings.FREE_DDICT.invokeExact(ptr); } @SuppressWarnings("unchecked") diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressStream.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressStream.java index 22c6ba9..6f8f482 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressStream.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressStream.java @@ -80,7 +80,7 @@ public long sizeOf() { @Override protected void tryClose(MemorySegment ptr) throws Throwable { try { - long ignored = (long) Bindings.FREE_DCTX.invokeExact(ptr); + var _ = (long) Bindings.FREE_DCTX.invokeExact(ptr); } finally { arena.close(); } diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java index 0623a6a..2c6faa8 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdDictionary.java @@ -31,18 +31,21 @@ /// } public final class ZstdDictionary { - // ZDICT_cover_params_t { unsigned k,d,steps,nbThreads; double splitPoint; - // unsigned shrinkDict, shrinkDictMaxRegression; ZDICT_params_t zParams; } - // ZDICT_params_t { int compressionLevel; unsigned notificationLevel, dictID; } + private static final String FIELD_NB_THREADS = "nbThreads"; + private static final String FIELD_COMPRESSION_LEVEL = "compressionLevel"; + + // ZDICT_cover_params_t fields: unsigned k, d, steps, nbThreads; double + // splitPoint; unsigned shrinkDict, shrinkDictMaxRegression; then a nested + // ZDICT_params_t of int compressionLevel, unsigned notificationLevel, dictID. private static final MemoryLayout COVER_PARAMS = MemoryLayout.structLayout( JAVA_INT.withName("k"), JAVA_INT.withName("d"), JAVA_INT.withName("steps"), - JAVA_INT.withName("nbThreads"), + JAVA_INT.withName(FIELD_NB_THREADS), JAVA_DOUBLE.withName("splitPoint"), JAVA_INT.withName("shrinkDict"), JAVA_INT.withName("shrinkDictMaxRegression"), - JAVA_INT.withName("compressionLevel"), + JAVA_INT.withName(FIELD_COMPRESSION_LEVEL), JAVA_INT.withName("notificationLevel"), JAVA_INT.withName("dictID"), MemoryLayout.paddingLayout(4)); // trailing pad to the C struct's 8-byte alignment @@ -54,13 +57,13 @@ public final class ZstdDictionary { JAVA_INT.withName("d"), JAVA_INT.withName("f"), JAVA_INT.withName("steps"), - JAVA_INT.withName("nbThreads"), + JAVA_INT.withName(FIELD_NB_THREADS), MemoryLayout.paddingLayout(4), JAVA_DOUBLE.withName("splitPoint"), JAVA_INT.withName("accel"), JAVA_INT.withName("shrinkDict"), JAVA_INT.withName("shrinkDictMaxRegression"), - JAVA_INT.withName("compressionLevel"), + JAVA_INT.withName(FIELD_COMPRESSION_LEVEL), JAVA_INT.withName("notificationLevel"), JAVA_INT.withName("dictID")); @@ -192,8 +195,8 @@ private static ZstdDictionary optimize(List samples, int maxDictBytes, // zeroed params (auto-tune k/d/steps); set single-threaded + target level. MemoryLayout layout = fast ? FASTCOVER_PARAMS : COVER_PARAMS; MemorySegment params = arena.allocate(layout); - params.set(JAVA_INT, layout.byteOffset(PathElement.groupElement("nbThreads")), 1); - params.set(JAVA_INT, layout.byteOffset(PathElement.groupElement("compressionLevel")), compressionLevel); + params.set(JAVA_INT, layout.byteOffset(PathElement.groupElement(FIELD_NB_THREADS)), 1); + params.set(JAVA_INT, layout.byteOffset(PathElement.groupElement(FIELD_COMPRESSION_LEVEL)), compressionLevel); MethodHandle handle = fast ? Bindings.ZDICT_OPTIMIZE_FASTCOVER : Bindings.ZDICT_OPTIMIZE_COVER; MemorySegment dictBuf = arena.allocate(maxDictBytes); long produced; diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdInputStream.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdInputStream.java index cb6d246..e16ab4d 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdInputStream.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdInputStream.java @@ -159,8 +159,8 @@ public void close() throws IOException { } closed = true; try { - long ignored = (long) Bindings.FREE_DCTX.invokeExact(dctx); - } catch (Throwable ignored) { + var _ = (long) Bindings.FREE_DCTX.invokeExact(dctx); + } catch (Throwable _) { // best-effort free } arena.close(); diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdOutputStream.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdOutputStream.java index db8cedb..87d59e5 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdOutputStream.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdOutputStream.java @@ -173,8 +173,8 @@ public void close() throws IOException { } finally { closed = true; try { - long ignored = (long) Bindings.FREE_CCTX.invokeExact(cctx); - } catch (Throwable ignored) { + var _ = (long) Bindings.FREE_CCTX.invokeExact(cctx); + } catch (Throwable _) { // best-effort free } arena.close(); diff --git a/zstd/src/main/java/io/github/dfa1/zstd/ZstdStreamBuffer.java b/zstd/src/main/java/io/github/dfa1/zstd/ZstdStreamBuffer.java index bddd923..9e2bb01 100644 --- a/zstd/src/main/java/io/github/dfa1/zstd/ZstdStreamBuffer.java +++ b/zstd/src/main/java/io/github/dfa1/zstd/ZstdStreamBuffer.java @@ -6,9 +6,9 @@ import static java.lang.foreign.ValueLayout.ADDRESS; import static java.lang.foreign.ValueLayout.JAVA_LONG; -// Backing for ZSTD_inBuffer / ZSTD_outBuffer, which share the layout -// { void* ptr; size_t size; size_t pos; } -// on LP64: ptr@0 (8), size@8 (8), pos@16 (8) -> 24 bytes. +// Backing for ZSTD_inBuffer / ZSTD_outBuffer, which share a layout of a +// void pointer then two size_t values. On LP64: ptr@0 (8), size@8 (8), +// pos@16 (8) -> 24 bytes. final class ZstdStreamBuffer { static final long BYTES = 24;