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

Force encoding with timezones that include umlauts if zone encoding is IBM437 #1781

Merged
merged 5 commits into from Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/ohai/plugins/timezone.rb
Expand Up @@ -21,5 +21,25 @@
collect_data(:default) do
time Mash.new unless time
time[:timezone] = Time.now.getlocal.zone

# Windows in German display language outputs LATIN1 bytes for .zone, but marks them as
# IBM437, which somehow fails any attempt at conversion to other encodings when
# ä is present, as in the timezone name "Mitteleuropäische Zeit" (Central Europe Time)
#
# Windows-1252 is the legacy encoding for Windows for German that actually
# translates (ISO-8859-1 works as well), but going with the more correct
# encoding name for Windows' implementation of Latin-1
#
# References
# * [Code Page 437/IBM437](https://en.wikipedia.org/wiki/Code_page_437)
# * [ISO/IEC 8859-1](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
# * [Windows-1252](https://en.wikipedia.org/wiki/Windows-1252)
if time[:timezone].encoding == Encoding::IBM437
# Assume encoding is WINDOWS_1252
time[:timezone] = time[:timezone].force_encoding(Encoding::WINDOWS_1252)
# Re-encode in UTF_8. Note: If other encodings have problems converting
# it might be worth re-encode everything in UTF_8.
time[:timezone] = time[:timezone].encode(Encoding::UTF_8)
end
end
end
15 changes: 15 additions & 0 deletions lib/ohai/plugins/vmware.rb
Expand Up @@ -53,6 +53,21 @@ def get_vm_attributes(vmtools_path)
# to attribute "vmware[:<parameter>]"
%w{hosttime speed sessionid balloon swap memlimit memres cpures cpulimit}.each do |param|
vmware[param] = from_cmd([vmtools_path, "stat", param])
if param == "hosttime" && vmtools_path.include?("Program Files")
# popen and %x return stdout encoded as IBM437 in Windows but in a string marked
# UTF-8. The string doesn't throw an exception when encoded to "UTF-8" but
# displays [?] character in Windows without this.
#
# .force_encoding(Encoding::ISO_8859_1) causes the character to be dropped
# and .force_encoding(Encoding::Windows_1252) displays the „ character in place
# of an ä. .force_encoding(Encoding::IBM437) allows for the correct characters
tpowell-progress marked this conversation as resolved.
Show resolved Hide resolved
# to be displayed.
#
# Note:
# * this is broken for at least Ruby 2.7 through 3.1.3
# * confirmed that this is broken on Windows Server 2022
vmware[param] = vmware[param].force_encoding(Encoding::IBM437).encode("UTF-8")
end
if /UpdateInfo failed/.match?(vmware[param])
vmware[param] = nil
end
Expand Down