https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112011-11-15T16:53:20ZRuby Issue Tracking SystemRuby master - Bug #5637: warnings of shellescapehttps://bugs.ruby-lang.org/issues/5637?journal_id=222222011-11-15T16:53:20Zznz (Kazuhiro NISHIYAMA)
<ul><li><strong>ruby -v</strong> changed from <i>ruby 2.0.0dev (2011-11-15 trunk 33753) [x86_64-linux]</i> to <i>-</i></li></ul><p>西山和広です。</p>
<p>redmine の方で書くと消えてしまうようなので、メールで書き直します。</p>
<p>Shellwords.shellescape で警告が出ます。</p>
<p>% ./ruby -v -r shellwords -e 'p Shellwords.shellescape("\u3042")'<br>
ruby 2.0.0dev (2011-11-15 trunk 33753) [x86_64-linux]<br>
/home/chkbuild/tmp/build/ruby-trunk/20111114T222552Z/lib/ruby/1.9.1/shellwords.rb:86: warning: regexp match /.../n against to UTF-8 string<br>
/home/chkbuild/tmp/build/ruby-trunk/20111114T222552Z/lib/ruby/1.9.1/shellwords.rb:86: warning: regexp match /.../n against to UTF-8 string<br>
/home/chkbuild/tmp/build/ruby-trunk/20111114T222552Z/lib/ruby/1.9.1/shellwords.rb:86: warning: regexp match /.../n against to UTF-8 string<br>
"\あ"</p>
<p>エスケープ結果も変だと思います。<br>
エスケープ結果を 1.8.7 にあわせるのなら以下のパッチで<br>
どうでしょうか。</p>
<p>diff --git a/lib/shellwords.rb b/lib/shellwords.rb<br>
index 5d6ba75..78331a7 100644<br>
--- a/lib/shellwords.rb<br>
+++ b/lib/shellwords.rb<br>
@@ -79,11 +79,11 @@ module Shellwords<br>
# An empty argument will be skipped, so return empty quotes.<br>
return "''" if str.empty?</p>
<ul>
<li>str = str.dup</li>
</ul>
<ul>
<li>
<p>str = str.dup.force_encoding("ASCII-8BIT")</p>
<a name="Process-as-a-single-byte-sequence-because-not-all-shell"></a>
<h1 >Process as a single byte sequence because not all shell<a href="#Process-as-a-single-byte-sequence-because-not-all-shell" class="wiki-anchor">¶</a></h1>
<a name="implementations-are-multibyte-aware"></a>
<h1 >implementations are multibyte aware.<a href="#implementations-are-multibyte-aware" class="wiki-anchor">¶</a></h1>
</li>
</ul>
<ul>
<li>str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\1")</li>
</ul>
<ul>
<li>
<p>str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\1")</p>
<a name="A-LF-cannot-be-escaped-with-a-backslash-because-a-backslash-LF"></a>
<h1 >A LF cannot be escaped with a backslash because a backslash + LF<a href="#A-LF-cannot-be-escaped-with-a-backslash-because-a-backslash-LF" class="wiki-anchor">¶</a></h1>
<a name="combo-is-regarded-as-line-continuation-and-simply-ignored"></a>
<h1 >combo is regarded as line continuation and simply ignored.<a href="#combo-is-regarded-as-line-continuation-and-simply-ignored" class="wiki-anchor">¶</a></h1>
</li>
</ul>
<p>diff --git a/test/test_shellwords.rb b/test/test_shellwords.rb<br>
index d48a888..cbc5043 100644<br>
--- a/test/test_shellwords.rb<br>
+++ b/test/test_shellwords.rb<br>
@@ -36,4 +36,8 @@ class TestShellwords < Test::Unit::TestCase<br>
shellwords(bad_cmd)<br>
end<br>
end<br>
+</p>
<ul>
<li>def test_shellescape_utf8_string</li>
<li>assert_equal "\\343\\201\\202", shellescape("\u3042")</li>
<li>end<br>
end</li>
</ul>
<p>--<br>
|ZnZ(ゼット エヌ ゼット)<br>
|西山和広(Kazuhiro NISHIYAMA)</p> Ruby master - Bug #5637: warnings of shellescapehttps://bugs.ruby-lang.org/issues/5637?journal_id=222382011-11-16T20:24:59Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul><li><strong>Assignee</strong> set to <i>knu (Akinori MUSHA)</i></li></ul> Ruby master - Bug #5637: warnings of shellescapehttps://bugs.ruby-lang.org/issues/5637?journal_id=229262011-12-22T19:51:30Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>いろいろ考えたんですが、単に //n フラグを削るだけにしようと思います。</p>
<p>・1.8: 一律バイナリとして扱うのは、文字列にencoding情報がなく$KCODEもあてにならないため、やむを得ない仕様だった。(この事情は1.9+には当てはまらない)<br>
・1.9: 1.9.3の今までずっとこの挙動だった。警告はバグ(//nの修正漏れ)として消すが、挙動については非互換を招くので変えない。<br>
・2.0: 文字列の使い道(渡すシェルのlocaleなど)を知っているのは呼出元だけだが、1.9+では呼出元がASCII-8BITも含め適切にencodeすることができるので、shellescapeがそのencodingを尊重する現在の挙動こそ(たまたまだが)望ましく、変える必要はない。</p>
<p>警告の出しようもない(SJISなら云々とかもシェルのlocaleをSJISにするなど分かってやっている場合は害)ので、余計なことはせず、ドキュメントにだけ注記するつもりです。</p> Ruby master - Bug #5637: warnings of shellescapehttps://bugs.ruby-lang.org/issues/5637?journal_id=230282012-01-01T12:26:28Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li><li><strong>% Done</strong> changed from <i>0</i> to <i>100</i></li></ul><p>This issue was solved with changeset r34166.<br>
Kazuhiro, thank you for reporting this issue.<br>
Your contribution to Ruby is greatly appreciated.<br>
May Ruby be with you.</p>
<hr>
<ul>
<li>lib/shellwords.rb (Shellwords#shellescape): Drop the //n flag<br>
that only causes warnings with no real effect. [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: warnings of shellescape (Closed)" href="https://bugs.ruby-lang.org/issues/5637">#5637</a>]</li>
</ul> Ruby master - Bug #5637: warnings of shellescapehttps://bugs.ruby-lang.org/issues/5637?journal_id=286832012-08-07T02:48:20Zdariocravero (Darío Cravero)dario@qinnova.com.ar
<ul></ul><p>Hi,</p>
<p>Thanks for this patch!.. :)</p>
<p>One question though, from comment #3 it's not clear if it's safe to use it in 1.9.3. This is what Google Translator gave me:</p>
<p>"1.9: this behavior was all the way to 1.9.3 now. Turn off warning but does not change as a bug (missing fix of / / n), because the behavior leads to incompatibility."</p>
<p>However, I've applied it and, as expected, I don't see the warning anymore. Still, can you just confirm there're no side effects to this on 1.9.3?</p>
<p>Thanks a million!..</p> Ruby master - Bug #5637: warnings of shellescapehttps://bugs.ruby-lang.org/issues/5637?journal_id=287272012-08-08T20:25:04Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>As I documented, it's all up to how you use the resulted string.</p>
<p>If you are going to pass it to a shell that lacks support for the encoding of the string, then you should probably encode the original string in ASCII-8BIT before shell-escaping with shellescape() to get a byte-by-byte escape to make sure the shell won't find a metacharacter inside a multibyte character.</p>
<p>UTF-8 multibyte characters do not contain any ASCII character by design anyway, so most people in the everything-is-UTF-8 world don't even have to care about this.</p>
<p>But, for example, when you have to run a program passing a Shift_JIS string via a shell under a non-Shift_JIS locale, you'd probably have to compose the command line in the ASCII-8BIT encoding so that all shell metacharacters that may appear in Shift_JIS multibyte characters are properly escaped.</p>