1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Tweak rb_str_modify_expand() + read() spec to try to find out why it fails on some platforms

* Use a longer string as <= 23 characters it's embedded on CRuby and
  the value of rb_str_capacity() is implementation-specific.
This commit is contained in:
Benoit Daloze 2021-07-30 11:36:20 +02:00
parent fd96503f7b
commit ff6c176028
3 changed files with 21 additions and 9 deletions

View file

@ -4,6 +4,7 @@
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include "ruby/encoding.h"
@ -388,15 +389,24 @@ VALUE string_spec_RSTRING_PTR_after_yield(VALUE self, VALUE str) {
VALUE string_spec_RSTRING_PTR_read(VALUE self, VALUE str, VALUE path) {
char *cpath = StringValueCStr(path);
int fd = open(cpath, O_RDONLY);
rb_str_modify_expand(str, 10);
VALUE capacities = rb_ary_new();
if (fd < 0) {
rb_syserr_fail(errno, "open");
}
rb_str_modify_expand(str, 30);
rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str)));
char *buffer = RSTRING_PTR(str);
read(fd, buffer, 10);
rb_str_modify_expand(str, 21);
read(fd, buffer, 30);
rb_str_modify_expand(str, 53);
rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str)));
char *buffer2 = RSTRING_PTR(str);
read(fd, buffer2 + 10, 11);
rb_str_set_len(str, 21);
read(fd, buffer2 + 30, 53 - 30);
rb_str_set_len(str, 53);
close(fd);
return str;
return capacities;
}
VALUE string_spec_StringValue(VALUE self, VALUE str) {

View file

@ -1 +1 @@
fixture file contents
fixture file contents to test read() with RSTRING_PTR

View file

@ -599,10 +599,12 @@ describe "C-API String function" do
@s.RSTRING_PTR_short_memcpy(str).should == "Infinity"
end
it "allows read to update string contents" do
it "allows read() to update the string contents" do
filename = fixture(__FILE__, "read.txt")
str = ""
@s.RSTRING_PTR_read(str, filename).should == "fixture file contents"
capacities = @s.RSTRING_PTR_read(str, filename)
capacities.should == [30, 53]
str.should == "fixture file contents to test read() with RSTRING_PTR"
end
end