From 2e4a7f153fd49318595f73f20dfa3a3d156cc28d Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Mon, 3 Aug 2009 11:08:10 -0400 Subject: [PATCH] raise a more clear exception for range error on Fixnums on 64-bit --- ext/cbson/cbson.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/cbson/cbson.c b/ext/cbson/cbson.c index b3cc2e4..71e3d82 100644 --- a/ext/cbson/cbson.c +++ b/ext/cbson/cbson.c @@ -198,6 +198,11 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow case T_FIXNUM: { int int_value = FIX2INT(value); + if (rb_funcall(value, rb_intern(">"), 1, INT2NUM(2147483647)) == Qtrue || + rb_funcall(value, rb_intern("<"), 1, INT2NUM(-2147483648)) == Qtrue) { + rb_raise(rb_eRangeError, "MongoDB can only handle 4-byte ints" + " - try converting to a double before saving"); + } write_name_and_type(buffer, key, 0x10); buffer_write_bytes(buffer, (char*)&int_value, 4); break;