Skip to content

Possible division by zero in zstdcli_trace.c #4368

Description

@Aruise

Hi,

In function TRACE_log 'duration' is received from function ZSTD_trace_compress_end, which states that it could be zero

Which leads to a possible division by zero bug

void ZSTD_trace_compress_end(ZSTD_TraceCtx ctx, ZSTD_Trace const* trace)
{
    PTime const beginNanos = (PTime)ctx;
    PTime const endNanos = UTIL_clockSpanNano(g_enableTime);
    PTime const durationNanos = endNanos > beginNanos ? endNanos - beginNanos : 0;
    assert(g_traceFile != NULL);
    assert(trace->version == ZSTD_VERSION_NUMBER); /* CLI version must match. */
    TRACE_log("compress", durationNanos, trace);
}

PTime const durationNanos = endNanos > beginNanos ? endNanos - beginNanos : 0; states that 'durationNanos' will be either 'endNanos - beginNanos', if 'endNanos' is bigger, or zero otherwise

Next this function calls TRACE_log("compress", durationNanos, trace);

In TRACE_log, further, it passed to a equation, where possibly division by zero happens

static void TRACE_log(char const* method, PTime duration, ZSTD_Trace const* trace)
{
    int level = 0;
    int workers = 0;
    double const ratio = (double)trace->uncompressedSize / (double)trace->compressedSize;
    double const speed = ((double)trace->uncompressedSize * 1000) / (double)duration;
    if (trace->params) {
...

double const speed = ((double)trace->uncompressedSize * 1000) / (double)duration; here, if 'durationNanos' was zero, will be a division by zero

Same with ZSTD_trace_decompress_end

Activity

  1. terrelln commented on Apr 17, 2025

    @terrelln
    Contributor

    Thanks for the report @Aruise! Looks like a bug. It will only be triggered if the user passes --trace on the command line, which I don't expect many people are doing, so the impact should be quite limited.

  2. Mrigankkh commented on Apr 27, 2025

    @Mrigankkh

    Hey, I'm interested in working on this issue. Would it be okay if I take it?"

  3. terrelln commented on Apr 30, 2025

    @terrelln
    Contributor

    @Mrigankkh absolutely!

  4. Mrigankkh commented on Apr 30, 2025

    @Mrigankkh

    Awesome! On it!

  5. Mrigankkh commented on May 1, 2025

    @Mrigankkh

    Created a Pull Request for the Bug Fix: #4379

  6. mizoz commented on Apr 28, 2026

    @mizoz

    Hi! I'd love to contribute. Could you assign this issue to me?

  7. WilliamK112 commented on Jun 18, 2026

    @WilliamK112

    This appears to have been fixed already by #4464, which merged commit ae64545c6b6257c226dca6240be7b2d345d9bee5 (fixed a potential division by 0 in the cli trace unit). That PR modified programs/zstdcli_trace.c, and #4379 was later closed with the same note.

    So I do not think a new fix PR is needed for this issue. It may be closable as completed by #4464.

    Checked with Codex assistance; I verified #4379, #4464, and the merged commit metadata before commenting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions