aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-01-16 05:55:22 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-01-16 05:55:22 +0000
commit2402ecabb064525651667374a0f758321bbc7f65 (patch)
tree5c858bef621fa3e0276b617d9c3d8b1071298a9c
parentce2652c9d46acd102d80fe645000ad31dc45ebbb (diff)
downloadruby-2402ecabb064525651667374a0f758321bbc7f65.tar.gz
* file.c (sys_fail2): get rid of unlimited alloca.
* io.c (mode_enc, pipe_open, rb_io_s_popen): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15073 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--file.c26
-rw-r--r--include/ruby/encoding.h2
-rw-r--r--io.c44
4 files changed, 59 insertions, 19 deletions
diff --git a/ChangeLog b/ChangeLog
index ea709966b1..47fa70bbb2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Jan 16 14:55:15 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* include/ruby/intern.h (rb_str_tmp_new, rb_str_shared_replace):
diff --git a/file.c b/file.c
index 452f7ff18f..18caf6b8a8 100644
--- a/file.c
+++ b/file.c
@@ -2151,11 +2151,31 @@ static void
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);
}
diff --git a/include/ruby/encoding.h b/include/ruby/encoding.h
index 6536cb51ae..b914913b4d 100644
--- a/include/ruby/encoding.h
+++ b/include/ruby/encoding.h
@@ -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
diff --git a/io.c b/io.c
index 7d48f90f82..75c1fa3025 100644
--- a/io.c
+++ b/io.c
@@ -3184,19 +3184,25 @@ mode_enc(rb_io_t *fptr, const char *estr)
}
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) {
- fptr->enc2 = rb_enc_from_index(idx2);
- }
else {
- rb_warn("Unsupported encoding %s ignored", enc2name);
+ fptr->enc2 = rb_enc_from_index(idx2);
}
}
}
@@ -3523,6 +3529,7 @@ pipe_open(const char *cmd, int argc, VALUE *argv, const char *mode)
#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 @@ pipe_open(const char *cmd, int argc, VALUE *argv, const char *mode)
}
#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 @@ rb_io_s_popen(int argc, VALUE *argv, VALUE klass)
}
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);