bson 4.8.1 → 4.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +18 -6
  5. data/ext/bson/bson-endian.h +1 -1
  6. data/ext/bson/bson-native.h +1 -1
  7. data/ext/bson/bytebuf.c +1 -1
  8. data/ext/bson/endian.c +1 -1
  9. data/ext/bson/init.c +24 -22
  10. data/ext/bson/read.c +67 -19
  11. data/ext/bson/util.c +1 -1
  12. data/ext/bson/write.c +1 -1
  13. data/lib/bson.rb +1 -1
  14. data/lib/bson/active_support.rb +1 -1
  15. data/lib/bson/array.rb +14 -3
  16. data/lib/bson/binary.rb +1 -1
  17. data/lib/bson/boolean.rb +9 -2
  18. data/lib/bson/code.rb +1 -1
  19. data/lib/bson/code_with_scope.rb +16 -3
  20. data/lib/bson/config.rb +1 -1
  21. data/lib/bson/date.rb +1 -1
  22. data/lib/bson/date_time.rb +1 -1
  23. data/lib/bson/db_pointer.rb +7 -1
  24. data/lib/bson/decimal128.rb +1 -1
  25. data/lib/bson/decimal128/builder.rb +1 -1
  26. data/lib/bson/document.rb +1 -1
  27. data/lib/bson/environment.rb +2 -1
  28. data/lib/bson/error.rb +4 -0
  29. data/lib/bson/ext_json.rb +3 -3
  30. data/lib/bson/false_class.rb +1 -1
  31. data/lib/bson/float.rb +1 -1
  32. data/lib/bson/hash.rb +7 -2
  33. data/lib/bson/int32.rb +1 -1
  34. data/lib/bson/int64.rb +1 -1
  35. data/lib/bson/integer.rb +1 -1
  36. data/lib/bson/json.rb +1 -1
  37. data/lib/bson/max_key.rb +1 -1
  38. data/lib/bson/min_key.rb +1 -1
  39. data/lib/bson/nil_class.rb +1 -1
  40. data/lib/bson/object.rb +1 -1
  41. data/lib/bson/object_id.rb +1 -1
  42. data/lib/bson/open_struct.rb +1 -1
  43. data/lib/bson/regexp.rb +8 -2
  44. data/lib/bson/registry.rb +1 -1
  45. data/lib/bson/specialized.rb +1 -1
  46. data/lib/bson/string.rb +1 -1
  47. data/lib/bson/symbol.rb +1 -1
  48. data/lib/bson/time.rb +1 -1
  49. data/lib/bson/time_with_zone.rb +1 -1
  50. data/lib/bson/timestamp.rb +1 -1
  51. data/lib/bson/true_class.rb +1 -1
  52. data/lib/bson/undefined.rb +1 -1
  53. data/lib/bson/version.rb +2 -2
  54. data/spec/bson/array_spec.rb +1 -1
  55. data/spec/bson/binary_spec.rb +1 -1
  56. data/spec/bson/binary_uuid_spec.rb +1 -1
  57. data/spec/bson/boolean_spec.rb +1 -1
  58. data/spec/bson/code_spec.rb +1 -1
  59. data/spec/bson/code_with_scope_spec.rb +1 -1
  60. data/spec/bson/date_spec.rb +1 -1
  61. data/spec/bson/date_time_spec.rb +1 -1
  62. data/spec/bson/decimal128_spec.rb +1 -1
  63. data/spec/bson/document_spec.rb +1 -1
  64. data/spec/bson/ext_json_parse_spec.rb +34 -2
  65. data/spec/bson/false_class_spec.rb +1 -1
  66. data/spec/bson/float_spec.rb +1 -1
  67. data/spec/bson/hash_spec.rb +1 -1
  68. data/spec/bson/int32_spec.rb +1 -1
  69. data/spec/bson/int64_spec.rb +1 -1
  70. data/spec/bson/integer_spec.rb +1 -1
  71. data/spec/bson/json_spec.rb +1 -1
  72. data/spec/bson/max_key_spec.rb +1 -1
  73. data/spec/bson/min_key_spec.rb +1 -1
  74. data/spec/bson/nil_class_spec.rb +1 -1
  75. data/spec/bson/object_id_spec.rb +1 -1
  76. data/spec/bson/object_spec.rb +1 -1
  77. data/spec/bson/open_struct_spec.rb +1 -1
  78. data/spec/bson/raw_spec.rb +12 -1
  79. data/spec/bson/regexp_spec.rb +1 -1
  80. data/spec/bson/registry_spec.rb +1 -1
  81. data/spec/bson/string_spec.rb +1 -1
  82. data/spec/bson/symbol_spec.rb +1 -1
  83. data/spec/bson/time_spec.rb +1 -1
  84. data/spec/bson/time_with_zone_spec.rb +1 -1
  85. data/spec/bson/timestamp_spec.rb +1 -1
  86. data/spec/bson/true_class_spec.rb +1 -1
  87. data/spec/bson/undefined_spec.rb +1 -1
  88. data/spec/bson_spec.rb +1 -1
  89. data/spec/runners/common_driver.rb +1 -1
  90. data/spec/runners/corpus.rb +13 -10
  91. data/spec/runners/corpus_legacy.rb +1 -1
  92. data/spec/spec_helper.rb +12 -1
  93. data/spec/spec_tests/data/corpus/top.json +0 -4
  94. data/spec/support/shared_examples.rb +1 -1
  95. data/spec/support/spec_config.rb +6 -0
  96. metadata +92 -94
  97. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef5a31f0820c55727982451d554e05971480a6df19a3a959eea4731326193614
4
- data.tar.gz: 8928dec44431fa67fba6f5b95a469ee5e8c8d17d12880ac5c35aa556acff697a
3
+ metadata.gz: 1d24a5aa6f5678defced03d887f8d946a7c03992cff980281138bb5836b83366
4
+ data.tar.gz: b22e4544322858b2473b83f4842ba7074efd9a2183c181818ffe7120ae85bf1d
5
5
  SHA512:
6
- metadata.gz: 48e4afe1f4da2dd9161e326f05c9c3dcf0f287845eb46fb3eebfe08e761df0e73d36bf0745867fca70ace7345d8a243826da9a0f89f9b3c4a28e903b74817949
7
- data.tar.gz: b40043516efec6952d2f83ffa84183ed4e7e23a545ed9b51e12729d65f6a944131468ea4278dccf4cdde5e6ec9f859bc9cad04aeae167f9a143ebe62731df9e3
6
+ metadata.gz: f54379785c0d7c5dbd9e75919aa8ab5f0b6fa04158a2f4789c47bf763b763901c0a64255f4aadffc62a76cc3068e57d31352a49e5acc7fd5e6766fe7d7a32b3b
7
+ data.tar.gz: 667307d98f30bfc0ea6a3a15bdfed67fd9a935c4512504bae9a2f545d2e1730f1105facf4acd11d03a315efacb877d531029feec01b77d7a54d1182df297b8ae
Binary file
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
- BSON [![Build Status](https://secure.travis-ci.org/mongodb/bson-ruby.svg?branch=master)](http://travis-ci.org/mongodb/bson-ruby)
2
- [![Build status Windows](https://ci.appveyor.com/api/projects/status/p5aqko7umsx351nm?svg=true)](https://ci.appveyor.com/project/p-mongo/bson-ruby/branch/master)
3
- [![Code Climate](https://codeclimate.com/github/mongodb/bson-ruby.svg)](https://codeclimate.com/github/mongodb/bson-ruby)
4
- [![Coverage Status](https://coveralls.io/repos/mongodb/bson-ruby/badge.svg?branch=master)](https://coveralls.io/r/mongodb/bson-ruby?branch=master)
5
- [![Inline docs](http://inch-ci.org/github/mongodb/bson-ruby.svg?branch=master)](http://inch-ci.org/github/mongodb/bson-ruby)
1
+ BSON
2
+ [![Gem Version][rubygems-img]][rubygems-url]
3
+ [![Build Status][travis-img]][travis-url]
4
+ [![Build status Windows][appveyor-img]][appveyor-url]
5
+ [![Coverage Status][coveralls-img]][coveralls-url]
6
+ [![Inline docs][inch-img]][inch-url]
6
7
  ====
7
8
 
8
9
  An implementation of the BSON specification in Ruby.
@@ -38,7 +39,7 @@ As of 2.0.0, this project adheres to the
38
39
  License
39
40
  -------
40
41
 
41
- Copyright (C) 2009-2019 MongoDB Inc.
42
+ Copyright (C) 2009-2020 MongoDB Inc.
42
43
 
43
44
  Licensed under the Apache License, Version 2.0 (the "License");
44
45
  you may not use this file except in compliance with the License.
@@ -51,3 +52,14 @@ distributed under the License is distributed on an "AS IS" BASIS,
51
52
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
52
53
  See the License for the specific language governing permissions and
53
54
  limitations under the License.
55
+
56
+ [rubygems-img]: https://badge.fury.io/rb/bson.svg
57
+ [rubygems-url]: http://badge.fury.io/rb/bson
58
+ [travis-img]: https://secure.travis-ci.org/mongodb/bson-ruby.svg?branch=master
59
+ [travis-url]: http://travis-ci.org/mongodb/bson-ruby
60
+ [appveyor-img]: https://ci.appveyor.com/api/projects/status/p5aqko7umsx351nm?svg=true
61
+ [appveyor-url]: https://ci.appveyor.com/project/p-mongo/bson-ruby/branch/master
62
+ [coveralls-img]: https://coveralls.io/repos/mongodb/bson-ruby/badge.svg?branch=master
63
+ [coveralls-url]: https://coveralls.io/r/mongodb/bson-ruby?branch=master
64
+ [inch-img]: http://inch-ci.org/github/mongodb/bson-ruby.svg?branch=master
65
+ [inch-url]: http://inch-ci.org/github/mongodb/bson-ruby
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (C) 2015-2019 MongoDB, Inc.
2
+ * Copyright (C) 2015-2020 MongoDB Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (C) 2009-2019 MongoDB, Inc.
2
+ * Copyright (C) 2009-2020 MongoDB Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (C) 2009-2019 MongoDB, Inc.
2
+ * Copyright (C) 2009-2020 MongoDB Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (C) 2015-2019 MongoDB, Inc.
2
+ * Copyright (C) 2015-2020 MongoDB Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (C) 2009-2019 MongoDB Inc.
2
+ * Copyright (C) 2009-2020 MongoDB Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -38,25 +38,26 @@ void Init_bson_native()
38
38
  char rb_bson_machine_id[256];
39
39
 
40
40
  VALUE rb_bson_module = rb_define_module("BSON");
41
-
41
+
42
42
  /* Document-class: BSON::ByteBuffer
43
43
  *
44
44
  * Stores BSON-serialized data and provides efficient serialization and
45
45
  * deserialization of common Ruby classes using native code.
46
46
  */
47
47
  VALUE rb_byte_buffer_class = rb_define_class_under(rb_bson_module, "ByteBuffer", rb_cObject);
48
-
48
+
49
49
  VALUE rb_bson_object_id_class = rb_const_get(rb_bson_module, rb_intern("ObjectId"));
50
50
  VALUE rb_bson_object_id_generator_class = rb_const_get(rb_bson_object_id_class, rb_intern("Generator"));
51
51
  VALUE rb_digest_class = rb_const_get(rb_cObject, rb_intern("Digest"));
52
52
  VALUE rb_md5_class = rb_const_get(rb_digest_class, rb_intern("MD5"));
53
53
 
54
54
  rb_bson_illegal_key = rb_const_get(rb_const_get(rb_bson_module, rb_intern("String")),rb_intern("IllegalKey"));
55
+ rb_gc_register_mark_object(rb_bson_illegal_key);
55
56
 
56
57
  rb_define_alloc_func(rb_byte_buffer_class, rb_bson_byte_buffer_allocate);
57
58
  rb_define_method(rb_byte_buffer_class, "initialize", rb_bson_byte_buffer_initialize, -1);
58
59
  rb_define_method(rb_byte_buffer_class, "length", rb_bson_byte_buffer_length, 0);
59
-
60
+
60
61
  /*
61
62
  * call-seq:
62
63
  * buffer.read_position -> Fixnum
@@ -64,13 +65,13 @@ void Init_bson_native()
64
65
  * Returns the read position in the buffer.
65
66
  */
66
67
  rb_define_method(rb_byte_buffer_class, "read_position", rb_bson_byte_buffer_read_position, 0);
67
-
68
+
68
69
  rb_define_method(rb_byte_buffer_class, "get_byte", rb_bson_byte_buffer_get_byte, 0);
69
70
  rb_define_method(rb_byte_buffer_class, "get_bytes", rb_bson_byte_buffer_get_bytes, 1);
70
71
  rb_define_method(rb_byte_buffer_class, "get_cstring", rb_bson_byte_buffer_get_cstring, 0);
71
72
  rb_define_method(rb_byte_buffer_class, "get_decimal128_bytes", rb_bson_byte_buffer_get_decimal128_bytes, 0);
72
73
  rb_define_method(rb_byte_buffer_class, "get_double", rb_bson_byte_buffer_get_double, 0);
73
-
74
+
74
75
  /*
75
76
  * call-seq:
76
77
  * buffer.get_hash(**options) -> Hash
@@ -82,7 +83,7 @@ void Init_bson_native()
82
83
  * @return [ BSON::Document ] The decoded document.
83
84
  */
84
85
  rb_define_method(rb_byte_buffer_class, "get_hash", rb_bson_byte_buffer_get_hash, -1);
85
-
86
+
86
87
  /*
87
88
  * call-seq:
88
89
  * buffer.get_array(**options) -> Array
@@ -94,11 +95,11 @@ void Init_bson_native()
94
95
  * @return [ Array ] The decoded array.
95
96
  */
96
97
  rb_define_method(rb_byte_buffer_class, "get_array", rb_bson_byte_buffer_get_array, -1);
97
-
98
+
98
99
  rb_define_method(rb_byte_buffer_class, "get_int32", rb_bson_byte_buffer_get_int32, 0);
99
100
  rb_define_method(rb_byte_buffer_class, "get_int64", rb_bson_byte_buffer_get_int64, 0);
100
101
  rb_define_method(rb_byte_buffer_class, "get_string", rb_bson_byte_buffer_get_string, 0);
101
-
102
+
102
103
  /*
103
104
  * call-seq:
104
105
  * buffer.write_position -> Fixnum
@@ -106,7 +107,7 @@ void Init_bson_native()
106
107
  * Returns the write position in the buffer.
107
108
  */
108
109
  rb_define_method(rb_byte_buffer_class, "write_position", rb_bson_byte_buffer_write_position, 0);
109
-
110
+
110
111
  /*
111
112
  * call-seq:
112
113
  * buffer.put_byte(binary_str) -> ByteBuffer
@@ -117,7 +118,7 @@ void Init_bson_native()
117
118
  * Returns the modified +self+.
118
119
  */
119
120
  rb_define_method(rb_byte_buffer_class, "put_byte", rb_bson_byte_buffer_put_byte, 1);
120
-
121
+
121
122
  /*
122
123
  * call-seq:
123
124
  * buffer.put_bytes(binary_str) -> ByteBuffer
@@ -170,7 +171,7 @@ void Init_bson_native()
170
171
  * instead on the null terminator), unlike the BSON string serialization.
171
172
  */
172
173
  rb_define_method(rb_byte_buffer_class, "put_cstring", rb_bson_byte_buffer_put_cstring, 1);
173
-
174
+
174
175
  /**
175
176
  * call-seq:
176
177
  * buffer.put_symbol(sym) -> ByteBuffer
@@ -188,7 +189,7 @@ void Init_bson_native()
188
189
  * indistinguishable from a string with the same value written to the buffer.
189
190
  */
190
191
  rb_define_method(rb_byte_buffer_class, "put_symbol", rb_bson_byte_buffer_put_symbol, 1);
191
-
192
+
192
193
  /*
193
194
  * call-seq:
194
195
  * buffer.put_int32(fixnum) -> ByteBuffer
@@ -200,7 +201,7 @@ void Init_bson_native()
200
201
  * Returns the modified +self+.
201
202
  */
202
203
  rb_define_method(rb_byte_buffer_class, "put_int32", rb_bson_byte_buffer_put_int32, 1);
203
-
204
+
204
205
  /*
205
206
  * call-seq:
206
207
  * buffer.put_int64(fixnum) -> ByteBuffer
@@ -212,7 +213,7 @@ void Init_bson_native()
212
213
  * Returns the modified +self+.
213
214
  */
214
215
  rb_define_method(rb_byte_buffer_class, "put_int64", rb_bson_byte_buffer_put_int64, 1);
215
-
216
+
216
217
  /*
217
218
  * call-seq:
218
219
  * buffer.put_double(double) -> ByteBuffer
@@ -222,7 +223,7 @@ void Init_bson_native()
222
223
  * Returns the modified +self+.
223
224
  */
224
225
  rb_define_method(rb_byte_buffer_class, "put_double", rb_bson_byte_buffer_put_double, 1);
225
-
226
+
226
227
  /*
227
228
  * call-seq:
228
229
  * buffer.put_decimal128(low_64bit, high_64bit) -> ByteBuffer
@@ -235,7 +236,7 @@ void Init_bson_native()
235
236
  * Returns the modified +self+.
236
237
  */
237
238
  rb_define_method(rb_byte_buffer_class, "put_decimal128", rb_bson_byte_buffer_put_decimal128, 2);
238
-
239
+
239
240
  /*
240
241
  * call-seq:
241
242
  * buffer.put_hash(hash) -> ByteBuffer
@@ -245,7 +246,7 @@ void Init_bson_native()
245
246
  * Returns the modified +self+.
246
247
  */
247
248
  rb_define_method(rb_byte_buffer_class, "put_hash", rb_bson_byte_buffer_put_hash, 2);
248
-
249
+
249
250
  /*
250
251
  * call-seq:
251
252
  * buffer.put_array(array) -> ByteBuffer
@@ -255,7 +256,7 @@ void Init_bson_native()
255
256
  * Returns the modified +self+.
256
257
  */
257
258
  rb_define_method(rb_byte_buffer_class, "put_array", rb_bson_byte_buffer_put_array, 2);
258
-
259
+
259
260
  /*
260
261
  * call-seq:
261
262
  * buffer.replace_int32(position, fixnum) -> ByteBuffer
@@ -272,7 +273,7 @@ void Init_bson_native()
272
273
  * Returns the modified +self+.
273
274
  */
274
275
  rb_define_method(rb_byte_buffer_class, "replace_int32", rb_bson_byte_buffer_replace_int32, 2);
275
-
276
+
276
277
  /*
277
278
  * call-seq:
278
279
  * buffer.rewind! -> ByteBuffer
@@ -284,7 +285,7 @@ void Init_bson_native()
284
285
  * Returns the modified +self+.
285
286
  */
286
287
  rb_define_method(rb_byte_buffer_class, "rewind!", rb_bson_byte_buffer_rewind, 0);
287
-
288
+
288
289
  /*
289
290
  * call-seq:
290
291
  * buffer.to_s -> String
@@ -296,7 +297,7 @@ void Init_bson_native()
296
297
  * the buffer itself.
297
298
  */
298
299
  rb_define_method(rb_byte_buffer_class, "to_s", rb_bson_byte_buffer_to_s, 0);
299
-
300
+
300
301
  rb_define_method(rb_bson_object_id_generator_class, "next_object_id", rb_bson_object_id_generator_next, -1);
301
302
 
302
303
  // Get the object id machine id and hash it.
@@ -309,4 +310,5 @@ void Init_bson_native()
309
310
  rb_bson_object_id_counter = FIX2INT(rb_funcall(rb_mKernel, rb_intern("rand"), 1, INT2FIX(0x1000000)));
310
311
 
311
312
  rb_bson_registry = rb_const_get(rb_bson_module, rb_intern("Registry"));
313
+ rb_gc_register_mark_object(rb_bson_registry);
312
314
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (C) 2009-2019 MongoDB, Inc.
2
+ * Copyright (C) 2009-2020 MongoDB Inc.
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
@@ -17,22 +17,28 @@
17
17
  #include "bson-native.h"
18
18
  #include <ruby/encoding.h>
19
19
 
20
- static void pvt_validate_length(byte_buffer_t *b);
20
+ static void pvt_raise_decode_error(volatile VALUE msg);
21
+ static int32_t pvt_validate_length(byte_buffer_t *b);
21
22
  static uint8_t pvt_get_type_byte(byte_buffer_t *b);
22
23
  static VALUE pvt_get_int32(byte_buffer_t *b);
23
24
  static VALUE pvt_get_int64(byte_buffer_t *b, int argc, VALUE *argv);
24
25
  static VALUE pvt_get_double(byte_buffer_t *b);
25
- static VALUE pvt_get_string(byte_buffer_t *b);
26
+ static VALUE pvt_get_string(byte_buffer_t *b, const char *data_type);
26
27
  static VALUE pvt_get_symbol(byte_buffer_t *b, VALUE rb_buffer, int argc, VALUE *argv);
27
28
  static VALUE pvt_get_boolean(byte_buffer_t *b);
28
29
  static VALUE pvt_read_field(byte_buffer_t *b, VALUE rb_buffer, uint8_t type, int argc, VALUE *argv);
29
30
  static void pvt_skip_cstring(byte_buffer_t *b);
30
31
 
32
+ void pvt_raise_decode_error(volatile VALUE msg) {
33
+ VALUE klass = pvt_const_get_3("BSON", "Error", "BSONDecodeError");
34
+ rb_exc_raise(rb_exc_new_str(klass, msg));
35
+ }
36
+
31
37
  /**
32
38
  * validate the buffer contains the amount of bytes the array / hash claimns
33
39
  * and that it is null terminated
34
40
  */
35
- void pvt_validate_length(byte_buffer_t *b)
41
+ int32_t pvt_validate_length(byte_buffer_t *b)
36
42
  {
37
43
  int32_t length;
38
44
 
@@ -53,6 +59,8 @@ void pvt_validate_length(byte_buffer_t *b)
53
59
  else{
54
60
  rb_raise(rb_eRangeError, "Buffer contained invalid length %d at %zu", length, b->read_position);
55
61
  }
62
+
63
+ return length;
56
64
  }
57
65
 
58
66
  /**
@@ -64,7 +72,7 @@ VALUE pvt_read_field(byte_buffer_t *b, VALUE rb_buffer, uint8_t type, int argc,
64
72
  case BSON_TYPE_INT32: return pvt_get_int32(b);
65
73
  case BSON_TYPE_INT64: return pvt_get_int64(b, argc, argv);
66
74
  case BSON_TYPE_DOUBLE: return pvt_get_double(b);
67
- case BSON_TYPE_STRING: return pvt_get_string(b);
75
+ case BSON_TYPE_STRING: return pvt_get_string(b, "String");
68
76
  case BSON_TYPE_SYMBOL: return pvt_get_symbol(b, rb_buffer, argc, argv);
69
77
  case BSON_TYPE_ARRAY: return rb_bson_byte_buffer_get_array(argc, argv, rb_buffer);
70
78
  case BSON_TYPE_DOCUMENT: return rb_bson_byte_buffer_get_hash(argc, argv, rb_buffer);
@@ -119,9 +127,20 @@ VALUE rb_bson_byte_buffer_get_bytes(VALUE self, VALUE i)
119
127
  }
120
128
 
121
129
  VALUE pvt_get_boolean(byte_buffer_t *b){
122
- VALUE result = Qnil;
130
+ VALUE result;
131
+ char byte_value;
123
132
  ENSURE_BSON_READ(b, 1);
124
- result = *READ_PTR(b) == 1 ? Qtrue: Qfalse;
133
+ byte_value = *READ_PTR(b);
134
+ switch (byte_value) {
135
+ case 1:
136
+ result = Qtrue;
137
+ break;
138
+ case 0:
139
+ result = Qfalse;
140
+ break;
141
+ default:
142
+ pvt_raise_decode_error(rb_sprintf("Invalid boolean byte value: %d", (int) byte_value));
143
+ }
125
144
  b->read_position += 1;
126
145
  return result;
127
146
  }
@@ -134,22 +153,35 @@ VALUE rb_bson_byte_buffer_get_string(VALUE self)
134
153
  byte_buffer_t *b;
135
154
 
136
155
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
137
- return pvt_get_string(b);
156
+ return pvt_get_string(b, "String");
138
157
  }
139
158
 
140
- VALUE pvt_get_string(byte_buffer_t *b)
159
+ VALUE pvt_get_string(byte_buffer_t *b, const char *data_type)
141
160
  {
142
- int32_t length;
143
161
  int32_t length_le;
162
+ int32_t length;
163
+ char *str_ptr;
144
164
  VALUE string;
165
+ unsigned char last_byte;
145
166
 
146
167
  ENSURE_BSON_READ(b, 4);
147
- memcpy(&length, READ_PTR(b), 4);
148
- length_le = BSON_UINT32_FROM_LE(length);
149
- b->read_position += 4;
150
- ENSURE_BSON_READ(b, length_le);
151
- string = rb_enc_str_new(READ_PTR(b), length_le - 1, rb_utf8_encoding());
152
- b->read_position += length_le;
168
+ memcpy(&length_le, READ_PTR(b), 4);
169
+ length = BSON_UINT32_FROM_LE(length_le);
170
+ if (length < 0) {
171
+ pvt_raise_decode_error(rb_sprintf("String length is negative: %d", length));
172
+ }
173
+ if (length == 0) {
174
+ pvt_raise_decode_error(rb_str_new_cstr("String length is zero but string must be null-terminated"));
175
+ }
176
+ ENSURE_BSON_READ(b, 4 + length);
177
+ str_ptr = READ_PTR(b) + 4;
178
+ last_byte = *(READ_PTR(b) + 4 + length_le - 1);
179
+ if (last_byte != 0) {
180
+ pvt_raise_decode_error(rb_sprintf("Last byte of the string is not null: 0x%x", (int) last_byte));
181
+ }
182
+ rb_bson_utf8_validate(str_ptr, length - 1, true, data_type);
183
+ string = rb_enc_str_new(str_ptr, length - 1, rb_utf8_encoding());
184
+ b->read_position += 4 + length_le;
153
185
  return string;
154
186
  }
155
187
 
@@ -166,7 +198,7 @@ VALUE pvt_get_symbol(byte_buffer_t *b, VALUE rb_buffer, int argc, VALUE *argv)
166
198
  VALUE value, klass;
167
199
 
168
200
  if (pvt_get_mode_option(argc, argv) == BSON_MODE_BSON) {
169
- value = pvt_get_string(b);
201
+ value = pvt_get_string(b, "Symbol");
170
202
  klass = pvt_const_get_3("BSON", "Symbol", "Raw");
171
203
  value = rb_funcall(klass, rb_intern("new"), 1, value);
172
204
  } else {
@@ -306,10 +338,13 @@ VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self){
306
338
  byte_buffer_t *b = NULL;
307
339
  uint8_t type;
308
340
  VALUE cDocument = pvt_const_get_2("BSON", "Document");
341
+ int32_t length;
342
+ char *start_ptr;
309
343
 
310
344
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
311
345
 
312
- pvt_validate_length(b);
346
+ start_ptr = READ_PTR(b);
347
+ length = pvt_validate_length(b);
313
348
 
314
349
  doc = rb_funcall(cDocument, rb_intern("allocate"), 0);
315
350
 
@@ -318,6 +353,11 @@ VALUE rb_bson_byte_buffer_get_hash(int argc, VALUE *argv, VALUE self){
318
353
  rb_hash_aset(doc, field, pvt_read_field(b, self, type, argc, argv));
319
354
  RB_GC_GUARD(field);
320
355
  }
356
+
357
+ if (READ_PTR(b) - start_ptr != length) {
358
+ pvt_raise_decode_error(rb_sprintf("Expected to read %d bytes for the hash but read %ld bytes", length, READ_PTR(b) - start_ptr));
359
+ }
360
+
321
361
  return doc;
322
362
  }
323
363
 
@@ -325,10 +365,13 @@ VALUE rb_bson_byte_buffer_get_array(int argc, VALUE *argv, VALUE self){
325
365
  byte_buffer_t *b;
326
366
  VALUE array = Qnil;
327
367
  uint8_t type;
368
+ int32_t length;
369
+ char *start_ptr;
328
370
 
329
371
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
330
372
 
331
- pvt_validate_length(b);
373
+ start_ptr = READ_PTR(b);
374
+ length = pvt_validate_length(b);
332
375
 
333
376
  array = rb_ary_new();
334
377
  while((type = pvt_get_type_byte(b)) != 0){
@@ -336,5 +379,10 @@ VALUE rb_bson_byte_buffer_get_array(int argc, VALUE *argv, VALUE self){
336
379
  rb_ary_push(array, pvt_read_field(b, self, type, argc, argv));
337
380
  }
338
381
  RB_GC_GUARD(array);
382
+
383
+ if (READ_PTR(b) - start_ptr != length) {
384
+ pvt_raise_decode_error(rb_sprintf("Expected to read %d bytes for the hash but read %ld bytes", length, READ_PTR(b) - start_ptr));
385
+ }
386
+
339
387
  return array;
340
388
  }