Skip to content

systemd: fix SystemdMeter parsing when falling back to systemctl show#2037

Open
dengzhongyuan365-dev wants to merge 1 commit into
htop-dev:mainfrom
dengzhongyuan365-dev:fix/issue-1982-systemdmeter-newline
Open

systemd: fix SystemdMeter parsing when falling back to systemctl show#2037
dengzhongyuan365-dev wants to merge 1 commit into
htop-dev:mainfrom
dengzhongyuan365-dev:fix/issue-1982-systemdmeter-newline

Conversation

@dengzhongyuan365-dev

Copy link
Copy Markdown

Summary

  • Fix SystemdMeter displaying (?/? failed) (?/? jobs) when htop falls back to systemctl show for parsing systemd state

Root Cause

fgets() preserves trailing newlines in the buffer, so strtoul() leaves endptr pointing to '\n' instead of '\0'. The validation check *endptr == '\0' fails, causing all numeric fields to remain at INVALID_VALUE.

This only affects the systemctl show fallback path (used when sd_bus_open_user fails, e.g. under file capabilities). The sd_bus path is unaffected.

Note: SystemState= was already handled correctly — it explicitly strips the newline with strchr(lineBuffer, '\n').

Changes

File Change
linux/SystemdMeter.c Accept '\n' as valid end-of-value terminator in addition to '\0' (4 occurrences)

Test Plan

  • Build passes with --enable-werror
  • Verify on a system where sd_bus_open_user fails (e.g. Gentoo with filecaps), or test by temporarily forcing updateViaExec path

Fixes #1982

When htop falls back to parsing `systemctl show` output via
fgets/strtoul, the trailing newline preserved by fgets causes
strtoul's endptr to point to '\n' instead of '\0'. The value
check only tested for '\0', so all numeric fields stayed at
INVALID_VALUE, displaying as "(?/? failed) (?/? jobs)".

Accept both '\n' and '\0' as valid end-of-value terminators.

Fixes htop-dev#1982
@dengzhongyuan365-dev dengzhongyuan365-dev marked this pull request as ready for review July 1, 2026 07:14
@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: a7a00462-a0ef-44d6-87d3-967306ec6697

📥 Commits

Reviewing files that changed from the base of the PR and between 1ef4226 and 5839bca.

📒 Files selected for processing (1)
  • linux/SystemdMeter.c

📝 Walkthrough

Walkthrough

Changes

This change adjusts numeric parsing validation in updateViaExec within SystemdMeter.c. After calling strtoul on values for NFailedUnits, NNames, and NJobs, the code now accepts a result whose endptr points to either a newline character or the string terminator, rather than requiring only the null terminator. The UINT_MAX range check and assignment logic are unchanged.

Assessment against linked issues

Objective Addressed Explanation
Fix SystemdMeter showing "(?/? failed) (?/? jobs)" for user instances (#1982)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Poem

A newline snuck in, systemctl's small quirk,
The parser once choked, refusing to work.
Now \n or \0, both welcomed with grace,
The failed units count back in their place. 🐰


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

SystemdMeter for user shows (?/? failed) (?/? jobs)

1 participant