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

Open Beginner friendly
#5,125 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
backend

Research direction

Read lib/compress/zstd_compress.c at ZSTD_compressStream_generic() around line 6174 and compare the stage check in lib/compress/zstdmt_compress.c around lines 1885-1888. Reproduce the single-threaded case from the issue, then run the relevant streaming compression tests. Done when ZSTD_e_continue after an unfinished ZSTD_e_end returns ZSTD_error_stage_wrong, matching the multi-threaded behavior.

Written by the indexing model from the issue text.

Description

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().

Dominant language
C
Stars
28.1k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from facebook/zstd

All issues in facebook/zstd

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.