Skip to content

fix(disk): do not divide by zero on a filesystem with no capacity - #1062

Open
karlitschek wants to merge 1 commit into
masterfrom
fix/disk-info-zero-capacity
Open

fix(disk): do not divide by zero on a filesystem with no capacity#1062
karlitschek wants to merge 1 commit into
masterfrom
fix/disk-info-zero-capacity

Conversation

@karlitschek

Copy link
Copy Markdown
Member

getDiskInfo() computed the used percentage as used * 100 / Blocks, using the block count straight out of df with no guard. A filesystem reporting zero total blocks therefore raised DivisionByZeroError.

That is an Error rather than an Exception, so the catch (RuntimeException) around the df call did not stop it and nothing else in the chain did either: Os::getDiskData() and Os::getDiskInfo() just forward. The result was a 500 for the whole monitoring page and the whole /api/v1/diskdata response, not a single missing row.

Whether it triggers depends on the df implementation. coreutils hides zero-block filesystems unless asked with -a, but busybox df lists them, so this is reachable on Alpine based images — an nsfs mount from snapd is enough.

A filesystem with no capacity is now reported as 0% used, keeping the row rather than dropping it silently. The used figure is also clamped at zero, so a filesystem reporting more available than total can no longer produce a negative size and a negative percentage.

Both backends carried the identical expression and both are fixed. The FreeBSD one had no disk tests at all, so it gains its first.

Verified by running the new tests against the unfixed collectors: both error with DivisionByZeroError at Linux.php:229 and FreeBSD.php:209, and pass after the change. The existing expectations are untouched — a normal filesystem still reports 16.13%.

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

getDiskInfo() computed the used percentage as `used * 100 / Blocks`, using
the block count straight out of `df` with no guard. A filesystem reporting
zero total blocks therefore raised DivisionByZeroError.

That is an Error rather than an Exception, so the `catch (RuntimeException)`
around the df call did not stop it and nothing else in the chain did either:
Os::getDiskData() and Os::getDiskInfo() just forward. The result was a 500
for the whole monitoring page and the whole /api/v1/diskdata response, not a
single missing row.

Whether it triggers depends on the df implementation. coreutils hides
zero-block filesystems unless asked with -a, but busybox df lists them, so
this is reachable on Alpine based images — an nsfs mount from snapd is
enough.

A filesystem with no capacity is now reported as 0% used, keeping the row
rather than dropping it silently. The used figure is also clamped at zero, so
a filesystem reporting more available than total can no longer produce a
negative size and a negative percentage.

Both backends carried the identical expression and both are fixed. The
FreeBSD one had no disk tests at all, so it gains its first.

Verified by running the new tests against the unfixed collectors: both error
with DivisionByZeroError at Linux.php:229 and FreeBSD.php:209, and pass after
the change. The existing expectations are untouched — a normal filesystem
still reports 16.13%.

Signed-off-by: Frank Karlitschek <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant