From 71b6a4ba85150c88414f50ac4965cab71021202d Mon Sep 17 00:00:00 2001 From: James Lopez Date: Thu, 20 Apr 2017 16:10:33 +0200 Subject: [PATCH] Return empty string on UTF8 conversion with U_STRING_NOT_TERMINATED_WARNING error --- changelogs/unreleased/fix-encoding-issue.yml | 4 ++++ lib/gitlab/git/encoding_helper.rb | 8 +++++++- spec/lib/gitlab/git/encoding_helper_spec.rb | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/fix-encoding-issue.yml diff --git a/changelogs/unreleased/fix-encoding-issue.yml b/changelogs/unreleased/fix-encoding-issue.yml new file mode 100644 index 00000000000..5fa8ab46e72 --- /dev/null +++ b/changelogs/unreleased/fix-encoding-issue.yml @@ -0,0 +1,4 @@ +--- +title: Fix encoding issue exporting a project +merge_request: +author: diff --git a/lib/gitlab/git/encoding_helper.rb b/lib/gitlab/git/encoding_helper.rb index e57d228e688..f918074cb14 100644 --- a/lib/gitlab/git/encoding_helper.rb +++ b/lib/gitlab/git/encoding_helper.rb @@ -40,7 +40,13 @@ module Gitlab def encode_utf8(message) detect = CharlockHolmes::EncodingDetector.detect(message) if detect - CharlockHolmes::Converter.convert(message, detect[:encoding], 'UTF-8') + begin + CharlockHolmes::Converter.convert(message, detect[:encoding], 'UTF-8') + rescue ArgumentError => e + Rails.logger.warn("Ignoring error converting #{detect[:encoding]} into UTF8: #{e.message}") + + '' + end else clean(message) end diff --git a/spec/lib/gitlab/git/encoding_helper_spec.rb b/spec/lib/gitlab/git/encoding_helper_spec.rb index 27bcc241b82..f6ac7b23d1d 100644 --- a/spec/lib/gitlab/git/encoding_helper_spec.rb +++ b/spec/lib/gitlab/git/encoding_helper_spec.rb @@ -56,6 +56,10 @@ describe Gitlab::Git::EncodingHelper do expect(r.encoding.name).to eq('UTF-8') end end + + it 'returns empty string on conversion errors' do + expect { ext_class.encode_utf8('') }.not_to raise_error(ArgumentError) + end end describe '#clean' do