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

* marshal.c (w_nbyte): should output always via rb_io_write().

* marshal.c (dump_ensure): ditto.

* marshal.c (marshal_dump): should call "binmode" method, if it
  responds to.

* marshal.c (r_byte): should input always via "getc" method.

* marshal.c (r_bytes0): should input always via "read" method.

* marshal.c (marshal_load): need not to set up FILE* fp;


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3544 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2003-03-03 07:20:17 +00:00
parent 6a6d0ad220
commit 6d0b44501e
2 changed files with 41 additions and 58 deletions

View file

@ -1,3 +1,18 @@
Sun Mar 2 09:51:47 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>
* marshal.c (w_nbyte): should output always via rb_io_write().
* marshal.c (dump_ensure): ditto.
* marshal.c (marshal_dump): should call "binmode" method, if it
responds to.
* marshal.c (r_byte): should input always via "getc" method.
* marshal.c (r_bytes0): should input always via "read" method.
* marshal.c (marshal_load): need not to set up FILE* fp;
Mon Mar 3 11:29:04 2003 Yukihiro Matsumoto <matz@ruby-lang.org> Mon Mar 3 11:29:04 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
* parse.y (arg): parse 'lhs = a rescue b' as 'lhs=(a rescue b)'. * parse.y (arg): parse 'lhs = a rescue b' as 'lhs=(a rescue b)'.

View file

@ -76,11 +76,10 @@ shortlen(len, ds)
static ID s_dump, s_load; static ID s_dump, s_load;
static ID s_dump_data, s_load_data, s_alloc; static ID s_dump_data, s_load_data, s_alloc;
static ID s_getc, s_read, s_write; static ID s_getc, s_read, s_write, s_binmode;
struct dump_arg { struct dump_arg {
VALUE obj; VALUE obj;
FILE *fp;
VALUE str, dest; VALUE str, dest;
st_table *symbol; st_table *symbol;
st_table *data; st_table *data;
@ -96,23 +95,17 @@ struct dump_call_arg {
static void w_long _((long, struct dump_arg*)); static void w_long _((long, struct dump_arg*));
static void static void
w_byten(s, n, arg) w_nbyte(s, n, arg)
char *s; char *s;
int n; int n;
struct dump_arg *arg; struct dump_arg *arg;
{ {
if (arg->fp) { VALUE buf = arg->str;
if (rb_io_fwrite(s, n, arg->fp) < 0) rb_str_buf_cat(buf, s, n);
rb_sys_fail(0); if (arg->dest && RSTRING(buf)->len >= BUFSIZ) {
} if (arg->taint) OBJ_TAINT(buf);
else { rb_io_write(arg->dest, buf);
VALUE buf = arg->str; rb_str_resize(buf, 0);
rb_str_buf_cat(buf, s, n);
if (arg->dest && RSTRING(buf)->len >= BUFSIZ) {
if (arg->taint) OBJ_TAINT(buf);
rb_io_write(arg->dest, buf);
rb_str_resize(buf, 0);
}
} }
} }
@ -121,7 +114,7 @@ w_byte(c, arg)
char c; char c;
struct dump_arg *arg; struct dump_arg *arg;
{ {
w_byten(&c, 1, arg); w_nbyte(&c, 1, arg);
} }
static void static void
@ -131,7 +124,7 @@ w_bytes(s, n, arg)
struct dump_arg *arg; struct dump_arg *arg;
{ {
w_long(n, arg); w_long(n, arg);
w_byten(s, n, arg); w_nbyte(s, n, arg);
} }
static void static void
@ -580,7 +573,7 @@ dump_ensure(arg)
{ {
st_free_table(arg->symbol); st_free_table(arg->symbol);
st_free_table(arg->data); st_free_table(arg->data);
if (!arg->fp && arg->taint) { if (arg->taint) {
OBJ_TAINT(arg->str); OBJ_TAINT(arg->str);
} }
return 0; return 0;
@ -608,25 +601,16 @@ marshal_dump(argc, argv)
} }
arg.dest = 0; arg.dest = 0;
if (port) { if (port) {
if (rb_obj_is_kind_of(port, rb_cIO)) { if (!rb_respond_to(port, s_write)) {
OpenFile *fptr;
rb_io_binmode(port);
GetOpenFile(port, fptr);
rb_io_check_writable(fptr);
arg.fp = (fptr->f2) ? fptr->f2 : fptr->f;
}
else if (rb_respond_to(port, s_write)) {
arg.fp = 0;
arg.str = rb_str_buf_new(0);
arg.dest = port;
}
else {
rb_raise(rb_eTypeError, "instance of IO needed"); rb_raise(rb_eTypeError, "instance of IO needed");
} }
arg.str = rb_str_buf_new(0);
arg.dest = port;
if (rb_respond_to(port, s_binmode)) {
rb_funcall2(port, s_binmode, 0, 0);
}
} }
else { else {
arg.fp = 0;
port = rb_str_buf_new(0); port = rb_str_buf_new(0);
arg.str = port; arg.str = port;
} }
@ -647,7 +631,6 @@ marshal_dump(argc, argv)
} }
struct load_arg { struct load_arg {
FILE *fp;
char *ptr, *end; char *ptr, *end;
st_table *symbol; st_table *symbol;
VALUE data; VALUE data;
@ -663,11 +646,7 @@ r_byte(arg)
{ {
int c; int c;
if (arg->fp) { if (!arg->end) {
c = rb_getc(arg->fp);
if (c == EOF) rb_eof_error();
}
else if (!arg->end) {
VALUE src = (VALUE)arg->ptr; VALUE src = (VALUE)arg->ptr;
VALUE v = rb_funcall2(src, s_getc, 0, 0); VALUE v = rb_funcall2(src, s_getc, 0, 0);
if (NIL_P(v)) rb_eof_error(); if (NIL_P(v)) rb_eof_error();
@ -741,14 +720,7 @@ r_bytes0(len, arg)
{ {
VALUE str; VALUE str;
if (arg->fp) { if (!arg->end) {
str = rb_str_new(0, len);
if (rb_io_fread(RSTRING(str)->ptr, len, arg->fp) != len) {
too_short:
rb_raise(rb_eArgError, "marshal data too short");
}
}
else if (!arg->end) {
VALUE src = (VALUE)arg->ptr; VALUE src = (VALUE)arg->ptr;
VALUE n = LONG2NUM(len); VALUE n = LONG2NUM(len);
str = rb_funcall2(src, s_read, 1, &n); str = rb_funcall2(src, s_read, 1, &n);
@ -759,7 +731,8 @@ r_bytes0(len, arg)
} }
else { else {
if (arg->ptr + len > arg->end) { if (arg->ptr + len > arg->end) {
goto too_short; too_short:
rb_raise(rb_eArgError, "marshal data too short");
} }
str = rb_str_new(arg->ptr, len); str = rb_str_new(arg->ptr, len);
arg->ptr += len; arg->ptr += len;
@ -1214,23 +1187,17 @@ marshal_load(argc, argv)
volatile VALUE hash; /* protect from GC */ volatile VALUE hash; /* protect from GC */
rb_scan_args(argc, argv, "11", &port, &proc); rb_scan_args(argc, argv, "11", &port, &proc);
if (rb_obj_is_kind_of(port, rb_cIO)) { if (rb_respond_to(port, rb_intern("to_str"))) {
rb_io_binmode(port);
GetOpenFile(port, fptr);
rb_io_check_readable(fptr);
arg.fp = fptr->f;
arg.taint = Qtrue;
}
else if (rb_respond_to(port, rb_intern("to_str"))) {
arg.taint = OBJ_TAINTED(port); /* original taintedness */ arg.taint = OBJ_TAINTED(port); /* original taintedness */
StringValue(port); /* possible conversion */ StringValue(port); /* possible conversion */
arg.fp = 0;
arg.ptr = RSTRING(port)->ptr; arg.ptr = RSTRING(port)->ptr;
arg.end = arg.ptr + RSTRING(port)->len; arg.end = arg.ptr + RSTRING(port)->len;
} }
else if (rb_respond_to(port, s_getc) && rb_respond_to(port, s_read)) { else if (rb_respond_to(port, s_getc) && rb_respond_to(port, s_read)) {
if (rb_respond_to(port, s_binmode)) {
rb_funcall2(port, s_binmode, 0, 0);
}
arg.taint = Qfalse; arg.taint = Qfalse;
arg.fp = 0;
arg.ptr = (char *)port; arg.ptr = (char *)port;
arg.end = 0; arg.end = 0;
} }
@ -1273,6 +1240,7 @@ Init_marshal()
s_getc = rb_intern("getc"); s_getc = rb_intern("getc");
s_read = rb_intern("read"); s_read = rb_intern("read");
s_write = rb_intern("write"); s_write = rb_intern("write");
s_binmode = rb_intern("binmode");
rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1); rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1);
rb_define_module_function(rb_mMarshal, "load", marshal_load, -1); rb_define_module_function(rb_mMarshal, "load", marshal_load, -1);