compress: single-threaded ZSTD_compressStream2() accepts ZSTD_e_continue after unfinished ZSTD_e_end instead of returning stage_wrong
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 82/100
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_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().
- Lenguaje dominante
- C
- Estrellas
- 28.1k
- Forks
- 2.7k
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de facebook/zstd
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 83/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 83/100
-
windows-artifacts: the win-arm64 job uses a Visual Studio generator missing from windows-11-armPosiblemente ocupada @Ranjeet2063 la tomó hace 12 días. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 86/100
Todos los issues de facebook/zstd
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
NVIDIA/open-gpu-kernel-modules#1635 ·
Los mantenedores suelen responder en 1 día
-
iOS: the "Edit controls" button never hides, even with a controller connected, and covers the gameAbiertogame: 010065A014024000 ios
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
seedds/nxnative#3714 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
HTTP/3
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día