Backport #8290

String#inspect should not represent NUL as "\0" when octal digits may follow.

Added by Akinori MUSHA over 2 years ago. Updated over 2 years ago.

[ruby-core:54458]
Status:Closed
Priority:Normal
Assignee:Tomoyuki Chikanaga

Description

Starting from Ruby 2.0.0, String#inspect represents the NUL character as "\0" but it is not appropriate at least when octal digits follow.
Not only is it confusing to human eyes but it also breaks the characteristic of String#inspect that you can "eval" the result to get the original string, though not guaranteed.

% ruby -ve 'p "\0"+"12"'

ruby 1.8.7 (2012-10-12 patchlevel 371) [x86_64-freebsd9]

"\00012"
=> OK

ruby 1.9.3p409 (2013-04-04) [x86_64-freebsd9]

"\u000012"
=> OK

ruby 2.0.0p145 (2013-04-17 revision 40339) [x86_64-freebsd9]

"\012"
=> Confusing

ruby 2.1.0dev (2013-04-19 trunk 40366) [x86_64-freebsd9]

"\012"
=> Confusing

Attached is a patch that fixes this issue.

0001-string.c-rb_str_inspect-NUL-should-not-be-represente.patch Magnifier (1.81 KB) Akinori MUSHA, 04/19/2013 03:51 PM

Associated revisions

Revision 40414
Added by Akinori MUSHA over 2 years ago

Add references.

  • string.c (rb_str_inspect): NUL should not be represented as "\0" when octal digits may follow. [Bug #8290]

Revision 40423
Added by Yui NARUSE over 2 years ago

  • string.c (rb_str_inspect): refix r40413, on Ruby 1.9 usual character escape uses hex/Unicode escapes, so fix to use Unicode escape on Unicode strings and hex on others. [Bug #8290]

Revision 40488
Added by Tomoyuki Chikanaga over 2 years ago

merge revision(s) 40413,40414,40415,40423: [Backport #8290]

* string.c (rb_str_inspect): NUL should not be represented as
  "\0" when octal digits may follow.

* string.c (rb_str_inspect): NUL should not be represented as "\0"
  when octal digits may follow.   [Bug #8290]

* test/ruby/test_module.rb
  (TestModule#test_const_get_invalid_name)
  (test_const_defined_invalid_name): Fix expected values.

* string.c (rb_str_inspect): refix r40413, on Ruby 1.9 usual character
  escape uses hex/Unicode escapes, so fix to use Unicode escape on
  Unicode strings and hex on others.  [Bug #8290]

History

#1 Updated by Akinori MUSHA over 2 years ago

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

This issue was solved with changeset r40414.
Akinori, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


Add references.

  • string.c (rb_str_inspect): NUL should not be represented as "\0" when octal digits may follow. [Bug #8290]

#2 Updated by Yui NARUSE over 2 years ago

  • Category deleted (core)
  • Status changed from Closed to Assigned
  • Target version deleted (next minor)
  • Tracker changed from Bug to Backport
  • Project changed from Ruby trunk to Backport200

#3 Updated by Tomoyuki Chikanaga over 2 years ago

  • Assignee set to Tomoyuki Chikanaga

#4 Updated by Tomoyuki Chikanaga over 2 years ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r40488.
Akinori, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


merge revision(s) 40413,40414,40415,40423: [Backport #8290]

* string.c (rb_str_inspect): NUL should not be represented as
  "\0" when octal digits may follow.

* string.c (rb_str_inspect): NUL should not be represented as "\0"
  when octal digits may follow.   [Bug #8290]

* test/ruby/test_module.rb
  (TestModule#test_const_get_invalid_name)
  (test_const_defined_invalid_name): Fix expected values.

* string.c (rb_str_inspect): refix r40413, on Ruby 1.9 usual character
  escape uses hex/Unicode escapes, so fix to use Unicode escape on
  Unicode strings and hex on others.  [Bug #8290]

Also available in: Atom PDF