Force UTF-8 encoding of lsb_release output

It's possible that the output may be encoded ASCII-8BIT, which cannot be automatically encoded to UTF-8 by Ruby's JSON library.

In this particular case, elementaryOS 6.1 returns `Jólnir` as a part of the lsb_release Description field.
Ruby's IO.popen would return this whole output in ASCII-8BIT.
When this description field was passed to homebrew-bundle to be written in a JSON lockfile,
Ruby throws an exception because it could not automatically reencode from ASCII-8BIT to UTF-8.

This may actually affect other output of the calls to the underlying `Utils.popen_read` but this may be the first thing to trigger it since
JSON requires UTF-8 output and nothing else using that function may require UTF-8.

Fixes #12689 12689
Fixes Homebrew/homebrew-bundle#1043
This commit is contained in:
Colin Dean 2022-01-09 21:12:52 -05:00 committed by GitHub
parent 7fc2159fcd
commit b632123d6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,7 @@ module OS
def os_version
if which("lsb_release")
lsb_info = Utils.popen_read("lsb_release", "-a")
description = lsb_info[/^Description:\s*(.*)$/, 1]
description = lsb_info[/^Description:\s*(.*)$/, 1].force_encoding("UTF-8")
codename = lsb_info[/^Codename:\s*(.*)$/, 1]
if codename.blank? || (codename == "n/a")
description