From 889a620b769145a7c20c7ce78aff62d80e485496 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 4 Oct 2004 05:04:22 +0000 Subject: [PATCH] * io.c (rb_file_open_internal, rb_io_reopen): fname might be altered while GC. [ruby-dev:24408] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6995 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ io.c | 21 ++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 244d028f76..a2bbf8b975 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Oct 4 14:03:40 2004 Nobuyoshi Nakada + + * io.c (rb_file_open_internal, rb_io_reopen): fname might be altered + while GC. [ruby-dev:24408] + Mon Oct 4 12:53:45 2004 Hidetoshi NAGAI * ext/tk/lib/tk/optiondb.rb: support definition of command diff --git a/io.c b/io.c index 71273ed0b6..9f34783d0b 100644 --- a/io.c +++ b/io.c @@ -2421,6 +2421,8 @@ rb_io_mode_modenum(mode) return flags; } +#define MODENUM_MAX 4 + static char* rb_io_modenum_mode(flags, mode) int flags; @@ -2548,12 +2550,13 @@ rb_file_open_internal(io, fname, mode) const char *fname, *mode; { OpenFile *fptr; + char mbuf[MODENUM_MAX]; MakeOpenFile(io, fptr); fptr->mode = rb_io_mode_flags(mode); - fptr->f = rb_fopen(fname, mode); fptr->path = strdup(fname); + fptr->f = rb_fopen(fptr->path, rb_io_flags_mode(fptr->mode, mbuf)); return io; } @@ -2574,7 +2577,7 @@ rb_file_sysopen_internal(io, fname, flags, mode) OpenFile *fptr; int fd; char *m; - char mbuf[4]; + char mbuf[MODENUM_MAX]; MakeOpenFile(io, fptr); @@ -2878,7 +2881,7 @@ rb_io_popen(str, argc, argv, klass) { char *mode; VALUE pname, pmode, port; - char mbuf[4]; + char mbuf[MODENUM_MAX]; if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) { mode = "r"; @@ -2965,7 +2968,7 @@ rb_io_s_popen(argc, argv, klass) { char *mode; VALUE pname, pmode, port, tmp; - char mbuf[4]; + char mbuf[MODENUM_MAX]; if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) { mode = "r"; @@ -3353,7 +3356,7 @@ rb_io_reopen(argc, argv, file) VALUE file; { VALUE fname, nmode; - char *mode; + char mode[MODENUM_MAX]; OpenFile *fptr; rb_secure(4); @@ -3373,10 +3376,10 @@ rb_io_reopen(argc, argv, file) } if (!NIL_P(nmode)) { - mode = StringValuePtr(nmode); + strncpy(mode, StringValuePtr(nmode), sizeof(mode)); + mode[sizeof(mode) - 1] = 0; } else { - mode = ALLOCA_N(char, 4); rb_io_flags_mode(fptr->mode, mode); } @@ -3388,7 +3391,7 @@ rb_io_reopen(argc, argv, file) fptr->path = strdup(RSTRING(fname)->ptr); fptr->mode = rb_io_mode_flags(mode); if (!fptr->f) { - fptr->f = rb_fopen(RSTRING(fname)->ptr, mode); + fptr->f = rb_fopen(fptr->path, mode); if (fptr->f2) { fclose(fptr->f2); fptr->f2 = 0; @@ -3948,7 +3951,7 @@ rb_io_initialize(argc, argv, io) VALUE fnum, mode, orig; OpenFile *fp, *ofp = NULL; int fd, flags, fmode; - char mbuf[4]; + char mbuf[MODENUM_MAX]; rb_secure(4); rb_scan_args(argc, argv, "11", &fnum, &mode);