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

io.c: check ARGV element type

* io.c (argf_next_argv): check ARGV element type, and try
  conversion if necessary.  [ruby-core:71140] [Bug #11610]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52211 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-10-22 04:13:52 +00:00
parent 23419d859a
commit 66e41cbe4f
3 changed files with 26 additions and 11 deletions

View file

@ -1,3 +1,8 @@
Thu Oct 22 13:13:49 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* io.c (argf_next_argv): check ARGV element type, and try
conversion if necessary. [ruby-core:71140] [Bug #11610]
Thu Oct 22 11:11:16 2015 Shugo Maeda <shugo@ruby-lang.org> Thu Oct 22 11:11:16 2015 Shugo Maeda <shugo@ruby-lang.org>
* test/net/ftp/test_ftp.rb: add tests for getbinaryfile and * test/net/ftp/test_ftp.rb: add tests for getbinaryfile and

24
io.c
View file

@ -8004,9 +8004,11 @@ argf_next_argv(VALUE argf)
if (ARGF.next_p == 1) { if (ARGF.next_p == 1) {
retry: retry:
if (RARRAY_LEN(ARGF.argv) > 0) { if (RARRAY_LEN(ARGF.argv) > 0) {
ARGF.filename = rb_str_encode_ospath(rb_ary_shift(ARGF.argv)); VALUE filename = rb_ary_shift(ARGF.argv);
fn = StringValueCStr(ARGF.filename); StringValueCStr(filename);
if (strlen(fn) == 1 && fn[0] == '-') { ARGF.filename = rb_str_encode_ospath(filename);
fn = StringValueCStr(filename);
if (RSTRING_LEN(filename) == 1 && fn[0] == '-') {
ARGF.current_file = rb_stdin; ARGF.current_file = rb_stdin;
if (ARGF.inplace) { if (ARGF.inplace) {
rb_warn("Can't do inplace edit for stdio; skipping"); rb_warn("Can't do inplace edit for stdio; skipping");
@ -8015,7 +8017,7 @@ argf_next_argv(VALUE argf)
} }
else { else {
VALUE write_io = Qnil; VALUE write_io = Qnil;
int fr = rb_sysopen(ARGF.filename, O_RDONLY, 0); int fr = rb_sysopen(filename, O_RDONLY, 0);
if (ARGF.inplace) { if (ARGF.inplace) {
struct stat st; struct stat st;
@ -8029,7 +8031,7 @@ argf_next_argv(VALUE argf)
rb_io_close(rb_stdout); rb_io_close(rb_stdout);
} }
fstat(fr, &st); fstat(fr, &st);
str = ARGF.filename; str = filename;
if (*ARGF.inplace) { if (*ARGF.inplace) {
str = rb_str_dup(str); str = rb_str_dup(str);
rb_str_cat2(str, ARGF.inplace); rb_str_cat2(str, ARGF.inplace);
@ -8039,14 +8041,14 @@ argf_next_argv(VALUE argf)
(void)unlink(RSTRING_PTR(str)); (void)unlink(RSTRING_PTR(str));
if (rename(fn, RSTRING_PTR(str)) < 0) { if (rename(fn, RSTRING_PTR(str)) < 0) {
rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file", rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file",
ARGF.filename, str, strerror(errno)); filename, str, strerror(errno));
goto retry; goto retry;
} }
fr = rb_sysopen(str, O_RDONLY, 0); fr = rb_sysopen(str, O_RDONLY, 0);
#else #else
if (rename(fn, RSTRING_PTR(str)) < 0) { if (rename(fn, RSTRING_PTR(str)) < 0) {
rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file", rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file",
ARGF.filename, str, strerror(errno)); filename, str, strerror(errno));
close(fr); close(fr);
goto retry; goto retry;
} }
@ -8058,13 +8060,13 @@ argf_next_argv(VALUE argf)
#else #else
if (unlink(fn) < 0) { if (unlink(fn) < 0) {
rb_warn("Can't remove %"PRIsVALUE": %s, skipping file", rb_warn("Can't remove %"PRIsVALUE": %s, skipping file",
ARGF.filename, strerror(errno)); filename, strerror(errno));
close(fr); close(fr);
goto retry; goto retry;
} }
#endif #endif
} }
fw = rb_sysopen(ARGF.filename, O_WRONLY|O_CREAT|O_TRUNC, 0666); fw = rb_sysopen(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
#ifndef NO_SAFE_RENAME #ifndef NO_SAFE_RENAME
fstat(fw, &st2); fstat(fw, &st2);
#ifdef HAVE_FCHMOD #ifdef HAVE_FCHMOD
@ -8080,9 +8082,9 @@ argf_next_argv(VALUE argf)
err = chown(fn, st.st_uid, st.st_gid); err = chown(fn, st.st_uid, st.st_gid);
#endif #endif
if (err && getuid() == 0 && st2.st_uid == 0) { if (err && getuid() == 0 && st2.st_uid == 0) {
const char *wkfn = RSTRING_PTR(ARGF.filename); const char *wkfn = RSTRING_PTR(filename);
rb_warn("Can't set owner/group of %"PRIsVALUE" to same as %"PRIsVALUE": %s, skipping file", rb_warn("Can't set owner/group of %"PRIsVALUE" to same as %"PRIsVALUE": %s, skipping file",
ARGF.filename, str, strerror(errno)); filename, str, strerror(errno));
(void)close(fr); (void)close(fr);
(void)close(fw); (void)close(fw);
(void)unlink(wkfn); (void)unlink(wkfn);

View file

@ -899,4 +899,12 @@ class TestArgf < Test::Unit::TestCase
assert_equal "done with eof\n", f.gets assert_equal "done with eof\n", f.gets
end end
end end
def test_wrong_type
assert_separately([], <<-'end;')
bug11610 = '[ruby-core:71140] [Bug #11610]'
ARGV[0] = nil
assert_raise(TypeError, bug11610) {gets}
end;
end
end end