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

Abierto Apto para principiantes
#5,125 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
82/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c
Área
backend

Línea de trabajo

Lee lib/compress/zstd_compress.c en ZSTD_compressStream_generic() alrededor de la línea 6174 y compara la comprobación de etapa en lib/compress/zstdmt_compress.c alrededor de las líneas 1885-1888. Reproduce el caso de un solo hilo del issue y luego ejecuta las pruebas pertinentes de compresión en streaming. El trabajo estará terminado cuando ZSTD_e_continue después de un ZSTD_e_end sin finalizar devuelva ZSTD_error_stage_wrong, igual que en el comportamiento multihilo.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
C
Estrellas
28.1k
Forks
2.7k
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de facebook/zstd

Todos los issues de facebook/zstd

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.