Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

RangesIO#write corrupts output if passed and UTF8 string #14

Merged
merged 1 commit into from

2 participants

@romuloceccon

RangesIO#write has a bug which corrupts the output file if the encoding is UTF-8, due to differences between the string size as reported by String#length and the actual number of bytes written (as File#tell would report). This patch fixes de issue.

@aquasync aquasync merged commit 5d4652f into aquasync:master
@romuloceccon romuloceccon deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 17, 2013
  1. @romuloceccon
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 0 deletions.
  1. +5 −0 lib/ole/ranges_io.rb
  2. +21 −0 test/test_storage.rb
View
5 lib/ole/ranges_io.rb
@@ -195,6 +195,11 @@ def size= size
end
def write data
+ # duplicates object to avoid side effects for the caller, but do so only if
+ # encoding isn't already ASCII-8BIT (slight optimization)
+ unless data.encoding == Encoding::ASCII_8BIT
+ data = data.dup.force_encoding(Encoding::ASCII_8BIT)
+ end
return 0 if data.empty?
data_pos = 0
# if we don't have room, we can use the truncate hook to make more space.
View
21 test/test_storage.rb
@@ -1,4 +1,5 @@
#! /usr/bin/ruby
+# coding: utf-8
$: << File.dirname(__FILE__) + '/../lib'
#require 'rubygems'
@@ -216,5 +217,25 @@ def test_create_dirent
assert_raises(ArgumentError) { Ole::Storage::Dirent.new ole, :type => :bogus }
end
end
+
+ def test_write_utf8_string
+ tmp_file = "#{TEST_DIR}/encoding.tmp"
+ begin
+ Ole::Storage.open File.open(tmp_file, 'wb+') do |ole|
+ ole.file.open '1', 'w' do |writer|
+ writer.write("programação ")
+ writer.write("ruby")
+ end
+ end
+
+ Ole::Storage.open File.open(tmp_file, 'rb') do |ole|
+ ole.file.open '1', 'r' do |reader|
+ assert_equal("programação ruby", reader.read.force_encoding('UTF-8'))
+ end
+ end
+ ensure
+ FileUtils.rm(tmp_file) if File.exist?(tmp_file)
+ end
+ end
end
Something went wrong with that request. Please try again.