bson 4.8.0 → 4.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) 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 +1 -1
  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 +4 -3
  30. data/lib/bson/false_class.rb +1 -1
  31. data/lib/bson/float.rb +1 -1
  32. data/lib/bson/hash.rb +14 -3
  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 +40 -6
  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 +205 -2
  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 +1 -1
  93. data/spec/spec_tests/data/corpus/top.json +0 -4
  94. data/spec/support/shared_examples.rb +1 -1
  95. metadata +86 -87
  96. metadata.gz.sig +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c37081fa823c2700f07a23ca6ecbec6fc67cffbf7915b4a21e59940b332b678d
4
- data.tar.gz: 16d483a311c4252f4ef1463be2f86240450b82294a972d8bb5bb2bb332fd7b17
3
+ metadata.gz: d50166247ab279ada5bb92adacd6f1e02f2723688f1894a67f1a64d4c45c6005
4
+ data.tar.gz: 65d6a8870198a2d9c2d09b497b6e039100ff681e5f2c2e872574128cbc1df3b1
5
5
  SHA512:
6
- metadata.gz: 1618b74348d155c904b2f24739d7b9403bc0b1c23e67cc6ce89a92ac19070dd5965dc1fccc6353c2eefb82993bf5fec30702bccaaf6bb8171777421253832cce
7
- data.tar.gz: 1362710c9b36348437bf266a52c18347c1228ddd92231b520e6b068b1742f42895c2108a64cdff2487c5b874c12e3af5ce4d4435c00cebad942b83f1ea9e17ab
6
+ metadata.gz: caed5dcdc24d0fb9e90c1d0708ab9d65cfd279d1d22ada39799aa2475ec748b459614739aeb411a70b12e2000a1ee07b2c1c280f328d36ffc10a38076aa6d4fc
7
+ data.tar.gz: 4971b5eefb856aa14f4ee6fc1976b38a2bb571be7faf8c330637c34ee1f5022d185a836bd15f9852fd9b12ecb10e21e8f5b98560e8a18f89fe2bcc5a2b0193bd
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.
@@ -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
  }
@@ -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,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2018-2019 MongoDB Inc.
1
+ # Copyright (C) 2018-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -119,10 +119,21 @@ module BSON
119
119
  buffer.get_array(**options)
120
120
  else
121
121
  array = new
122
- buffer.get_int32 # throw away the length
122
+ start_position = buffer.read_position
123
+ expected_byte_size = buffer.get_int32
123
124
  while (type = buffer.get_byte) != NULL_BYTE
124
125
  buffer.get_cstring
125
- array << BSON::Registry.get(type).from_bson(buffer, **options)
126
+ cls = BSON::Registry.get(type)
127
+ value = if options.empty?
128
+ cls.from_bson(buffer)
129
+ else
130
+ cls.from_bson(buffer, **options)
131
+ end
132
+ array << value
133
+ end
134
+ actual_byte_size = buffer.read_position - start_position
135
+ if actual_byte_size != expected_byte_size
136
+ raise Error::BSONDecodeError, "Expected array to take #{expected_byte_size} bytes but it took #{actual_byte_size} bytes"
126
137
  end
127
138
  array
128
139
  end
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -39,7 +39,14 @@ module BSON
39
39
  #
40
40
  # @since 2.0.0
41
41
  def self.from_bson(buffer, **options)
42
- buffer.get_byte == TrueClass::TRUE_BYTE
42
+ case v = buffer.get_byte
43
+ when TrueClass::TRUE_BYTE
44
+ true
45
+ when FalseClass::FALSE_BYTE
46
+ false
47
+ else
48
+ raise Error::BSONDecodeError, "Invalid boolean byte value: #{v}"
49
+ end
43
50
  end
44
51
 
45
52
  # Register this type when the module is loaded.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -119,8 +119,21 @@ module BSON
119
119
  #
120
120
  # @since 2.0.0
121
121
  def self.from_bson(buffer, **options)
122
- buffer.get_int32 # Throw away the total length.
123
- new(buffer.get_string, ::Hash.from_bson(buffer, **options))
122
+ # Code with scope has a length (?) field which is not needed for
123
+ # decoding, but spec tests want this field validated.
124
+ start_position = buffer.read_position
125
+ length = buffer.get_int32
126
+ javascript = buffer.get_string
127
+ scope = if options.empty?
128
+ ::Hash.from_bson(buffer)
129
+ else
130
+ ::Hash.from_bson(buffer, **options)
131
+ end
132
+ read_bytes = buffer.read_position - start_position
133
+ if read_bytes != length
134
+ raise Error::BSONDecodeError, "CodeWithScope invalid: claimed length #{length}, actual length #{read_bytes}"
135
+ end
136
+ new(javascript, scope)
124
137
  end
125
138
 
126
139
  # Register this type when the module is loaded.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2016-2019 MongoDB Inc.
1
+ # Copyright (C) 2016-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2009-2019 MongoDB Inc.
1
+ # Copyright (C) 2009-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -95,7 +95,13 @@ module BSON
95
95
  #
96
96
  # @see http://bsonspec.org/#/specification
97
97
  def self.from_bson(buffer, **options)
98
- new(buffer.get_string, ObjectId.from_bson(buffer, **options))
98
+ ref = buffer.get_string
99
+ id = if options.empty?
100
+ ObjectId.from_bson(buffer)
101
+ else
102
+ ObjectId.from_bson(buffer, **options)
103
+ end
104
+ new(ref, id)
99
105
  end
100
106
 
101
107
  # Register this type when the module is loaded.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2016-2019 MongoDB Inc.
1
+ # Copyright (C) 2016-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2016-2019 MongoDB Inc.
1
+ # Copyright (C) 2016-2020 MongoDB Inc.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.