Skip to content

Commit

Permalink
Do not crash when logging an invalid UTF-8 string (bsc#932014)
Browse files Browse the repository at this point in the history
- 3.1.43
  • Loading branch information
lslezak committed Jan 11, 2016
1 parent 6b7d6b4 commit 5db59f9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions package/yast2-ruby-bindings.changes
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Mon Jan 11 12:56:37 UTC 2016 - lslezak@suse.cz

- Do not crash when logging an invalid UTF-8 string (bsc#932014)
- 3.1.43

-------------------------------------------------------------------
Tue Dec 1 16:06:11 UTC 2015 - jreidinger@suse.com

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-ruby-bindings.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2-ruby-bindings
Version: 3.1.42
Version: 3.1.43
Url: https://github.com/yast/yast-ruby-bindings
Release: 0
BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand Down
19 changes: 18 additions & 1 deletion src/ruby/yast/logger.rb
@@ -1,6 +1,10 @@
require "yastx"
require "yast/builtins"

# FIXME: a workaround for https://github.com/yast/yast-ruby-bindings/issues/133
require "enc/encdb.so"
require "enc/trans/transdb.so"

module Yast
# @private
module_function def y2_logger_helper(level, args)
Expand All @@ -17,7 +21,20 @@ module Yast
end
end

res = Builtins.sformat(*args)
# replace invalid characters by the replacement symbol
# see https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
safe_args = args.map do |arg|
return arg unless arg.is_a?(::String)

if arg.encoding == Encoding::UTF_8
arg.scrub("�")
else
# broken strings might be passed as e.g. ASCII-8BIT and need to be recoded
arg.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�")
end
end

res = Builtins.sformat(*safe_args)
res.gsub!(/%/, "%%") # reescape all %
caller[caller_frame] =~ /(.+):(\d+):in `([^']+)'/
y2_logger(level, "Ruby", Regexp.last_match(1), Regexp.last_match(2).to_i, Regexp.last_match(3), res)
Expand Down
15 changes: 15 additions & 0 deletions tests/ruby/y2logger_spec.rb
Expand Up @@ -41,6 +41,21 @@ module Yast
expect(Yast).to receive(:y2milestone).with(Y2Logger::CALL_FRAME, TEST_MESSAGE)
@test_logger.info { TEST_MESSAGE }
end

it "does not crash when logging an invalid UTF-8 string" do
# do not process this string otherwise you'll get
invalid_utf8 = "invalid sequence: \xE3\x80"
# just make sure it is really an UTF-8 string
expect(invalid_utf8.encoding).to eq(Encoding::UTF_8)
expect { Yast.y2milestone(invalid_utf8) }.not_to raise_error
end

it "does not crash when logging ASCII string with invalid UTF-8" do
# do not process this string otherwise you'll get
invalid_ascii = "invalid sequence: \xE3\x80"
invalid_ascii.force_encoding(Encoding::ASCII)
expect { Yast.y2milestone(invalid_ascii) }.not_to raise_error
end
end

describe Yast::Logger do
Expand Down

0 comments on commit 5db59f9

Please sign in to comment.