Copilot commented on code in PR #3457:
URL: https://github.com/apache/avro/pull/3457#discussion_r2369583500


##########
lang/c++/impl/ZstdDecompressWrapper.cc:
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifdef ZSTD_CODEC_AVAILABLE
+
+#include "ZstdDecompressWrapper.hh"
+#include "Exception.hh"
+
+#include <zstd.h>
+
+namespace avro {
+
+std::string ZstdDecompressWrapper::decompress(const std::vector<char> 
&compressed) {
+    std::string uncompressed;
+    // Get the decompressed size
+    size_t decompressed_size = ZSTD_getFrameContentSize(
+        reinterpret_cast<const char *>(compressed.data()), compressed.size());
+    if (decompressed_size == ZSTD_CONTENTSIZE_ERROR) {
+        throw Exception("ZSTD: Not a valid compressed frame");
+    } else if (decompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) {
+        // Stream decompress the data
+        ZSTD_inBuffer in{compressed.data(), compressed.size(), 0};
+        std::vector<char> tmp(ZSTD_DStreamOutSize());
+        ZSTD_outBuffer out{tmp.data(), tmp.size(), 0};
+        size_t ret;
+        do {
+            out.pos = 0;
+            ret = ZSTD_decompressStream(dctx_, &out, &in);
+            if (ZSTD_isError(ret)) {
+                throw Exception("ZSTD decompression error: {}", 
ZSTD_getErrorName(ret));
+            }
+            uncompressed.append(tmp.data(), out.pos);
+        } while (ret != 0);
+    } else {
+        // Batch decompress the data
+        uncompressed.resize(decompressed_size);
+        size_t result = ZSTD_decompress(
+            uncompressed.data(), decompressed_size,
+            reinterpret_cast<const char *>(compressed.data()), 
compressed.size());
+
+        if (ZSTD_isError(result)) {
+            throw Exception("ZSTD decompression error: {}", 
ZSTD_getErrorName(result));
+        }
+        if (result != decompressed_size) {
+            throw Exception("ZSTD: Decompressed size mismatch: expected {}, 
got {}",
+                            decompressed_size, result);
+        }
+    }
+    return uncompressed;
+}
+
+ZstdDecompressWrapper::ZstdDecompressWrapper() {
+    dctx_ = ZSTD_createDCtx();
+    if (!dctx_) {
+        ZSTD_freeDCtx(dctx_);
+        throw Exception("ZSTD_createDCtx() failed");
+    }

Review Comment:
   Calling ZSTD_freeDCtx() on a null pointer when ZSTD_createDCtx() fails is 
incorrect. The null check should simply throw the exception without attempting 
to free the null pointer.
   ```suggestion
           throw Exception("ZSTD_createDCtx() failed");
       }
       }
   ```



##########
lang/c++/impl/ZstdCompressWrapper.cc:
##########
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifdef ZSTD_CODEC_AVAILABLE
+
+#include "ZstdCompressWrapper.hh"
+#include "Exception.hh"
+
+#include <zstd.h>
+
+namespace avro {
+
+std::vector<char> ZstdCompressWrapper::compress(const std::vector<char> 
&uncompressed) {
+    // Pre-allocate buffer for compressed data
+    size_t max_compressed_size = ZSTD_compressBound(uncompressed.size());
+    if (ZSTD_isError(max_compressed_size)) {
+        throw Exception("ZSTD compression error: {}", 
ZSTD_getErrorName(max_compressed_size));
+    }
+    std::vector<char> compressed(max_compressed_size);
+
+    // Compress the data using ZSTD block API
+    size_t compressed_size = ZSTD_compress(
+        compressed.data(), max_compressed_size,
+        uncompressed.data(), uncompressed.size(),
+        ZSTD_CLEVEL_DEFAULT);
+
+    if (ZSTD_isError(compressed_size)) {
+        throw Exception("ZSTD compression error: {}", 
ZSTD_getErrorName(compressed_size));
+    }
+    compressed.resize(compressed_size);
+    return compressed;
+}
+
+ZstdCompressWrapper::ZstdCompressWrapper() {
+    cctx_ = ZSTD_createCCtx();
+    if (!cctx_) {
+        ZSTD_freeCCtx(cctx_);

Review Comment:
   Calling ZSTD_freeCCtx() on a null pointer when ZSTD_createCCtx() fails is 
incorrect. The null check should simply throw the exception without attempting 
to free the null pointer.
   ```suggestion
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to