Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

avoid modifying frozen string from nil

ulfertsa9dfb35150e4
diff --git a/app/models/work_package/exporter/csv.rb b/app/models/work_package/exporter/csv.rb
index 208b44660e5..9d5566c6e95 100644
--- a/app/models/work_package/exporter/csv.rb
+++ b/app/models/work_package/exporter/csv.rb
@@ -95,8 +95,7 @@ class WorkPackage::Exporter::CSV < WorkPackage::Exporter::Base
csv_format_value(work_package, column)
end
- if !row.empty?
-
+ if row.any?
row << if work_package.description
work_package.description.squish
else
@@ -118,6 +117,10 @@ class WorkPackage::Exporter::CSV < WorkPackage::Exporter::Base
format_date(value)
when Time
format_time(value)
+ when nil
+ # ruby 2.7.1 will return a frozen string for nil.to_s which will cause an error when e.g. trying to
+ # force an encoding
+ ''
else
value
end
diff --git a/lib/redmine/codeset_util.rb b/lib/redmine/codeset_util.rb
index 87b373a27da..535c90d8b49 100644
--- a/lib/redmine/codeset_util.rb
+++ b/lib/redmine/codeset_util.rb
@@ -31,11 +31,15 @@ module Redmine
def self.replace_invalid_utf8(str)
return str if str.nil?
str.force_encoding('UTF-8')
- if !str.valid_encoding?
- str = str.encode('US-ASCII', invalid: :replace,
- undef: :replace, replace: '?').encode('UTF-8')
+ if str.valid_encoding?
+ str
+ else
+ str.encode('US-ASCII',
+ invalid: :replace,
+ undef: :replace,
+ replace: '?')
+ .encode('UTF-8')
end
- str
end
def self.to_utf8(str, encoding)
@@ -60,38 +64,16 @@ module Redmine
str
end
- def self.to_utf8_by_setting(str)
- return str if str.nil?
- to_utf8_by_setting_internal(str).force_encoding('UTF-8')
- end
-
- def self.to_utf8_by_setting_internal(str)
- return str if str.nil?
- str.force_encoding('ASCII-8BIT')
- return str if str.empty?
- return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
- str.force_encoding('UTF-8')
- encodings = Setting.repositories_encodings.split(',').map(&:strip)
- encodings.each do |encoding|
- begin
- str.force_encoding(encoding)
- utf8 = str.encode('UTF-8')
- return utf8 if utf8.valid_encoding?
- rescue
- # do nothing here and try the next encoding
- end
- end
- replace_invalid_utf8(str).force_encoding('UTF-8')
- end
-
def self.from_utf8(str, encoding)
str ||= ''
str.force_encoding('UTF-8')
if encoding.upcase != 'UTF-8'
- str = str.encode(encoding, invalid: :replace,
- undef: :replace, replace: '?')
+ str.encode(encoding,
+ invalid: :replace,
+ undef: :replace,
+ replace: '?')
else
- str = replace_invalid_utf8(str)
+ replace_invalid_utf8(str)
end
end
end
diff --git a/spec/features/work_packages/export_spec.rb b/spec/features/work_packages/export_spec.rb
index c9068c871d1..ed43210cdb3 100644
--- a/spec/features/work_packages/export_spec.rb
+++ b/spec/features/work_packages/export_spec.rb
@@ -154,6 +154,8 @@ describe 'work package export', type: :feature do
expect(page).to have_no_content(wp_2.description) # safeguard
+ sleep(0.5)
+
export!
expect(subject).not_to have_text(wp_1.description)
diff --git a/spec/lib/redmine/codeset_util_spec.rb b/spec/lib/redmine/codeset_util_spec.rb
deleted file mode 100644
index 23fec7db500..00000000000
--- a/spec/lib/redmine/codeset_util_spec.rb
+++ /dev/null
@@ -1,183 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2020 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2017 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See docs/COPYRIGHT.rdoc for more details.
-#++
-
-require 'spec_helper'
-
-describe Redmine::CodesetUtil do
- context 'with utf8 and latin1 setting', with_settings: { repositories_encodings: 'UTF-8,ISO-8859-1' } do
- let(:s1) do
- "Texte encod\xc3\xa9".tap do |s|
- s.force_encoding('UTF-8')
- end
- end
- let(:s2) do
- "Texte encod\xe9".tap do |s|
- s.force_encoding('ASCII-8BIT')
- end
- end
- let(:s3) do
- "Texte encod\xe9".tap do |s|
- s.force_encoding('UTF-8')
- end
- end
-
- it 'transforms from 8bit' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s2))
- .to eql s1
-
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s2).encoding.to_s)
- .to eql 'UTF-8'
- end
-
- it 'transforms from utf8' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s3))
- .to eql s1
-
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s3).encoding.to_s)
- .to eql 'UTF-8'
- end
- end
-
- context 'with utf-8 and euc jp setting', with_settings: { repositories_encodings: 'UTF-8,EUC-JP' } do
- let(:s1) do
- "\xe3\x83\xac\xe3\x83\x83\xe3\x83\x89\xe3\x83\x9e\xe3\x82\xa4\xe3\x83\xb3".tap do |s|
- s.force_encoding('UTF-8')
- end
- end
- let(:s2) do
- "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3".tap do |s|
- s.force_encoding('ASCII-8BIT')
- end
- end
- let(:s3) do
- "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3".tap do |s|
- s.force_encoding('UTF-8')
- end
- end
-
- it 'transforms from 8bit' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s2))
- .to eql s1
-
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s2).encoding.to_s)
- .to eql 'UTF-8'
- end
-
- it 'transforms from utf8' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s3))
- .to eql s1
-
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s3).encoding.to_s)
- .to eql 'UTF-8'
- end
- end
-
- context 'with latin1 only setting', with_settings: { repositories_encodings: 'ISO-8859-1' } do
- let(:s1) do
- "\xc3\x82\xc2\x80".tap do |s|
- s.force_encoding('UTF-8')
- end
- end
- let(:s2) do
- "\xC2\x80".tap do |s|
- s.force_encoding('ASCII-8BIT')
- end
- end
- let(:s3) do
- "\xC2\x80".tap do |s|
- s.force_encoding('UTF-8')
- end
- end
-
- it 'transforms from 8bit' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s2))
- .to eql s1
-
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s2).encoding.to_s)
- .to eql 'UTF-8'
- end
-
- it 'transforms from utf8' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s3))
- .to eql s1
-
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s3).encoding.to_s)
- .to eql 'UTF-8'
- end
- end
-
- context 'with blank setting', with_settings: { repositories_encodings: '' } do
- it 'transforms blank string' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(''))
- .to eql ''
- end
-
- it 'transforms nil' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(nil))
- .to eql nil
- end
-
- context 'with an invalid utf8 sequences' do
- let(:s1) do
- "Texte encod\xe9 en ISO-8859-1.".tap do |s|
- s.force_encoding('ASCII-8BIT')
- end
- end
-
- it 'returns a valid utf-8 encoding' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s1).encoding.to_s)
- .to eql 'UTF-8'
- end
-
- it 'strips out the invalid char' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s1))
- .to eql 'Texte encod? en ISO-8859-1.'
- end
- end
- end
-
- context 'with ja jis setting', with_settings: { repositories_encodings: 'ISO-2022-JP' } do
- context 'with an invalid utf8 sequences' do
- let(:s1) do
- "test\xb5\xfetest\xb5\xfe".tap do |s|
- s.force_encoding('ASCII-8BIT')
- end
- end
-
- it 'returns a valid utf-8 encoding' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s1).encoding.to_s)
- .to eql 'UTF-8'
- end
-
- it 'strips out the invalid char' do
- expect(Redmine::CodesetUtil.to_utf8_by_setting(s1))
- .to eql 'test??test??'
- end
- end
- end
-end