compress: single-threaded ZSTD_compressStream2() accepts ZSTD_e_continue after unfinished ZSTD_e_end instead of returning stage_wrong
まだ誰も着手していません。
評価
調査の方向性
lib/compress/zstd_compress.c の6174行目付近にある ZSTD_compressStream_generic() を読み、lib/compress/zstdmt_compress.c の1885〜1888行目付近にあるステージチェックと比較してください。issue にあるシングルスレッドのケースを再現し、関連するストリーミング圧縮テストを実行してください。未完了の ZSTD_e_end の後に ZSTD_e_continue が ZSTD_error_stage_wrong を返し、マルチスレッドの場合と同じ動作になれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Describe the bug
lib/zstd.h:966-969 states:
"after a
ZSTD_e_enddirective, if internal buffer is not fully flushed (@return != 0), onlyZSTD_e_endorZSTD_e_flushoperations are allowed. Before starting a new compression job, or changing compression parameters, it is required to fully flush internal buffers."
Multi-threaded streaming enforces this explicitly at lib/compress/zstdmt_compress.c:1885-1888:
if ((mtctx->frameEnded) && (endOp==ZSTD_e_continue)) {
/* current frame being ended. Only flush/end are allowed */
return ERROR(stage_wrong);
}
However, single-threaded ZSTD_compressStream_generic() (lib/compress/zstd_compress.c:6139-6323) has no such check. Calling ZSTD_compressStream2() with ZSTD_e_continue and new input while a previous ZSTD_e_end still has unflushed bytes (zcs->frameEnded == 1, zcs->streamStage == zcss_flush) silently flushes the ended frame, resets the session, leaves the new input unconsumed (in2.pos == 0), and returns 0 (OK) instead of ZSTD_error_stage_wrong (60).
To Reproduce
C reproducer (built and run against multi-threaded libzstd.so from dev 539a64c423 to compare ST vs MT):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zstd.h"
#include "zstd_errors.h"
int main(void) {
/* 1. Single-threaded (nbWorkers = 0) */
{
const char* data = "0123456789abcdefXYZ";
char outbuf[128];
ZSTD_CCtx* cctx = ZSTD_createCCtx();
ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
ZSTD_inBuffer in1 = { data, 16, 0 };
ZSTD_outBuffer out1 = { outbuf, 4, 0 };
size_t r1 = ZSTD_compressStream2(cctx, &out1, &in1, ZSTD_e_end);
ZSTD_inBuffer in2 = { data + 16, 3, 0 };
ZSTD_outBuffer out2 = { outbuf + out1.pos, sizeof(outbuf) - out1.pos, 0 };
size_t r2 = ZSTD_compressStream2(cctx, &out2, &in2, ZSTD_e_continue);
printf("ST after ZSTD_e_end: r1=%zu (in1.pos=%zu/16, out1.pos=%zu/4)\n", r1, in1.pos, out1.pos);
printf("ST after ZSTD_e_continue: r2=%zu (%s, in2.pos=%zu/3, out2.pos=%zu)\n",
r2, ZSTD_isError(r2) ? ZSTD_getErrorName(r2) : "OK", in2.pos, out2.pos);
ZSTD_freeCCtx(cctx);
}
/* 2. Multi-threaded (nbWorkers = 2, input > ZSTDMT_JOBSIZE_MIN) */
{
size_t n = 1024 * 1024;
char* data = malloc(n + 16);
char* outbuf = malloc(2 * 1024 * 1024);
memset(data, 'A', n);
memcpy(data + n, "XYZ", 3);
ZSTD_CCtx* cctx = ZSTD_createCCtx();
ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 2);
ZSTD_CCtx_setParameter(cctx, ZSTD_c_jobSize, 512 * 1024);
ZSTD_inBuffer in1 = { data, n, 0 };
ZSTD_outBuffer out1 = { outbuf, 4, 0 };
size_t r1 = ZSTD_compressStream2(cctx, &out1, &in1, ZSTD_e_continue);
while (in1.pos < in1.size && !ZSTD_isError(r1)) {
out1.size = out1.pos + 4096;
r1 = ZSTD_compressStream2(cctx, &out1, &in1, ZSTD_e_continue);
}
out1.size = out1.pos;
r1 = ZSTD_compressStream2(cctx, &out1, &in1, ZSTD_e_end);
ZSTD_inBuffer in2 = { data + n, 3, 0 };
ZSTD_outBuffer out2 = { outbuf + out1.pos, 1024 * 1024, 0 };
size_t r2 = ZSTD_compressStream2(cctx, &out2, &in2, ZSTD_e_continue);
printf("MT after ZSTD_e_end: r1=%zu (in1.pos=%zu/%zu)\n", r1, in1.pos, in1.size);
printf("MT after ZSTD_e_continue: code=%u (%s)\n",
(unsigned)ZSTD_getErrorCode(r2), ZSTD_getErrorName(r2));
ZSTD_freeCCtx(cctx);
free(data);
free(outbuf);
}
return 0;
}
Actual output on dev 539a64c423:
ST after ZSTD_e_end: r1=25 (in1.pos=16/16, out1.pos=4/4)
ST after ZSTD_e_continue: r2=0 (OK, in2.pos=0/3, out2.pos=25)
MT after ZSTD_e_end: r1=31 (in1.pos=1048576/1048576)
MT after ZSTD_e_continue: code=60 (Operation not authorized at current processing stage)
Expected behavior
Single-threaded ZSTD_compressStream2(..., ZSTD_e_continue) after an unfinished ZSTD_e_end should fail with ZSTD_error_stage_wrong (60), matching lib/zstd.h:966-969 and multi-threaded ZSTDMT_compressStream_generic().
Desktop (please complete the following information):
- OS: Linux x86_64 (Ubuntu 22.04)
- Version:
dev539a64c423(v1.6.0+) - Compiler: gcc 11.4.0
- Flags:
-O2 - Build system: Makefile
Additional context
Suggested fix in ZSTD_compressStream_generic() (lib/compress/zstd_compress.c:6174), mirroring lib/compress/zstdmt_compress.c:1885-1888:
--- a/lib/compress/zstd_compress.c
+++ b/lib/compress/zstd_compress.c
@@ -6172,6 +6172,10 @@ static size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
assert(output->pos <= output->size);
assert((U32)flushMode <= (U32)ZSTD_e_end);
+ if (zcs->frameEnded && flushMode == ZSTD_e_continue) {
+ RETURN_ERROR(stage_wrong, "current frame being ended; only flush/end are allowed");
+ }
+
while (someMoreWork) {
Note: cctx->frameEnded is reset to 0 in ZSTD_CCtx_init_compressStream2() (line 6476), which always runs when streamStage == zcss_init before entering ZSTD_compressStream_generic().
- 主要言語
- C
- スター
- 28.1k
- フォーク
- 2.7k
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
facebook/zstd のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
-
windows-artifacts: the win-arm64 job uses a Visual Studio generator missing from windows-11-arm対応中かも @Ranjeet2063 が 12 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
NVIDIA/open-gpu-kernel-modules#1635 ·
メンテナーはふだん 1 日以内に返信
-
iOS: the "Edit controls" button never hides, even with a controller connected, and covers the gameオープンgame: 010065A014024000 ios
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
seedds/nxnative#3714 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
HTTP/3
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信