@@ -352,6 +352,14 @@ class ZstdCompressContext final : public ZstdContext { |
352 | 352 |
|
353 | 353 | uint64_t pledged_src_size_ = ZSTD_CONTENTSIZE_UNKNOWN; |
354 | 354 | std::optional<uint64_t> consumed_src_size_; |
| 355 | + |
| 356 | + // A frame is complete once ZSTD_compressStream2() has been called with |
| 357 | + // ZSTD_e_end and has returned 0. Resetting an incomplete frame is only unsafe |
| 358 | + // once some of that frame has already been written out: those bytes cannot be |
| 359 | + // discarded, and the next frame would be appended to the fragment. Unflushed |
| 360 | + // internal state alone is cancelled by ZSTD_reset_session_only. |
| 361 | + bool frame_complete_ = true; |
| 362 | + bool frame_output_emitted_ = false; |
355 | 363 | }; |
356 | 364 |
|
357 | 365 | class ZstdDecompressContext final : public ZstdContext { |
@@ -1678,6 +1686,8 @@ CompressionError ZstdCompressContext::Init(uint64_t pledged_src_size, |
1678 | 1686 | std::string_view dictionary, |
1679 | 1687 | bool) { |
1680 | 1688 | pledged_src_size_ = pledged_src_size; |
| 1689 | + frame_complete_ = true; |
| 1690 | + frame_output_emitted_ = false; |
1681 | 1691 | if (pledged_src_size == ZSTD_CONTENTSIZE_UNKNOWN) { |
1682 | 1692 | consumed_src_size_.reset(); |
1683 | 1693 | } else { |
@@ -1718,6 +1728,17 @@ CompressionError ZstdCompressContext::Init(uint64_t pledged_src_size, |
1718 | 1728 | } |
1719 | 1729 |
|
1720 | 1730 | CompressionError ZstdCompressContext::ResetStream() { |
| 1731 | + // ZSTD_reset_session_only cancels unflushed internal data. Bytes that have |
| 1732 | + // already been written out cannot be taken back, so refuse reset only when |
| 1733 | + // the current incomplete frame has already emitted output. |
| 1734 | + if (!frame_complete_ && frame_output_emitted_) { |
| 1735 | + return CompressionError( |
| 1736 | + "Cannot reset a zstd stream with an incomplete frame; end the frame " |
| 1737 | + "or discard the output produced so far", |
| 1738 | + "ERR_ZLIB_INCOMPLETE_FRAME", |
| 1739 | + ZSTD_error_stage_wrong); |
| 1740 | + } |
| 1741 | + |
1721 | 1742 | size_t result = ZSTD_CCtx_reset(cctx_.get(), ZSTD_reset_session_only); |
1722 | 1743 | if (ZSTD_isError(result)) { |
1723 | 1744 | const ZSTD_ErrorCode error = ZSTD_getErrorCode(result); |
@@ -1737,6 +1758,8 @@ CompressionError ZstdCompressContext::ResetStream() { |
1737 | 1758 | } else { |
1738 | 1759 | consumed_src_size_ = 0; |
1739 | 1760 | } |
| 1761 | + frame_complete_ = true; |
| 1762 | + frame_output_emitted_ = false; |
1740 | 1763 | error_ = ZSTD_error_no_error; |
1741 | 1764 | error_string_.clear(); |
1742 | 1765 | error_code_string_.clear(); |
@@ -1751,19 +1774,28 @@ void ZstdCompressContext::DoThreadPoolWork() { |
1751 | 1774 | if (consumed_src_size_.has_value()) { |
1752 | 1775 | *consumed_src_size_ += input_.pos - input_pos; |
1753 | 1776 | } |
| 1777 | + if (output_.pos > 0) { |
| 1778 | + frame_output_emitted_ = true; |
| 1779 | + } |
1754 | 1780 | if (ZSTD_isError(remaining)) { |
1755 | 1781 | error_ = ZSTD_getErrorCode(remaining); |
1756 | 1782 | error_code_string_ = ZstdStrerror(error_); |
1757 | 1783 | error_string_ = ZSTD_getErrorString(error_); |
1758 | | - } else if (remaining == 0 && flush_ == ZSTD_e_end && |
1759 | | - consumed_src_size_.has_value()) { |
1760 | | - uint64_t const consumed_src_size = *consumed_src_size_; |
1761 | | - consumed_src_size_.reset(); |
1762 | | - if (consumed_src_size != pledged_src_size_) { |
1763 | | - error_ = ZSTD_error_srcSize_wrong; |
1764 | | - error_code_string_ = ZstdStrerror(error_); |
1765 | | - error_string_ = ZSTD_getErrorString(error_); |
| 1784 | + frame_complete_ = false; |
| 1785 | + } else if (remaining == 0 && flush_ == ZSTD_e_end) { |
| 1786 | + frame_complete_ = true; |
| 1787 | + frame_output_emitted_ = false; |
| 1788 | + if (consumed_src_size_.has_value()) { |
| 1789 | + uint64_t const consumed_src_size = *consumed_src_size_; |
| 1790 | + consumed_src_size_.reset(); |
| 1791 | + if (consumed_src_size != pledged_src_size_) { |
| 1792 | + error_ = ZSTD_error_srcSize_wrong; |
| 1793 | + error_code_string_ = ZstdStrerror(error_); |
| 1794 | + error_string_ = ZSTD_getErrorString(error_); |
| 1795 | + } |
1766 | 1796 | } |
| 1797 | + } else { |
| 1798 | + frame_complete_ = false; |
1767 | 1799 | } |
1768 | 1800 | } |
1769 | 1801 |
|
|