Skip to content

Bug Fix: Fixed potential Divide by Zero in zstdcli_trace.c #4379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

Mrigankkh
Copy link

Bug Fix for Issue #4368

In the TRACE_log function in zstdcli_trace.c, in case duration is 0, speed is set to 0, circumventing the potential divide by 0 error.

double const speed = ((double)trace->uncompressedSize * 1000) / (double)duration;
const double speed = duration != 0
? ((double)trace->uncompressedSize * 1000) / (double)duration
: 0.0;
Copy link
Contributor

@sebres sebres Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero speed? Shall not it be INFINITY either?..

However, since duration is a precise time counter (and it's nanosecond precise), I don't think the duration as diff between 2 measurements could be ever 0, because would mean few CPU instructions.
But to be sure (and safe in the future), I'd suggest simply something under its min-value (e. g. small fraction like .1 or 1e-4):

- double const speed = ((double)trace->uncompressedSize * 1000) / (double)duration;
+ double const speed = ((double)trace->uncompressedSize * 1000) / (duration ? (double)duration : .1);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this suggestion

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

Successfully merging this pull request may close these issues.

4 participants