Make XzEtentWriter::Init() idempotent
xz_dec_init allocates memory, so avoid memory leak in calling Init()
multiple times
Test: th
Change-Id: I2dc20f6f9127b6d7749369385f982a08c78e6e44
diff --git a/payload_consumer/xz_extent_writer.h b/payload_consumer/xz_extent_writer.h
index 70338f2..caf34ec 100644
--- a/payload_consumer/xz_extent_writer.h
+++ b/payload_consumer/xz_extent_writer.h
@@ -34,6 +34,10 @@
namespace chromeos_update_engine {
class XzExtentWriter : public ExtentWriter {
+ struct xz_deleter {
+ constexpr void operator()(xz_dec* p) { xz_dec_end(p); }
+ };
+
public:
explicit XzExtentWriter(std::unique_ptr<ExtentWriter> underlying_writer)
: underlying_writer_(std::move(underlying_writer)) {}
@@ -47,7 +51,7 @@
// The underlying ExtentWriter.
std::unique_ptr<ExtentWriter> underlying_writer_;
// The opaque xz decompressor struct.
- xz_dec* stream_{nullptr};
+ std::unique_ptr<xz_dec, xz_deleter> stream_{nullptr};
brillo::Blob input_buffer_;
DISALLOW_COPY_AND_ASSIGN(XzExtentWriter);