Skip to content
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

Progress Metric - Division by zero #6325

Closed
danhardman opened this issue Apr 1, 2024 · 1 comment
Closed

Progress Metric - Division by zero #6325

danhardman opened this issue Apr 1, 2024 · 1 comment
Labels
bug Verified bug by the Nova team

Comments

@danhardman
Copy link

  • Laravel Version: 10.10
  • Nova Version: v4.33.1 (Silver Surfer)
  • PHP Version: 8.1
  • Reproduction code:

vendor/laravel/nova/src/Metrics/ProgressResult.php@L166

/**
     * Prepare the metric result for JSON serialization.
     *
     * @return array<string, mixed>
     */
    public function jsonSerialize(): array
    {
        $target = max(0.0, -66.0); // Replaced with test values to demonstrate

        return [
            'value' => $this->resolveTransformedValue($this->value),
            'target' => $this->resolveTransformedValue($target),
            'percentage' => $target !== 0 ? round(($this->value / $target) * 100, $this->roundingPrecision, $this->roundingMode) : 0,
            'prefix' => $this->prefix,
            'suffix' => $this->suffix,
            'suffixInflection' => $this->suffixInflection,
            'format' => $this->format,
            'avoid' => $this->avoid,
        ];
    }

Description:

If $this->value is float 0.0 and $this->target is <0 then $target is assigned 0.0 which means $target !== 0 passes and we get a division by 0 error.

I know it's a stupid scenario, I'll update my code to not send a negative target. However, might be worth using a loose comparison or floatval as the Progress metric does support floats.

Detailed steps to reproduce the issue on a fresh Nova installation:

Use a progress metric with a value of 0.0 and a target below 0.

@crynobone crynobone added the bug Verified bug by the Nova team label Apr 3, 2024
Copy link

github-actions bot commented Apr 9, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Verified bug by the Nova team
Projects
None yet
Development

No branches or pull requests

2 participants