ruby-changes:3584
From: ko1@a...
Date: Wed, 16 Jan 2008 14:56:11 +0900 (JST)
Subject: [ruby-changes:3584] nobu - Ruby:r15073 (trunk): * file.c (sys_fail2): get rid of unlimited alloca.
nobu 2008-01-16 14:55:22 +0900 (Wed, 16 Jan 2008) New Revision: 15073 Modified files: trunk/ChangeLog trunk/file.c trunk/include/ruby/encoding.h trunk/io.c Log: * file.c (sys_fail2): get rid of unlimited alloca. * io.c (mode_enc, pipe_open, rb_io_s_popen): ditto. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/file.c?r1=15073&r2=15072&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15073&r2=15072&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/io.c?r1=15073&r2=15072&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/include/ruby/encoding.h?r1=15073&r2=15072&diff_format=u Index: include/ruby/encoding.h =================================================================== --- include/ruby/encoding.h (revision 15072) +++ include/ruby/encoding.h (revision 15073) @@ -44,6 +44,8 @@ #define ENCODING_IS_ASCII8BIT(obj) (ENCODING_GET_INLINED(obj) == 0) +#define ENCODING_MAXNAMELEN 42 + #define ENC_CODERANGE_MASK (FL_USER8|FL_USER9) #define ENC_CODERANGE_UNKNOWN 0 #define ENC_CODERANGE_7BIT FL_USER8 Index: ChangeLog =================================================================== --- ChangeLog (revision 15072) +++ ChangeLog (revision 15073) @@ -1,3 +1,9 @@ +Wed Jan 16 14:55:15 2008 Nobuyoshi Nakada <nobu@r...> + + * file.c (sys_fail2): get rid of unlimited alloca. + + * io.c (mode_enc, pipe_open, rb_io_s_popen): ditto. + Wed Jan 16 12:51:30 2008 Nobuyoshi Nakada <nobu@r...> * include/ruby/intern.h (rb_str_tmp_new, rb_str_shared_replace): Index: io.c =================================================================== --- io.c (revision 15072) +++ io.c (revision 15073) @@ -3184,20 +3184,26 @@ } if (p0) { - enc2name = ALLOCA_N(char, p0-estr+1); - strncpy(enc2name, estr, p0-estr); - enc2name[p0-estr] = '\0'; - idx2=rb_enc_find_index(enc2name); - if (idx2 == idx) { + int n = p0 - estr; + if (n > ENCODING_MAXNAMELEN) { + idx2 = -1; + } + else { + enc2name = ALLOCA_N(char, n+1); + memcpy(enc2name, estr, n); + enc2name[n] = '\0'; + idx2 = rb_enc_find_index(enc2name); + } + if (idx2 < 0) { + rb_warn("Unsupported encoding %s ignored", enc2name); + } + else if (idx2 == idx) { rb_warn("Ignoring internal encoding %s: it is identical to external encoding %s", enc2name, p1); } - else if (idx2 >= 0) { + else { fptr->enc2 = rb_enc_from_index(idx2); } - else { - rb_warn("Unsupported encoding %s ignored", enc2name); - } } } @@ -3523,6 +3529,7 @@ #elif defined(_WIN32) int openmode = rb_io_mode_modenum(mode); const char *exename = NULL; + volatile VALUE cmdbuf; #endif FILE *fp = 0; int fd = -1; @@ -3602,15 +3609,22 @@ } #elif defined(_WIN32) if (argc) { - char **args = ALLOCA_N(char *, argc+1); + volatile VALUE argbuf; + char **args; int i; + if (argc >= FIXNUM_MAX / sizeof(char *)) { + rb_raise(rb_eArgError, "too many arguments"); + } + argbuf = rb_str_tmp_new((argc+1) * sizeof(char *)); + args = (void *)RSTRING_PTR(argbuf); for (i = 0; i < argc; ++i) { args[i] = RSTRING_PTR(argv[i]); } args[i] = NULL; exename = cmd; - cmd = rb_w32_join_argv(ALLOCA_N(char, rb_w32_argv_size(args)), args); + cmdbuf = rb_str_tmp_new(rb_w32_argv_size(args)); + cmd = rb_w32_join_argv(RSTRING_PTR(cmdbuf), args); } while ((pid = rb_w32_pipe_exec(cmd, exename, openmode, &fd, &write_fd)) == -1) { /* exec failed */ @@ -3757,11 +3771,9 @@ } tmp = rb_check_array_type(pname); if (!NIL_P(tmp)) { - long len = RARRAY_LEN(tmp); - VALUE *args = ALLOCA_N(VALUE, len); - - MEMCPY(args, RARRAY_PTR(tmp), VALUE, len); - port = pipe_open_v(len, args, mode); + tmp = rb_ary_dup(tmp); + RBASIC(tmp)->klass = 0; + port = pipe_open_v(RARRAY_LEN(tmp), RARRAY_PTR(tmp), mode); } else { SafeStringValue(pname); Index: file.c =================================================================== --- file.c (revision 15072) +++ file.c (revision 15073) @@ -2151,11 +2151,31 @@ sys_fail2(VALUE s1, VALUE s2) { char *buf; - int len; +#ifdef MAX_PATH + const int max_pathlen = MAX_PATH; +#else + const int max_pathlen = MAXPATHLEN; +#endif + const char *e1, *e2; + int len = 5; + int l1 = RSTRING_LEN(s1), l2 = RSTRING_LEN(s2); - len = RSTRING_LEN(s1) + RSTRING_LEN(s2) + 5; + e1 = e2 = ""; + if (l1 > max_pathlen) { + l1 = max_pathlen - 3; + e1 = "..."; + len += 3; + } + if (l2 > max_pathlen) { + l2 = max_pathlen - 3; + e2 = "..."; + len += 3; + } + len += l1 + l2; buf = ALLOCA_N(char, len); - snprintf(buf, len, "(%s, %s)", RSTRING_PTR(s1), RSTRING_PTR(s2)); + snprintf(buf, len, "(%.*s%s, %.*s%s)", + l1, RSTRING_PTR(s1), e1, + l2, RSTRING_PTR(s2), e2); rb_sys_fail(buf); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/