From: "mame (Yusuke Endoh)" Date: 2012-10-31T01:38:19+09:00 Subject: [ruby-core:48610] [ruby-trunk - Bug #5530][Assigned] SEEK_SET malfunctions when used with 'append' File.open mode Issue #5530 has been updated by mame (Yusuke Endoh). Status changed from Feedback to Assigned Assignee set to kosaki (Motohiro KOSAKI) Target version set to 2.0.0 kosaki (Motohiro KOSAKI) wrote: > Which documentation bring you confusing? IO.new does not say that append mode ignores fseek. That said, I don't like to make rdoc messy. How about adding just "See fopen(3) man pages"? Kosaki-san, may I leave this to you? -- Yusuke Endoh ---------------------------------------- Bug #5530: SEEK_SET malfunctions when used with 'append' File.open mode https://bugs.ruby-lang.org/issues/5530#change-32022 Author: jduck (Joshua J. Drake) Status: Assigned Priority: Normal Assignee: kosaki (Motohiro KOSAKI) Category: Target version: 2.0.0 ruby -v: all The following code demonstrates the issue. As documented, IO#seek says "SEEK_SET" will move to a position relative to the start of the file. Using 'ab', it doesn't actually seek where it should. It's possible the documentation is just wrong here, either the code or documentation should change though. #!/usr/bin/env ruby modes = [ 'ab', 'wb', 'w+b', 'r+b' ] filename = '/tmp/test.txt' desired = "AABBBBAA" modes.each { |mode| File.unlink(filename) rescue nil File.open(filename, "wb") { |fd| fd.write('AAAAAAAA') } File.open(filename, mode) { |fd| fd.seek(2, IO::SEEK_SET) fd.write("BBBB") fd.close } data = '' File.open(filename, "rb") { |fd| data = fd.read(fd.stat.size) } puts "%3s #{data.inspect}" % mode } File.unlink(filename) puts "" Output:

fear:0:ruby$ rvm all ruby -v file_mode_test.rb
ruby 1.8.6 (2010-09-02 patchlevel 420) [x86_64-linux]
 ab "AAAAAAAABBBB"
 wb "\000\000BBBB"
w+b "\000\000BBBB"
r+b "AABBBBAA"

ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux]
 ab "AAAAAAAABBBB"
 wb "\000\000BBBB"
w+b "\000\000BBBB"
r+b "AABBBBAA"

ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
 ab "AAAAAAAABBBB"
 wb "\x00\x00BBBB"
w+b "\x00\x00BBBB"
r+b "AABBBBAA"

ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
 ab "AAAAAAAABBBB"
 wb "\x00\x00BBBB"
w+b "\x00\x00BBBB"
r+b "AABBBBAA"

ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
 ab "AAAAAAAABBBB"
 wb "\000\000BBBB"
w+b "\000\000BBBB"
r+b "AABBBBAA"

ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
 ab "AAAAAAAABBBB"
 wb "\x00\x00BBBB"
w+b "\x00\x00BBBB"
r+b "AABBBBAA"

ruby 1.9.1p431 (2011-02-18 revision 30908) [x86_64-linux]
Error loading gem paths on load path in gem_prelude
can't modify frozen string
:69:in `force_encoding'
:69:in `set_home'
:38:in `dir'
:76:in `set_paths'
:47:in `path'
:286:in `push_all_highest_version_gems_on_load_path'
:355:in `'
 ab "AAAAAAAABBBB"
 wb "\x00\x00BBBB"
w+b "\x00\x00BBBB"
r+b "AABBBBAA"

ruby 1.9.3dev (2011-07-31 revision 32789) [x86_64-linux]
 ab "AAAAAAAABBBB"
 wb "\x00\x00BBBB"
w+b "\x00\x00BBBB"
r+b "AABBBBAA"

fear:0:ruby$
The other modes behave as expected. See also - http://dev.metasploit.com/redmine/issues/3199 -- http://bugs.ruby-lang.org/