compress: single-threaded ZSTD_compressStream2() accepts ZSTD_e_continue after unfinished ZSTD_e_end instead of returning stage_wrong

オープン 初心者向け
#5,125 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
backend

調査の方向性

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_end directive, if internal buffer is not fully flushed (@return != 0), only ZSTD_e_end or ZSTD_e_flush operations 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: dev 539a64c423 (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 はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

facebook/zstd のほかの issue

facebook/zstd の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。