File.read produces \0 as the first byte

File.read e$B$Ge(B /proc/self/status [email protected]$H$-$K!":G=i$N%P%$%He(B
e$B$,e(B \0 e$B$K$J$j$^$9!#e(B

% ./ruby -ve ‘p File.read("/proc/self/status")’
ruby 1.9.0 (2006-08-31) [i686-linux]
“\000ame:\truby\nState:\tR
(running)\nSleepAVG:\t88%\nTgid:\t20549\nPid:\t20549\nPPid:\t10271\nTracerPid:\t0\nUid:\t1000\t1000\t1000\t1000\nGid:\t1000\t1000\t1000\t1000\nFDSize:\t32\nGroups:\t20
24 25 29 44 46 1000 \nVmPeak:\t 3220 kB\nVmSize:\t 3220
kB\nVmLck:\t 0 kB\nVmHWM:\t 1488 kB\nVmRSS:\t 1488
kB\nVmData:\t 752 kB\nVmStk:\t 84 kB\nVmExe:\t 848
kB\nVmLib:\t 1464 kB\nVmPTE:\t 12
kB\nThreads:\t1\nSigQ:\t0/4294967295\nSigPnd:\t0000000000000000\nShdPnd:\t0000000000000000\nSigBlk:\t0000000000000000\nSigIgn:\t0000000000000000\nSigCgt:\t0000000000003a07\nCapInh:\t0000000000000000\nCapPrm:\t0000000000000000\nCapEff:\t0000000000000000\n”

gets e$B$GFI$`$H$=$&$O$J$j$^$;$s!#e(B

% ./ruby -e ‘open("/proc/self/status") {|f| p f.gets }’
“Name:\truby\n”

[email protected]$G$9!#e(B

At Fri, 1 Sep 2006 07:06:58 +0900,
Tanaka A. wrote in [ruby-dev:29368]:

File.read e$B$Ge(B /proc/self/status [email protected]$H$-$K!":G=i$N%P%$%He(B
e$B$,e(B \0 e$B$K$J$j$^$9!#e(B

embede$B$+$ie(Bnoembede$B$Ke(Bresizee$B$9$k$H$-$K4{B8$NFbMF$r%3%T!<$7$F$$$J$$e(B
e$B$h$&$G$9!#e(B

e$B$D$$$G$Ke([email protected]$He(BRSTRING_EMBED_LEN_MASKe$B$,B-$j$J$/$J$j$=$&$Je(B
e$B7o$b!#e(B

Index: ruby.h

RCS file: /cvs/ruby/src/ruby/ruby.h,v
retrieving revision 1.148
diff -p -u -2 -r1.148 ruby.h
— ruby.h 31 Aug 2006 10:47:43 -0000 1.148
+++ ruby.h 1 Sep 2006 00:11:33 -0000
@@ -363,5 +363,5 @@ struct RFloat {
#define ELTS_SHARED FL_USER2

-#define RSTRING_EMBED_LEN_MAX ((sizeof(VALUE)3)/sizeof(char)-1)
+#define RSTRING_EMBED_LEN_MAX (SIZEOF_VALUE
3-1)
struct RString {
struct RBasic basic;
@@ -381,4 +381,8 @@ struct RString {
#define RSTRING_EMBED_LEN_MASK (FL_USER2|FL_USER3|FL_USER4|FL_USER5)
#define RSTRING_EMBED_LEN_SHIFT (FL_USHIFT+2)
+#if RSTRING_EMBED_LEN_MAX > (RSTRING_EMBED_LEN_MASK >>
RSTRING_EMBED_LEN_SHIFT)
+#undef RSTRING_EMBED_LEN_MAX
+#define RSTRING_EMBED_LEN_MAX (RSTRING_EMBED_LEN_MASK >>
RSTRING_EMBED_LEN_SHIFT)
+#endif
#define RSTRING_LEN(str)
(!(RBASIC(str)->flags & RSTRING_NOEMBED) ?
Index: string.c

RCS file: /cvs/ruby/src/ruby/string.c,v
retrieving revision 1.258
diff -p -u -2 -r1.258 string.c
— string.c 31 Aug 2006 10:47:43 -0000 1.258
+++ string.c 1 Sep 2006 00:12:08 -0000
@@ -683,4 +683,5 @@ rb_str_resize(VALUE str, long len)
if (len != RSTRING_LEN(str)) {
if (STR_EMBED_P(str)) {

  •   char *ptr;
      if (len <= RSTRING_EMBED_LEN_MAX) {
      STR_SET_EMBED_LEN(str, len);
    

@@ -688,5 +689,7 @@ rb_str_resize(VALUE str, long len)
return str;
}

  •   RSTRING(str)->as.heap.ptr = ALLOC_N(char,len+1);
    
  •   ptr = ALLOC_N(char, len+1);
    
  •   MEMCPY(ptr, RSTRING(str)->ary, char, RSTRING_LEN(str));
    
  •   RSTRING(str)->as.heap.ptr = ptr;
      STR_SET_NOEMBED(str);
    
    }
    @@ -4166,5 +4169,5 @@ rb_str_ord(VALUE s)
    if (RSTRING_LEN(s) != 1) {
    rb_raise(rb_eTypeError,
  •    "expacted a characer, but string of size %d given",
    
  •    "expacted a characer, but string of size %ld given",
       RSTRING_LEN(s));
    
    }

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:29369] Re: File.read produces \0 as the first
byte.”
on Fri, 1 Sep 2006 09:17:28 +0900, [email protected] writes:

|At Fri, 1 Sep 2006 07:06:58 +0900,
|Tanaka A. wrote in [ruby-dev:29368]:
|> File.read e$B$Ge(B /proc/self/status [email protected]$H$-$K!":G=i$N%P%$%He(B
|> e$B$,e(B \0 e$B$K$J$j$^$9!#e(B
|
|embede$B$+$ie(Bnoembede$B$Ke(Bresizee$B$9$k$H$-$K4{B8$NFbMF$r%3%T!<$7$F$$$J$$e(B
|e$B$h$&$G$9!#e(B
|
|e$B$D$$$G$Ke([email protected]$He(BRSTRING_EMBED_LEN_MASKe$B$,B-$j$J$/$J$j$=$&$Je(B
|e$B7o$b!#e(B

e$B%3%_%C%H$7$F$/[email protected]$5$$!#e(B64bite$B4D6-$K$D$$$F$O9M$($F$^$;$s$G$7$?!#e(B
e$B=$9T$,B-$j$J$$!#e(B

In article [email protected],
[email protected] writes:

e$B$D$$$G$Ke([email protected]$He(BRSTRING_EMBED_LEN_MASKe$B$,B-$j$J$/$J$j$=$&$Je(B
e$B7o$b!#e(B

e$B$b$&e(B 1bit e$B;H$C$Fe(B 23byte
e$B$^$GI=8=$G$-$k$h$&$K$7$?$[$&$,$$$$$se(B
e$B$8$c$J$$$+$H;W$$$^$9!#e(B