Fix Encoding::CompatibilityError in hostnamectl plugin - #1964
Merged
tpowell-progress merged 1 commit intoAug 11, 2026
Conversation
Contributor
|
Hi, this was submitted by me. The broken ohai produces a truncated 'ohai hostnamectl' output: ohai hostnamectl the attached fix corrects that, and removes the icon as intended: { Karl Amrhein |
Mixlib::ShellOut returns stdout as ASCII-8BIT, so matching the UTF-8 icon-stripping regexp against it raises as soon as hostnamectl emits a multibyte character, truncating the attribute mash. Coerce the encoding before the gsub, and make the existing emoji spec fixtures binary so they exercise the path they were written to cover. Signed-off-by: Karl Amrhein <karlamrhein@gmail.com>
tpowell-progress
force-pushed
the
fix-hostnamectl-encoding
branch
from
August 11, 2026 20:51
00a3b86 to
a254f44
Compare
tpowell-progress
approved these changes
Aug 11, 2026
tpowell-progress
added a commit
that referenced
this pull request
Aug 12, 2026
Mixlib::ShellOut returns stdout as ASCII-8BIT, so matching the UTF-8 icon-stripping regexp against it raises as soon as hostnamectl emits a multibyte character, truncating the attribute mash. Coerce the encoding before the gsub, and make the existing emoji spec fixtures binary so they exercise the path they were written to cover. (cherry picked from commit 54b17dd) Signed-off-by: Karl Amrhein <karlamrhein@gmail.com> Signed-off-by: Thomas Powell <104777878+tpowell-progress@users.noreply.github.com> Co-authored-by: slac-chef-ci-cd <karlamrhein+cicd@gmail.com> Co-authored-by: Karl Amrhein <karlamrhein@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
Hostnamectlplugin crashes withEncoding::CompatibilityErrorwheneverhostnamectlemits a non-ASCII character, silently truncating the attributemash to whatever keys preceded the offending line.
The plugin already had eight test cases covering exactly this input — one per
chassis icon, added alongside #1909. All eight pass against the broken code.
The heredoc fixtures are UTF-8-tagged, while
Mixlib::ShellOutreturns stdout asASCII-8BIT, so the specs never exercised the encoding path they were writtento cover.
Verified on Ubuntu 24.04.4, ohai 18.2.20 (Chef Infra Client 18.11.11):
A UTF-8 regexp is only compatible with an
ASCII-8BITstring while that stringholds pure 7-bit bytes. The first line containing a multibyte character — for
example
Chassis: laptop 💻— raises, aborting the parse loop.Ohai::Runner#run_pluginre-raises
Ohai::Exceptions::Errorbut catches bareExceptionand logs at tracelevel, so the plugin's partial mash is retained and the failure surfaces only as
missing attributes:
This is silent in a chef-client run — cookbooks reading
node['hostnamectl']['chassis'](laptop detection, disk-encryption enforcement, and similar) misbehave with no error
surfaced.
Changes
Plugin — coerce the encoding before the icon-stripping regexp runs.
dupbecauseforce_encodingmutates theMixlib::ShellOutobject's own@stdout;scrubguardsagainst genuinely invalid byte sequences under a non-UTF-8 locale, and its U+FFFD
replacements are removed by the existing
gsubon the next line.Spec — make the fixtures binary so they reflect what
Mixlib::ShellOutactuallyreturns. This converts the eight existing emoji cases into genuine regression tests.
With the spec change alone (plugin reverted), the eight emoji cases fail.
With both changes, the full suite is green and
rake stylereports no offenses.Backport
18-stablecarries the same code and ships in Chef Infra Client 18.11.11, so thisneeds a cherry-pick there as well.
Types of changes
Checklist