summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Kanis <lars@greiz-reinsdorf.de>2020-12-19 22:07:41 +0100
committerGitHub <noreply@github.com>2020-12-19 22:07:41 +0100
commita262e0e8fbbf4735eb988d670915678191627ddd (patch)
treec1e9c61ff993869bb977c159a0c7d991e04b844d
parent0d8e5815716eeeb8fad11205646dfedd8bf23f03 (diff)
parent82602ec1e5aa800f049d3a6f6ab3b54f01d90394 (diff)
downloadffi-a262e0e8fbbf4735eb988d670915678191627ddd.tar.gz
Merge pull request #861 from larskanis/revoke-806
Revert "Merge pull request #806 from eregon/fix-write_string"
-rw-r--r--ext/ffi_c/AbstractMemory.c2
-rw-r--r--lib/ffi/pointer.rb28
-rw-r--r--spec/ffi/string_spec.rb47
3 files changed, 19 insertions, 58 deletions
diff --git a/ext/ffi_c/AbstractMemory.c b/ext/ffi_c/AbstractMemory.c
index 2a092ea..1a7fcde 100644
--- a/ext/ffi_c/AbstractMemory.c
+++ b/ext/ffi_c/AbstractMemory.c
@@ -499,7 +499,7 @@ memory_read_array_of_string(int argc, VALUE* argv, VALUE self)
* @raise {SecurityError} when writing unsafe string to memory
* @raise {IndexError} if +offset+ is too great
* @raise {NullPointerError} if memory not initialized
- * Put a string in memory. Writes a final \0 byte.
+ * Put a string in memory.
*/
static VALUE
memory_put_string(VALUE self, VALUE offset, VALUE str)
diff --git a/lib/ffi/pointer.rb b/lib/ffi/pointer.rb
index 43a547b..a5ee655 100644
--- a/lib/ffi/pointer.rb
+++ b/lib/ffi/pointer.rb
@@ -85,37 +85,23 @@ module FFI
# @param [String] str string to write
# @param [Numeric] len length of string to return
# @return [self]
- # Write +len+ first bytes of +str+ in pointer's contents and a final \0 byte.
+ # Write +len+ first bytes of +str+ in pointer's contents.
#
# Same as:
# ptr.write_string(str, len) # with len not nil
def write_string_length(str, len)
- write_string(str, len)
+ put_bytes(0, str, 0, len)
end unless method_defined?(:write_string_length)
# @param [String] str string to write
# @param [Numeric] len length of string to return
# @return [self]
- # Write +str+ in pointer's contents.
- # If +len+ is given, write the first +len+ bytes of +str+.
- # In both cases a final \0 byte is written after the string.
+ # Write +str+ in pointer's contents, or first +len+ bytes if
+ # +len+ is not +nil+.
def write_string(str, len=nil)
- if len
- if len == size
- warn "[DEPRECATION] Memory too small to write a final 0-byte in #{caller(1, 1)[0]}. This will raise an error in ffi-2.0. Please use write_bytes instead or enlarge the memory region."
- write_bytes(str, 0, len)
- else
- put_char(len, 0) # Check size before writing str
- write_bytes(str, 0, len)
- end
- else
- if str.bytesize == size
- warn "[DEPRECATION] Memory too small to write a final 0-byte in #{caller(1, 1)[0]}. This will raise an error in ffi-2.0. Please use write_bytes instead or enlarge the memory region."
- write_bytes(str)
- else
- put_string(0, str)
- end
- end
+ len = str.bytesize unless len
+ # Write the string data without NUL termination
+ put_bytes(0, str, 0, len)
end unless method_defined?(:write_string)
# @param [Type] type type of data to read from pointer's contents
diff --git a/spec/ffi/string_spec.rb b/spec/ffi/string_spec.rb
index 65d9dc8..5b16859 100644
--- a/spec/ffi/string_spec.rb
+++ b/spec/ffi/string_spec.rb
@@ -102,12 +102,11 @@ describe "String tests" do
describe "#write_string" do
# https://github.com/ffi/ffi/issues/805
describe "with no length given" do
- it "writes a final \\0" do
+ it "writes no final \\0" do
ptr = FFI::MemoryPointer.new(8)
ptr.write_int64(-1)
ptr.write_string("äbc")
- expect(ptr.read_bytes(5)).to eq("äbc\x00".b)
- expect(ptr.read_string).to eq("äbc".b)
+ expect(ptr.read_bytes(5)).to eq("äbc\xff".b)
end
it "doesn't write anything when size is exceeded" do
@@ -119,22 +118,10 @@ describe "String tests" do
expect(ptr.read_int64).to eq(-1)
end
- if FFI::VERSION < "2"
- it "prints a warning if final \\0 doesn't fit into memory" do
- ptr = FFI::MemoryPointer.new(5)
- expect do
- ptr.write_string("äbcd")
- end.to output(/memory too small/i).to_stderr
- expect(ptr.read_string).to eq("äbcd".b)
- end
- else
- it "denies writing if final \\0 doesn't fit into memory" do
- ptr = FFI::MemoryPointer.new(5)
- expect do
- ptr.write_string("äbcd")
- end.to raise_error(IndexError, /out of bounds/i)
- expect(ptr.read_string).to eq("".b)
- end
+ it "fits into memory" do
+ ptr = FFI::MemoryPointer.new(5)
+ ptr.write_string("äbcd")
+ expect(ptr.read_string).to eq("äbcd".b)
end
end
@@ -143,7 +130,7 @@ describe "String tests" do
ptr = FFI::MemoryPointer.new(8)
ptr.write_int64(-1)
ptr.write_string("äbcd", 3)
- expect(ptr.read_bytes(5)).to eq("äb\x00\xFF".b)
+ expect(ptr.read_bytes(5)).to eq("äb\xFF\xFF".b)
end
it "doesn't write anything when size is exceeded" do
@@ -155,22 +142,10 @@ describe "String tests" do
expect(ptr.read_int64).to eq(-1)
end
- if FFI::VERSION < "2"
- it "prints a warning if final \\0 doesn't fit into memory" do
- ptr = FFI::MemoryPointer.new(5)
- expect do
- ptr.write_string("äbcde", 5)
- end.to output(/memory too small/i).to_stderr
- expect(ptr.read_string).to eq("äbcd".b)
- end
- else
- it "denies writing if final \\0 doesn't fit into memory" do
- ptr = FFI::MemoryPointer.new(5)
- expect do
- ptr.write_string("äbcde", 5)
- end.to raise_error(IndexError, /out of bounds/i)
- expect(ptr.read_string).to eq("".b)
- end
+ it "fits into memory" do
+ ptr = FFI::MemoryPointer.new(5)
+ ptr.write_string("äbcde", 5)
+ expect(ptr.read_string).to eq("äbcd".b)
end
end
end