facebook / facebook/zstd

Possible division by zero in zstdcli_trace.c

Open
#4,368 7 comments 1 reaction 0 assignees View on GitHub
bug good first issue help wanted
Dominant language
C
Stars
27.9k
Forks
2.6k
Avg merge
1d 3h
Merged PRs (30d)
8

Description

Hi,

In function [TRACE_log](https://github.com/facebook/zstd/blob/d654fca78690fa15cceb8058ac47454d914a0e63/programs/zstdcli_trace.c#L80) 'duration' is received from function [ZSTD_trace_compress_end](https://github.com/facebook/zstd/blob/d654fca78690fa15cceb8058ac47454d914a0e63/programs/zstdcli_trace.c#L135), 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](https://github.com/facebook/zstd/blob/d654fca78690fa15cceb8058ac47454d914a0e63/programs/zstdcli_trace.c#L153)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.