Bug #13444
closedcompatibility breaking change: backticks is not evalueted as shell script call while in "unless" operator context
Description
Ruby versions from 2.0 to 2.3.x while executing shell script via backticks inside of "unless" construction had it executed and evalueted output as condition,
version 2.4 and onwards had condition evaluated as true but script is not executed.
I haven't find any note regarding such change in release-notes for 2.4.0 and 2.4.1
Steps to reproduce:
giving following code:
unless `touch abc.txt`
raise 'Error'
end
Expected:
cwd has a file abc.txt created
Actual:
cwd has no file abc.txt and no exception has been raised
Test-case (with version comparison via rvm)
file [test.rb]:
unless `touch test-#{RUBY_VERSION}`
raise 'Error'
end
begin
puts $?.success?
rescue StandardError => e
puts "rescued:#{e.message}"
end
s = `ls -l test-#{RUBY_VERSION}`
puts s.lines.length == 1 ? 'OK' : 'NOK'
exec:
$ rvm list
rvm rubies
ruby-2.0.0-p648 [ x86_64 ]
ruby-2.1.10 [ x86_64 ]
ruby-2.2.6 [ x86_64 ]
ruby-2.3.3 [ x86_64 ]
* ruby-2.4.0 [ x86_64 ]
=> ruby-2.4.1 [ x86_64 ]
$ rvm all --verbose do ruby test.rb
ruby-2.1.10: ruby 2.1.10p492 (2016-04-01 revision 54464) [x86_64-linux]
true
OK
ruby-2.2.6: ruby 2.2.6p396 (2016-11-15 revision 56800) [x86_64-linux]
true
OK
ruby-2.0.0-p648: ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-linux]
true
OK
ruby-2.3.3: ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
true
OK
ruby-2.4.0: ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
rescued:undefined method `success?' for nil:NilClass
NOK
ruby-2.4.1: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
rescued:undefined method `success?' for nil:NilClass
NOK
$ ls -l test-*
-rw-rw-r-- 1 user user 0 апр. 17 23:50 test-2.0.0
-rw-rw-r-- 1 user user 0 апр. 17 23:50 test-2.1.10
-rw-rw-r-- 1 user user 0 апр. 17 23:50 test-2.2.6
-rw-rw-r-- 1 user user 0 апр. 17 23:50 test-2.3.3
OS: Ubuntu 14.04
cat /etc/debian_version: jessie/sid
uname -isoprvm: Linux 3.13.0-116-generic #163-Ubuntu SMP Fri Mar 31 14:13:22 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Files
Updated by wanabe (_ wanabe) over 7 years ago
ruby
does not evaluate specific kinds of expressions at if
/unless
cond after r54775.
NODE_XSTR is one of them.
Updated by dikderoy (Roman Bulgakov) over 7 years ago
yes, You are right, it was my mistake to use it in this way, as this example is actually pointless and always True. all meaningful expression (as for ex. 1==Integer(echo 1
, 10)) are evaluated correctly. Probably you should close and delete this.
Updated by shyouhei (Shyouhei Urabe) over 7 years ago
Might be worth documenting.
Updated by dikderoy (Roman Bulgakov) over 7 years ago
Or putting a warning, because optimization actually changes behavior.
Updated by nobu (Nobuyoshi Nakada) over 7 years ago
- Status changed from Open to Closed
Applied in changeset trunk|r58398.
compile.c: wrong optimization
- compile.c (compile_branch_condition): expression which has side
effects should not be eliminated.
[ruby-core:80740] [Bug #13444]
Updated by nobu (Nobuyoshi Nakada) over 7 years ago
- Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: DONTNEED, 2.3: DONTNEED, 2.4: REQUIRED
Updated by nagachika (Tomoyuki Chikanaga) over 7 years ago
- Backport changed from 2.2: DONTNEED, 2.3: DONTNEED, 2.4: REQUIRED to 2.2: DONTNEED, 2.3: DONTNEED, 2.4: DONE
ruby_2_4 r58634 merged revision(s) 58398.
Updated by Eregon (Benoit Daloze) over 7 years ago
This is a bug, it does not execute part of the user program, even though the semantics are clearly defined.
Thanks for the fix nobu!
Maybe other case without side effects should warn, like if true
?