diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2013-12-04 21:07:54 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2013-12-04 21:07:54 -0800 |
commit | b518d764586aec0c01fee6273f0a77ad2dcc0b20 (patch) | |
tree | 49d26a31bd7f35e6124aceeeb11327a42d31159e /ext/ffi_yajl/ext/encoder | |
parent | c1380ba8f3ab689e0020ffe3603ac51e1885d69e (diff) | |
download | ffi-yajl-b518d764586aec0c01fee6273f0a77ad2dcc0b20.tar.gz |
fix NAN and Infinity handling for C extension
Diffstat (limited to 'ext/ffi_yajl/ext/encoder')
-rw-r--r-- | ext/ffi_yajl/ext/encoder/encoder.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/ext/ffi_yajl/ext/encoder/encoder.c b/ext/ffi_yajl/ext/encoder/encoder.c index 004a11b..ebfd1bb 100644 --- a/ext/ffi_yajl/ext/encoder/encoder.c +++ b/ext/ffi_yajl/ext/encoder/encoder.c @@ -1,7 +1,7 @@ #include <ruby.h> #include <yajl/yajl_gen.h> -static VALUE mFFI_Yajl, mExt, mEncoder; +static VALUE mFFI_Yajl, mExt, mEncoder, cEncodeError; /* FIXME: the json gem does a whole bunch of indirection around monkeypatching... not sure if we need to as well... */ @@ -130,6 +130,12 @@ static VALUE rb_cBignum_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { } static VALUE rb_cFloat_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { + ID sym_to_s = rb_intern("to_s"); + VALUE str = rb_funcall(self, sym_to_s, 0); + char *cptr = RSTRING_PTR(str); + if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) { + rb_raise(cEncodeError, "'%s' is an invalid number", cptr); + } yajl_gen_double((struct yajl_gen_t *) yajl_gen, NUM2DBL(self)); return Qnil; } @@ -160,6 +166,7 @@ static VALUE rb_cObject_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { void Init_encoder() { mFFI_Yajl = rb_define_module("FFI_Yajl"); + cEncodeError = rb_define_class_under(mFFI_Yajl, "EncodeError", rb_eStandardError); mExt = rb_define_module_under(mFFI_Yajl, "Ext"); mEncoder = rb_define_module_under(mExt, "Encoder"); rb_define_method(mEncoder, "do_yajl_encode", mEncoder_do_yajl_encode, 2); |