Skip to content

Commit df244fb

Browse files
committed
Fix key off by one error
1 parent 45a2353 commit df244fb

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

.yardopts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ pages/Rails.md
1313
pages/Security.md
1414
CHANGELOG.md
1515
LICENSE
16+
RELEASE_NOTES.md

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Fixed unicode UTF 8 parsing in string values.
66

7+
- Fixed hash key allocation issue.
8+
79
## 3.13.5 - 2021-09-08
810

911
- Assure value strings of zero length are not always cached.

ext/oj/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ static VALUE parser_new(VALUE self, VALUE mode) {
12111211
* - *:usual*
12121212
* - _cache_keys=_ sets the value of the _cache_keys_ flag.
12131213
* - _cache_keys_ returns the value of the _cache_keys_ flag.
1214-
* - _cache_strings=_ sets the value of the _cache_strings_ to an positive integer less than 35. Strings shorter than
1214+
* - _cache_strings=_ sets the value of the _cache_strings_ to a positive integer less than 35. Strings shorter than
12151215
* that length are cached.
12161216
* - _cache_strings_ returns the value of the _cache_strings_ integer value.
12171217
* - _cache_expunge=_ sets the value of the _cache_expunge_ where 0 never expunges, 1 expunges slowly, 2 expunges

ext/oj/usual.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef struct _col {
3939
typedef union _key {
4040
struct {
4141
int16_t len;
42-
char buf[22];
42+
char buf[30];
4343
};
4444
struct {
4545
int16_t xlen; // should be the same as len
@@ -209,21 +209,21 @@ static void push(ojParser p, VALUE v) {
209209
static VALUE cache_key(ojParser p, Key kp) {
210210
Delegate d = (Delegate)p->ctx;
211211

212-
if ((size_t)kp->len < sizeof(kp->buf) - 1) {
212+
if ((size_t)kp->len < sizeof(kp->buf)) {
213213
return cache_intern(d->key_cache, kp->buf, kp->len);
214214
}
215215
return cache_intern(d->key_cache, kp->key, kp->len);
216216
}
217217

218218
static VALUE str_key(ojParser p, Key kp) {
219-
if ((size_t)kp->len < sizeof(kp->buf) - 1) {
219+
if ((size_t)kp->len < sizeof(kp->buf)) {
220220
return rb_str_freeze(rb_utf8_str_new(kp->buf, kp->len));
221221
}
222222
return rb_str_freeze(rb_utf8_str_new(kp->key, kp->len));
223223
}
224224

225225
static VALUE sym_key(ojParser p, Key kp) {
226-
if ((size_t)kp->len < sizeof(kp->buf) - 1) {
226+
if ((size_t)kp->len < sizeof(kp->buf)) {
227227
return rb_str_freeze(rb_str_intern(rb_utf8_str_new(kp->buf, kp->len)));
228228
}
229229
return rb_str_freeze(rb_str_intern(rb_utf8_str_new(kp->key, kp->len)));
@@ -232,7 +232,7 @@ static VALUE sym_key(ojParser p, Key kp) {
232232
static ID get_attr_id(ojParser p, Key kp) {
233233
Delegate d = (Delegate)p->ctx;
234234

235-
if ((size_t)kp->len < sizeof(kp->buf) - 1) {
235+
if ((size_t)kp->len < sizeof(kp->buf)) {
236236
return (ID)cache_intern(d->attr_cache, kp->buf, kp->len);
237237
}
238238
return (ID)cache_intern(d->attr_cache, kp->key, kp->len);
@@ -253,7 +253,7 @@ static void push_key(ojParser p) {
253253
d->kend = d->khead + cap;
254254
}
255255
d->ktail->len = klen;
256-
if (klen <= sizeof(d->ktail->buf) + 1) {
256+
if (klen < sizeof(d->ktail->buf)) {
257257
memcpy(d->ktail->buf, key, klen);
258258
d->ktail->buf[klen] = '\0';
259259
} else {
@@ -336,15 +336,15 @@ static void close_object(ojParser p) {
336336
#if HAVE_RB_HASH_BULK_INSERT
337337
for (vp = head; kp < d->ktail; kp++, vp += 2) {
338338
*vp = d->get_key(p, kp);
339-
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
339+
if (sizeof(kp->buf) <= (size_t)kp->len) {
340340
xfree(kp->key);
341341
}
342342
}
343343
rb_hash_bulk_insert(d->vtail - head, head, obj);
344344
#else
345345
for (vp = head; kp < d->ktail; kp++, vp += 2) {
346346
rb_hash_aset(obj, d->get_key(p, kp), *(vp + 1));
347-
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
347+
if (sizeof(kp->buf) <= (size_t)kp->len) {
348348
xfree(kp->key);
349349
}
350350
}
@@ -368,7 +368,7 @@ static void close_object_class(ojParser p) {
368368

369369
for (vp = head; kp < d->ktail; kp++, vp += 2) {
370370
rb_funcall(obj, hset_id, 2, d->get_key(p, kp), *(vp + 1));
371-
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
371+
if (sizeof(kp->buf) <= (size_t)kp->len) {
372372
xfree(kp->key);
373373
}
374374
}
@@ -396,15 +396,15 @@ static void close_object_create(ojParser p) {
396396
#if HAVE_RB_HASH_BULK_INSERT
397397
for (vp = head; kp < d->ktail; kp++, vp += 2) {
398398
*vp = d->get_key(p, kp);
399-
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
399+
if (sizeof(kp->buf) <= (size_t)kp->len) {
400400
xfree(kp->key);
401401
}
402402
}
403403
rb_hash_bulk_insert(d->vtail - head, head, obj);
404404
#else
405405
for (vp = head; kp < d->ktail; kp++, vp += 2) {
406406
rb_hash_aset(obj, d->get_key(p, kp), *(vp + 1));
407-
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
407+
if (sizeof(kp->buf) <= (size_t)kp->len) {
408408
xfree(kp->key);
409409
}
410410
}
@@ -413,7 +413,7 @@ static void close_object_create(ojParser p) {
413413
obj = rb_class_new_instance(0, NULL, d->hash_class);
414414
for (vp = head; kp < d->ktail; kp++, vp += 2) {
415415
rb_funcall(obj, hset_id, 2, d->get_key(p, kp), *(vp + 1));
416-
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
416+
if (sizeof(kp->buf) <= (size_t)kp->len) {
417417
xfree(kp->key);
418418
}
419419
}
@@ -428,15 +428,15 @@ static void close_object_create(ojParser p) {
428428
#if HAVE_RB_HASH_BULK_INSERT
429429
for (vp = head; kp < d->ktail; kp++, vp += 2) {
430430
*vp = d->get_key(p, kp);
431-
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
431+
if (sizeof(kp->buf) <= (size_t)kp->len) {
432432
xfree(kp->key);
433433
}
434434
}
435435
rb_hash_bulk_insert(d->vtail - head, head, arg);
436436
#else
437437
for (vp = head; kp < d->ktail; kp++, vp += 2) {
438438
rb_hash_aset(arg, d->get_key(p, kp), *(vp + 1));
439-
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
439+
if (sizeof(kp->buf) <= (size_t)kp->len) {
440440
xfree(kp->key);
441441
}
442442
}
@@ -446,7 +446,7 @@ static void close_object_create(ojParser p) {
446446
obj = rb_class_new_instance(0, NULL, clas);
447447
for (vp = head; kp < d->ktail; kp++, vp += 2) {
448448
rb_ivar_set(obj, get_attr_id(p, kp), *(vp + 1));
449-
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
449+
if (sizeof(kp->buf) <= (size_t)kp->len) {
450450
xfree(kp->key);
451451
}
452452
}

oj.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
2626
s.test_files = Dir["test/**/*.rb"]
2727
s.extensions = ["ext/oj/extconf.rb"]
2828

29-
s.extra_rdoc_files = ['README.md'] + Dir["pages/*.md"]
29+
s.extra_rdoc_files = ['README.md', 'LICENSE', 'CHANGELOG.md', 'RELEASE_NOTES.md'] + Dir["pages/*.md"]
3030
s.rdoc_options = ['--title', 'Oj', '--main', 'README.md']
3131

3232
s.add_development_dependency 'rake-compiler', '>= 0.9', '< 2.0'

0 commit comments

Comments
 (0)