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

* pack.c (num2i32): pack should not raise RangeError.

[ruby-dev:22654]

* pack.c (pack_pack): do not auto convert nil to zero.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5542 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2004-01-22 18:06:38 +00:00
parent edddc36970
commit 7df46a4493
5 changed files with 64 additions and 103 deletions

View file

@ -1,7 +1,15 @@
Fri Jan 23 02:26:30 2004 Yukihiro Matsumoto <matz@ruby-lang.org>
* pack.c (num2i32): pack should not raise RangeError.
[ruby-dev:22654]
* pack.c (pack_pack): do not auto convert nil to zero.
Thu Jan 22 22:54:53 2004 Shugo Maeda <shugo@ruby-lang.org> Thu Jan 22 22:54:53 2004 Shugo Maeda <shugo@ruby-lang.org>
* lib/net/imap.rb (BEG_REGEXP): allow 8-bit characters in quoted * lib/net/imap.rb (BEG_REGEXP): allow 8-bit characters in quoted
strings for Novell GroupWise Internet Agent. strings for Novell GroupWise Internet Agent.
* lib/net/imap.rb (DATA_REGEXP): ditto. * lib/net/imap.rb (DATA_REGEXP): ditto.
Thu Jan 22 18:35:49 2004 WATANABE Hirofumi <eban@ruby-lang.org> Thu Jan 22 18:35:49 2004 WATANABE Hirofumi <eban@ruby-lang.org>

View file

@ -203,8 +203,9 @@ rb_quad_pack(buf, val)
long len = RBIGNUM(val)->len; long len = RBIGNUM(val)->len;
BDIGIT *ds; BDIGIT *ds;
if (len > SIZEOF_LONG_LONG/SIZEOF_BDIGITS) if (len > SIZEOF_LONG_LONG/SIZEOF_BDIGITS) {
rb_raise(rb_eRangeError, "bignum too big to convert into `quad int'"); len = SIZEOF_LONG/SIZEOF_BDIGITS;
}
ds = BDIGITS(val); ds = BDIGITS(val);
q = 0; q = 0;
while (len--) { while (len--) {
@ -725,16 +726,20 @@ rb_big_to_s(argc, argv, x)
} }
static unsigned long static unsigned long
big2ulong(x, type) big2ulong(x, type, check)
VALUE x; VALUE x;
char *type; char *type;
int check;
{ {
long len = RBIGNUM(x)->len; long len = RBIGNUM(x)->len;
BDIGIT_DBL num; BDIGIT_DBL num;
BDIGIT *ds; BDIGIT *ds;
if (len > SIZEOF_LONG/SIZEOF_BDIGITS) if (len > SIZEOF_LONG/SIZEOF_BDIGITS) {
rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type); if (check)
rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type);
len = SIZEOF_LONG/SIZEOF_BDIGITS;
}
ds = BDIGITS(x); ds = BDIGITS(x);
num = 0; num = 0;
while (len--) { while (len--) {
@ -744,11 +749,22 @@ big2ulong(x, type)
return num; return num;
} }
unsigned long
rb_big2ulong_pack(x)
VALUE x;
{
unsigned long num = big2ulong(x, "unsigned long", Qfalse);
if (!RBIGNUM(x)->sign) {
return -num;
}
return num;
}
unsigned long unsigned long
rb_big2ulong(x) rb_big2ulong(x)
VALUE x; VALUE x;
{ {
unsigned long num = big2ulong(x, "unsigned long"); unsigned long num = big2ulong(x, "unsigned long", Qtrue);
if (!RBIGNUM(x)->sign) { if (!RBIGNUM(x)->sign) {
if ((long)num < 0) { if ((long)num < 0) {
@ -763,7 +779,7 @@ long
rb_big2long(x) rb_big2long(x)
VALUE x; VALUE x;
{ {
unsigned long num = big2ulong(x, "long"); unsigned long num = big2ulong(x, "long", Qtrue);
if ((long)num < 0 && (RBIGNUM(x)->sign || (long)num != LONG_MIN)) { if ((long)num < 0 && (RBIGNUM(x)->sign || (long)num != LONG_MIN)) {
rb_raise(rb_eRangeError, "bignum too big to convert into `long'"); rb_raise(rb_eRangeError, "bignum too big to convert into `long'");

View file

@ -2004,8 +2004,9 @@ rb_convert_type(val, type, tname, method)
if (TYPE(val) == type) return val; if (TYPE(val) == type) return val;
v = convert_type(val, tname, method, Qtrue); v = convert_type(val, tname, method, Qtrue);
if (TYPE(v) != type) { if (TYPE(v) != type) {
rb_raise(rb_eTypeError, "%s#%s should return %s", char *cname = rb_obj_classname(val);
rb_obj_classname(val), method, tname); rb_raise(rb_eTypeError, "cannot convert %s to %s (%s#%s gives %s)",
cname, tname, cname, method, rb_obj_classname(v));
} }
return v; return v;
} }
@ -2023,8 +2024,9 @@ rb_check_convert_type(val, type, tname, method)
v = convert_type(val, tname, method, Qfalse); v = convert_type(val, tname, method, Qfalse);
if (NIL_P(v)) return Qnil; if (NIL_P(v)) return Qnil;
if (TYPE(v) != type) { if (TYPE(v) != type) {
rb_raise(rb_eTypeError, "%s#%s should return %s", char *cname = rb_obj_classname(val);
rb_obj_classname(val), method, tname); rb_raise(rb_eTypeError, "cannot convert %s to %s (%s#%s gives %s)",
cname, tname, cname, method, rb_obj_classname(v));
} }
return v; return v;
} }
@ -2037,8 +2039,9 @@ rb_to_integer(val, method)
{ {
VALUE v = convert_type(val, "Integer", method, Qtrue); VALUE v = convert_type(val, "Integer", method, Qtrue);
if (!rb_obj_is_kind_of(v, rb_cInteger)) { if (!rb_obj_is_kind_of(v, rb_cInteger)) {
rb_raise(rb_eTypeError, "%s#%s should return Integer", char *cname = rb_obj_classname(val);
rb_obj_classname(val), method); rb_raise(rb_eTypeError, "cannot convert %s to Integer (%s#%s gives %s)",
cname, cname, method, rb_obj_classname(v));
} }
return v; return v;
} }

108
pack.c
View file

@ -24,8 +24,6 @@
#ifdef NATINT_PACK #ifdef NATINT_PACK
# define OFF16B(p) ((char*)(p) + (natint?0:(sizeof(short) - SIZE16))) # define OFF16B(p) ((char*)(p) + (natint?0:(sizeof(short) - SIZE16)))
# define OFF32B(p) ((char*)(p) + (natint?0:(sizeof(long) - SIZE32))) # define OFF32B(p) ((char*)(p) + (natint?0:(sizeof(long) - SIZE32)))
# define NATINT_I32(x) (natint?NUM2LONG(x):(NUM2I32(x)))
# define NATINT_U32(x) (natint?NUM2ULONG(x):(NUM2U32(x)))
# define NATINT_LEN(type,len) (natint?sizeof(type):(len)) # define NATINT_LEN(type,len) (natint?sizeof(type):(len))
# ifdef WORDS_BIGENDIAN # ifdef WORDS_BIGENDIAN
# define OFF16(p) OFF16B(p) # define OFF16(p) OFF16B(p)
@ -36,8 +34,6 @@
# define NATINT_HTONS(x) (natint?htons(x):hton16(x)) # define NATINT_HTONS(x) (natint?htons(x):hton16(x))
# define NATINT_HTONL(x) (natint?htonl(x):hton32(x)) # define NATINT_HTONL(x) (natint?htonl(x):hton32(x))
#else #else
# define NATINT_I32(x) NUM2I32(x)
# define NATINT_U32(x) NUM2U32(x)
# define NATINT_LEN(type,len) sizeof(type) # define NATINT_LEN(type,len) sizeof(type)
# define NATINT_HTOVS(x) htovs(x) # define NATINT_HTOVS(x) htovs(x)
# define NATINT_HTOVL(x) htovl(x) # define NATINT_HTOVL(x) htovl(x)
@ -335,52 +331,21 @@ endian()
#define VTOHD(x,y) vtohd(x) #define VTOHD(x,y) vtohd(x)
#endif #endif
#if SIZEOF_LONG == SIZE32 unsigned long rb_big2ulong_pack _((VALUE x));
typedef long I32;
typedef unsigned long U32;
#define NUM2I32(x) NUM2LONG(x)
#define NUM2U32(x) NUM2ULONG(x)
#else
typedef int I32;
typedef unsigned int U32;
# if SIZEOF_INT == SIZE32
# define NUM2I32(x) NUM2INT(x)
# define NUM2U32(x) NUM2UINT(x)
# else
#define I32_MAX 2147483647 static unsigned long
#define I32_MIN (-I32_MAX-1)
static I32
num2i32(x) num2i32(x)
VALUE x; VALUE x;
{ {
long num = NUM2LONG(x); x = rb_to_int(x); /* is nil OK? (should not) */
if (num < I32_MIN || I32_MAX < num) { if (FIXNUM_P(x)) return FIX2LONG(x);
rb_raise(rb_eRangeError, "integer %ld too big to convert to `I32'", num); if (TYPE(x) == T_BIGNUM) {
return rb_big2ulong_pack(x);
} }
return (I32)num; rb_raise(rb_eTypeError, "cannot conver %s to `integer'", rb_obj_classname(x));
return 0; /* not reached */
} }
#define U32_MAX 4294967295
static U32
num2u32(x)
VALUE x;
{
unsigned long num = NUM2ULONG(x);
if (U32_MAX < num) {
rb_raise(rb_eRangeError, "integer %ld too big to convert to `U32'", num);
}
return (U32)num;
}
# define NUM2I32(x) num2i32(x)
# define NUM2U32(x) num2u32(x)
# endif
#endif
#if SIZEOF_LONG == SIZE32 || SIZEOF_INT == SIZE32 #if SIZEOF_LONG == SIZE32 || SIZEOF_INT == SIZE32
# define EXTEND32(x) # define EXTEND32(x)
@ -512,7 +477,7 @@ pack_pack(ary, fmt)
continue; continue;
} }
if (*p == '_' || *p == '!') { if (*p == '_' || *p == '!') {
char *natstr = "sSiIlL"; const char *natstr = "sSiIlL";
if (strchr(natstr, type)) { if (strchr(natstr, type)) {
#ifdef NATINT_PACK #ifdef NATINT_PACK
@ -702,10 +667,7 @@ pack_pack(ary, fmt)
char c; char c;
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) c = 0; c = num2i32(from);
else {
c = NUM2INT(from);
}
rb_str_buf_cat(res, &c, sizeof(char)); rb_str_buf_cat(res, &c, sizeof(char));
} }
break; break;
@ -716,10 +678,7 @@ pack_pack(ary, fmt)
short s; short s;
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) s = 0; s = num2i32(from);
else {
s = NUM2INT(from);
}
rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2)); rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
} }
break; break;
@ -730,13 +689,7 @@ pack_pack(ary, fmt)
long i; long i;
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) i = 0; i = num2i32(from);
else if (type == 'i') {
i = NATINT_I32(from);
}
else {
i = NATINT_U32(from);
}
rb_str_buf_cat(res, OFF32(&i), NATINT_LEN(int,4)); rb_str_buf_cat(res, OFF32(&i), NATINT_LEN(int,4));
} }
break; break;
@ -747,13 +700,7 @@ pack_pack(ary, fmt)
long l; long l;
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) l = 0; l = num2i32(from);
else if (type == 'l') {
l = NATINT_I32(from);
}
else {
l = NATINT_U32(from);
}
rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4)); rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
} }
break; break;
@ -764,7 +711,6 @@ pack_pack(ary, fmt)
char tmp[QUAD_SIZE]; char tmp[QUAD_SIZE];
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) from = INT2FIX(0);
rb_quad_pack(tmp, from); rb_quad_pack(tmp, from);
rb_str_buf_cat(res, (char*)&tmp, QUAD_SIZE); rb_str_buf_cat(res, (char*)&tmp, QUAD_SIZE);
} }
@ -775,10 +721,7 @@ pack_pack(ary, fmt)
unsigned short s; unsigned short s;
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) s = 0; s = num2i32(from);
else {
s = NUM2INT(from);
}
s = NATINT_HTONS(s); s = NATINT_HTONS(s);
rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2)); rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
} }
@ -789,10 +732,7 @@ pack_pack(ary, fmt)
unsigned long l; unsigned long l;
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) l = 0; l = num2i32(from);
else {
l = NATINT_U32(from);
}
l = NATINT_HTONL(l); l = NATINT_HTONL(l);
rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4)); rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
} }
@ -803,10 +743,7 @@ pack_pack(ary, fmt)
unsigned short s; unsigned short s;
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) s = 0; s = num2i32(from);
else {
s = NUM2INT(from);
}
s = NATINT_HTOVS(s); s = NATINT_HTOVS(s);
rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2)); rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
} }
@ -817,10 +754,7 @@ pack_pack(ary, fmt)
unsigned long l; unsigned long l;
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) l = 0; l = num2i32(from);
else {
l = NATINT_U32(from);
}
l = NATINT_HTOVL(l); l = NATINT_HTOVL(l);
rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4)); rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
} }
@ -932,10 +866,7 @@ pack_pack(ary, fmt)
int le; int le;
from = NEXTFROM; from = NEXTFROM;
if (NIL_P(from)) l = 0; l = num2i32(from);
else {
l = NUM2UINT(from);
}
le = uv_to_utf8(buf, l); le = uv_to_utf8(buf, l);
rb_str_buf_cat(res, (char*)buf, le); rb_str_buf_cat(res, (char*)buf, le);
} }
@ -1018,8 +949,7 @@ pack_pack(ary, fmt)
} }
} }
if (NIL_P(from)) ul = 0; {
else {
long l = NUM2LONG(from); long l = NUM2LONG(from);
if (l < 0) { if (l < 0) {
rb_raise(rb_eArgError, "cannot compress negative numbers"); rb_raise(rb_eArgError, "cannot compress negative numbers");

View file

@ -27,6 +27,10 @@ class TestPack < Test::Unit::TestCase
assert_equal "\377\377\377\376", [4294967294].pack('N') assert_equal "\377\377\377\376", [4294967294].pack('N')
assert_equal "\377\377\377\377", [4294967295].pack('N') assert_equal "\377\377\377\377", [4294967295].pack('N')
assert_equal "\200\000\000\000", [2**31].pack('N')
assert_equal "\177\377\377\377", [-2**31-1].pack('N')
assert_equal "\377\377\377\377", [-1].pack('N')
assert_equal "\000\000\000\001\000\000\000\001", [1,1].pack('N*') assert_equal "\000\000\000\001\000\000\000\001", [1,1].pack('N*')
assert_equal "\000\000\000\001\000\000\000\001\000\000\000\001", [1,1,1].pack('N*') assert_equal "\000\000\000\001\000\000\000\001\000\000\000\001", [1,1,1].pack('N*')
end end