Project

General

Profile

Bug #1929

str.dup.force_encodingが元のstrに影響を与えることがある

Added by kazuhiko (Kazuhiko Shiozaki) over 10 years ago. Updated over 8 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
ruby -v:
ruby 1.9.2dev (2009-08-09 trunk 24484) [x86_64-linux]
Backport:
[ruby-dev:39068]

Description

=begin
かずひこです。

tDiary trunk 3514をruby-trunkで動かすとカテゴリプラグインで日本語カテゴリのリンクをたどると
incompatible character encodings: ASCII-8BIT and UTF-8 (Encoding::CompatibilityError)
になります。
http://www.cozmixng.org/retro/projects/tdiary/tickets/171

つきつめていくと、どうやら ERB::Util.url_encodeの
def url_encode(s)
s.to_s.dup.force_encoding("ASCII-8BIT").gsub(/[a-zA-Z0-9_-.]/n) {
sprintf("%%%02X", $&.unpack("C")[0])
}
end
で、元のsが影響をうけているようなのです。
http://vvvvvv.sakura.ne.jp/ds14050/diary/20090223.html#p01

例えば、url_encodeの呼び出し側を次のように修正するとエラーが再現しません。

  • a = @category.map {|c| "category=#{u c}"}.join(';')
  • a = @category.map {|c| "category=#{u ''+c}"}.join(';')

もしくは、url_encodeを次のように修正するとエラーが再現しません。
def url_encode(s)
"#{s}".force_encoding("ASCII-8BIT").gsub(/[a-zA-Z0-9_-.]/n) {
sprintf("%%%02X", $&.unpack("C")[0])
}
end

短い再現コードが書けなかったのですが、どうぞよろしくお願いします。
かずひこ
=end

History

#1

Updated by znz (Kazuhiro NISHIYAMA) over 10 years ago

=begin
まだ長いように思いますが、単体で再現できるものができたので送っておきます。

% cat c.rb
#!/usr/bin/env ruby-trunk
# -- coding: utf-8 --
require 'pstore'
require 'cgi'
require 'erb'

def cache_file(c)
'c.db'
end

params = CGI::parse("category=%E3%81%A6%E3%81%99%E3%81%A8")
categories = params['category']
categorized = {}
$dump_categorized = proc do |file, line|
$stderr.puts [file, line, categories.map{|e|[e[0].object_id, e[0].encoding]}].inspect
$stderr.puts [file, line, categorized.map{|e|[e[0].object_id, e[0].encoding]}].inspect
end
$dump_categorized[FILE, LINE]
categories.each do |c|
PStore.new(cache_file(c)).transaction do |db|
categorized[c] = db['category']
db.abort
end
end

$dump_categorized[FILE, LINE]
categorized.keys.each do |c|
PStore.new(cache_file(c)).transaction do |db|
db['category'] = {} unless db.root?('category')
db['category'].update(categorized[c] || {})
end
end
$dump_categorized[FILE, LINE]
categories.map{|e|ERB::Util.u(e)}
$dump_categorized[FILE, LINE]
% ruby-trunk c.rb -v
["c.rb", 18, [[10938200, #Encoding:UTF-8]]]
["c.rb", 18, []]
["c.rb", 26, [[10937000, #Encoding:UTF-8]]]
["c.rb", 26, [[10937100, #Encoding:UTF-8]]]
["c.rb", 33, [[10935120, #Encoding:UTF-8]]]
["c.rb", 33, [[10937100, #Encoding:UTF-8]]]
["c.rb", 35, [[10932440, #Encoding:UTF-8]]]
["c.rb", 35, [[10937100, #Encoding:ASCII-8BIT]]]
% ruby-trunk -v
ruby 1.9.2dev (2009-08-07 trunk 24439) [x86_64-linux]
%
=end

#2

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r24509.
=end

Also available in: Atom PDF