From 02f30a5323bab224292dadb2a79319133997cc21 Mon Sep 17 00:00:00 2001 From: Guillem Jover <guillem@debian.org> Date: Mon, 9 Jan 2023 00:51:31 +0100 Subject: [PATCH] libdpkg: Split compression filter operation tracking from its status Tracking the compression filter operation and its status merges two distinct values into the same variable making things more difficult, as we were using bits when these should really be mutually exclusive values. --- lib/dpkg/compress.c | 49 +++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/dpkg/compress.c b/lib/dpkg/compress.c index bb1bf2412..d92530fc9 100644 --- a/lib/dpkg/compress.c +++ b/lib/dpkg/compress.c @@ -100,6 +100,18 @@ command_decompress_init(struct command *cmd, const char *name, const char *desc) } #endif +#if defined(WITH_LIBLZMA) +enum dpkg_stream_filter { + DPKG_STREAM_COMPRESS = 1, + DPKG_STREAM_DECOMPRESS = 2, +}; + +enum dpkg_stream_status { + DPKG_STREAM_INIT = 0, + DPKG_STREAM_RUN = 1, +}; +#endif + struct compressor { const char *name; const char *extension; @@ -475,19 +487,12 @@ static const struct compressor compressor_bzip2 = { #define XZ "xz" #ifdef WITH_LIBLZMA -enum dpkg_stream_status { - DPKG_STREAM_INIT = DPKG_BIT(1), - DPKG_STREAM_RUN = DPKG_BIT(2), - DPKG_STREAM_COMPRESS = DPKG_BIT(3), - DPKG_STREAM_DECOMPRESS = DPKG_BIT(4), - DPKG_STREAM_FILTER = DPKG_STREAM_COMPRESS | DPKG_STREAM_DECOMPRESS, -}; - struct io_lzma { const char *desc; struct compress_params *params; + enum dpkg_stream_filter filter; enum dpkg_stream_status status; lzma_action action; @@ -506,29 +511,33 @@ dpkg_lzma_strerror(struct io_lzma *io, lzma_ret code) case LZMA_MEM_ERROR: return strerror(ENOMEM); case LZMA_MEMLIMIT_ERROR: - if (io->status & DPKG_STREAM_RUN) + if (io->status == DPKG_STREAM_RUN) return _("memory usage limit reached"); return impossible; case LZMA_OPTIONS_ERROR: - if (io->status == (DPKG_STREAM_INIT | DPKG_STREAM_COMPRESS)) + if (io->filter == DPKG_STREAM_COMPRESS && + io->status == DPKG_STREAM_INIT) return _("unsupported compression preset"); - if (io->status == (DPKG_STREAM_RUN | DPKG_STREAM_DECOMPRESS)) + if (io->filter == DPKG_STREAM_DECOMPRESS && + io->status == DPKG_STREAM_RUN) return _("unsupported options in file header"); return impossible; case LZMA_DATA_ERROR: - if (io->status & DPKG_STREAM_RUN) + if (io->status == DPKG_STREAM_RUN) return _("compressed data is corrupt"); return impossible; case LZMA_BUF_ERROR: - if (io->status & DPKG_STREAM_RUN) + if (io->status == DPKG_STREAM_RUN) return _("unexpected end of input"); return impossible; case LZMA_FORMAT_ERROR: - if (io->status == (DPKG_STREAM_RUN | DPKG_STREAM_DECOMPRESS)) + if (io->filter == DPKG_STREAM_DECOMPRESS && + io->status == DPKG_STREAM_RUN) return _("file format not recognized"); return impossible; case LZMA_UNSUPPORTED_CHECK: - if (io->status == (DPKG_STREAM_INIT | DPKG_STREAM_COMPRESS)) + if (io->filter == DPKG_STREAM_COMPRESS && + io->status == DPKG_STREAM_INIT) return _("unsupported type of integrity check"); return impossible; default: @@ -554,7 +563,7 @@ filter_lzma(struct io_lzma *io, int fd_in, int fd_out) io->action = LZMA_RUN; io->status = DPKG_STREAM_INIT; io->init(io, &s); - io->status = (io->status & DPKG_STREAM_FILTER) | DPKG_STREAM_RUN; + io->status = DPKG_STREAM_RUN; do { ssize_t len; @@ -649,7 +658,7 @@ filter_unxz_init(struct io_lzma *io, lzma_stream *s) #endif lzma_ret ret; - io->status |= DPKG_STREAM_DECOMPRESS; + io->filter = DPKG_STREAM_DECOMPRESS; #ifdef HAVE_LZMA_MT_DECODER mt_options.memlimit_stop = UINT64_MAX; @@ -681,7 +690,7 @@ filter_xz_init(struct io_lzma *io, lzma_stream *s) #endif lzma_ret ret; - io->status |= DPKG_STREAM_COMPRESS; + io->filter = DPKG_STREAM_COMPRESS; preset = io->params->level; if (io->params->strategy == COMPRESSOR_STRATEGY_EXTREME) @@ -842,7 +851,7 @@ filter_unlzma_init(struct io_lzma *io, lzma_stream *s) uint64_t memlimit = UINT64_MAX; lzma_ret ret; - io->status |= DPKG_STREAM_DECOMPRESS; + io->filter = DPKG_STREAM_DECOMPRESS; ret = lzma_alone_decoder(s, memlimit); if (ret != LZMA_OK) @@ -856,7 +865,7 @@ filter_lzma_init(struct io_lzma *io, lzma_stream *s) lzma_options_lzma options; lzma_ret ret; - io->status |= DPKG_STREAM_COMPRESS; + io->filter = DPKG_STREAM_COMPRESS; preset = io->params->level; if (io->params->strategy == COMPRESSOR_STRATEGY_EXTREME) -- GitLab