Skip to content

A free open source IT asset/license management system

One more fix for #19303 - division by zero in asset models

snipe277960dade39
diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php
index c31451b1d3..ad03a5b068 100755
--- a/app/Models/AssetModel.php
+++ b/app/Models/AssetModel.php
@@ -167,18 +167,24 @@ class AssetModel extends SnipeModel
// as N+1 queries per model in the transformer loop. Read straight off
// getAttributes() rather than via __get so we don't accidentally
// trigger a relation load when the attribute isn't there.
+ // Cast to int at read time: PDO with MySQL default emulated prepares
+ // returns COUNT() values as string, and Eloquent doesn't auto-cast
+ // withCount aliases (no $casts entry for `remaining` / `assets_count`).
+ // A string "0" then slips past a strict === 0 guard AND — because PHP
+ // 8's arithmetic operators auto-convert numeric strings — still hits
+ // DivisionByZeroError at the ratio below.
$raw = $this->getAttributes();
- $available = $raw['remaining'] ?? $this->availableAssets()->count();
+ $available = (int) ($raw['remaining'] ?? $this->availableAssets()->count());
if ($available === 0) {
return 0;
}
- // Guard the divisor: in principle available > 0 implies total > 0
- // (available is a subset of total via the RTD scope), but a data
- // anomaly — an asset counted by availableAssets but not by assets,
- // or a race between the two correlated subqueries — has surfaced as
- // DivisionByZeroError in production. Return 0 rather than throw.
- $total = $raw['assets_count'] ?? $this->assets()->count();
+ // Also guard the divisor. In principle available > 0 implies
+ // total > 0 (available is a subset of total via the RTD scope), but
+ // a data anomaly — an asset counted by availableAssets but not by
+ // assets, or a race between the two correlated withCount subqueries
+ // — has been observed in production. Return 0 rather than throw.
+ $total = (int) ($raw['assets_count'] ?? $this->assets()->count());
if ($total === 0) {
return 0;
}
@@ -293,7 +299,7 @@ class AssetModel extends SnipeModel
public function isDeletable()
{
return Gate::allows('delete', $this)
- && (($this->assets_count ?? $this->assets()->count()) === 0)
+ && ((int) ($this->assets_count ?? $this->assets()->count()) === 0)
&& ($this->deleted_at == '');
}
diff --git a/tests/Unit/AssetModelTest.php b/tests/Unit/AssetModelTest.php
index 23a8a77077..c52f7757a2 100644
--- a/tests/Unit/AssetModelTest.php
+++ b/tests/Unit/AssetModelTest.php
@@ -193,6 +193,19 @@ class AssetModelTest extends TestCase
$this->assertSame(0, $model->percentRemaining());
}
+ public function test_percent_remaining_returns_zero_when_eager_loaded_counts_are_string_zero(): void
+ {
+ // Some driver / cache paths return withCount values as string "0"
+ // instead of int 0. A strict `=== 0` guard misses those, letting the
+ // ratio run and throw DivisionByZeroError. The loose truthy check
+ // must catch both.
+ $category = Category::factory()->create(['category_type' => 'asset']);
+ $model = AssetModel::factory()->create(['category_id' => $category->id]);
+ $model->forceFill(['remaining' => '3', 'assets_count' => '0']);
+
+ $this->assertSame(0, $model->percentRemaining());
+ }
+
public function test_percent_remaining_returns_zero_when_runtime_total_is_zero_but_available_is_not(): void
{
// Same guard, exercised through the non-eager-loaded fallback path