Skip to content

Commit

Permalink
* time.c (time_strftime): format should be ascii compatible.
Browse files Browse the repository at this point in the history
* parse.y (rb_intern3): non ASCII compatible symbols.

* re.c (rb_reg_regsub): add encoding check.

* string.c (rb_str_chomp_bang): ditto.

* test/ruby/test_utf16.rb (TestUTF16::test_chomp): raises exception.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15640 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Feb 29, 2008
1 parent deff2a3 commit daa622a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
12 changes: 12 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
Fri Feb 29 18:08:43 2008 Yukihiro Matsumoto <matz@ruby-lang.org>

* time.c (time_strftime): format should be ascii compatible.

* parse.y (rb_intern3): non ASCII compatible symbols.

* re.c (rb_reg_regsub): add encoding check.

* string.c (rb_str_chomp_bang): ditto.

* test/ruby/test_utf16.rb (TestUTF16::test_chomp): raises exception.

Fri Feb 29 15:16:31 2008 Yukihiro Matsumoto <matz@ruby-lang.org>

* string.c (rb_str_rpartition): calculation was done in byte indexing.
Expand Down
4 changes: 4 additions & 0 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -9008,6 +9008,10 @@ rb_intern3(const char *name, long len, rb_encoding *enc)
if (st_lookup(global_symbols.sym_id, str, (st_data_t *)&id))
return id;

if (rb_cString && !rb_enc_asciicompat(enc)) {
id = ID_JUNK;
goto new_id;
}
last = len-1;
id = 0;
switch (*m) {
Expand Down
1 change: 1 addition & 0 deletions re.c
Original file line number Diff line number Diff line change
Expand Up @@ -2960,6 +2960,7 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
rb_encoding *str_enc = rb_enc_get(str);
rb_encoding *src_enc = rb_enc_get(src);

rb_enc_check(str, src);
p = s = RSTRING_PTR(str);
e = s + RSTRING_LEN(str);

Expand Down
11 changes: 11 additions & 0 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5273,6 +5273,7 @@ rb_str_chomp_bang(int argc, VALUE *argv, VALUE str)
rs = rb_rs;
if (rs == rb_default_rs) {
smart_chomp:
rb_enc_check(str, rs);
rb_str_modify(str);
if (RSTRING_PTR(str)[len-1] == '\n') {
STR_DEC_LEN(str);
Expand Down Expand Up @@ -5648,6 +5649,11 @@ rb_str_scan(VALUE str, VALUE pat)
static VALUE
rb_str_hex(VALUE str)
{
rb_encoding *enc = rb_enc_get(str);

if (!rb_enc_asciicompat(enc)) {
rb_raise(rb_eArgError, "ASCII incompatible encoding: %s", rb_enc_name(enc));
}
return rb_str_to_inum(str, 16, Qfalse);
}

Expand All @@ -5669,6 +5675,11 @@ rb_str_hex(VALUE str)
static VALUE
rb_str_oct(VALUE str)
{
rb_encoding *enc = rb_enc_get(str);

if (!rb_enc_asciicompat(enc)) {
rb_raise(rb_eArgError, "ASCII incompatible encoding: %s", rb_enc_name(enc));
}
return rb_str_to_inum(str, -8, Qfalse);
}

Expand Down
20 changes: 15 additions & 5 deletions test/ruby/test_utf16.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,21 @@ def test_end_with
end

def test_hex
s1 = "f\0f\0".force_encoding("utf-16le")
assert_equal(255, s1.hex, "#{encdump s1}.hex")
assert_raise(ArgumentError) {
"ff".encode("utf-16le").hex
}
assert_raise(ArgumentError) {
"ff".encode("utf-16be").hex
}
end

def test_oct
assert_equal(077, "77".encode("utf-16le").oct)
assert_equal(077, "77".encode("utf-16be").oct)
assert_raise(ArgumentError) {
"77".encode("utf-16le").oct
}
assert_raise(ArgumentError) {
"77".encode("utf-16be").oct
}
end

def test_count
Expand Down Expand Up @@ -224,7 +232,9 @@ def test_concat_nonempty

def test_chomp
s = "\1\n".force_encoding("utf-16be")
assert_str_equal(s, s.chomp, "#{encdump s}.chomp")
assert_raise(ArgumentError, "#{encdump s}.chomp") {
s.chomp
}
end

def test_succ
Expand Down
4 changes: 4 additions & 0 deletions time.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "ruby/ruby.h"
#include <sys/types.h>
#include <time.h>
#include "ruby/encoding.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
Expand Down Expand Up @@ -2077,6 +2078,9 @@ time_strftime(VALUE time, VALUE format)
time_get_tm(time, tobj->gmt);
}
StringValue(format);
if (!rb_enc_str_asciicompat_p(format)) {
rb_raise(rb_eArgError, "format should have ASCII compatible encoding");
}
format = rb_str_new4(format);
fmt = RSTRING_PTR(format);
len = RSTRING_LEN(format);
Expand Down

0 comments on commit daa622a

Please sign in to comment.