Skip to content

Commit 9962216

Browse files
authored
Merge pull request #576 from bytecodesky/fix-integer-overflow-ints_utils.cpp
Fix: prevent integer overflow in events_ints_print bounds check
2 parents ca56fd7 + 3c6b1c2 commit 9962216

3 files changed

Lines changed: 13 additions & 7 deletions

File tree

changelog/current.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
---
5757
more data here
5858
```
59+
- [PR#576](https://github.com/biojppm/rapidyaml/pull/576) - `extra::events_ints_print()`: Prevent integer overflow in bounds check (thanks @bytecodesky).
5960

6061

6162
### JSON emitting changes
@@ -98,3 +99,8 @@
9899
- [PR#565](https://github.com/biojppm/rapidyaml/pull/565) (fixes [#564](https://github.com/biojppm/rapidyaml/issues/564)) - `Tree` arena: allow relocation of zero-length strings when placed at the end (relax assertions triggered in `Tree::_relocated()`)
99100
- [PR#563](https://github.com/biojppm/rapidyaml/pull/563) (fixes [#562](https://github.com/biojppm/rapidyaml/issues/562)) - Fix bug in `NodeRef::cend()`
100101
- [PR#568](https://github.com/biojppm/rapidyaml/pull/568) - Move `escape_scalar()` from `c4/yml/extra/scalar.hpp` to `c4/yml/escape_scalar.hpp` (and removed the original header)
102+
103+
104+
### Thanks
105+
106+
- @bytecodesky

src_extra/c4/yml/extra/event_handler_ints.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ struct EventHandlerIntsState : public c4::yml::ParserState
204204
* BDOC, // begin doc
205205
* VAL_|BSEQ|FLOW, // begin seq as val, flow
206206
* VAL_|SCLR|PLAI, 1, 1, // val scalar, plain style: "a" starts at offset 1 and has length 1
207-
* VAL_|SCLR|PLAI|PSTR, 4, 2, // val scalar, plain style: "bb" starts at offset 4 and has length 2
208-
* VAL_|SCLR|PLAI|PSTR, 8, 3, // val scalar, plain style: "ccc" starts at offset 8 and has length 3
209-
* ESEQ|PSTR, // end seq
207+
* VAL_|SCLR|PLAI|PSTR, 4, 2, // val scalar, plain style: "bb" starts at offset 4 and has length 2; preceded by a string event (PSTR)
208+
* VAL_|SCLR|PLAI|PSTR, 8, 3, // val scalar, plain style: "ccc" starts at offset 8 and has length 3; preceded by a string event (PSTR)
209+
* ESEQ|PSTR, // end seq; preceded by a string event (PSTR)
210210
* EDOC, // end doc
211211
* ESTR, // end stream
212212
* };
@@ -239,7 +239,7 @@ i : 6 | 7 8 9 | 10 1
239239
| |
240240
prev event has string prev event has string
241241
(to get to prev, jump (to get to prev, jump
242-
back 3: ie 6->3) back 3: ie 9->6)
242+
back 3 slots: ie 6->3) back 3 slots: ie 9->6)
243243
244244
245245
@@ -251,7 +251,7 @@ i : 12 | 13 14
251251
|
252252
prev event has string
253253
(to get to it, jump
254-
back 3: ie 12->9)
254+
back 3 slots: ie 12->9)
255255
@endcode
256256
*
257257
* Note that the buffer contains both events and strings encoded as
@@ -405,7 +405,7 @@ i : 12 | 13 14
405405
*
406406
* ```c++
407407
* const std::string src = ...; // the YAML code to be parsed
408-
* std::string parsed_src = src; // this is where we will parse (filter during parsring)
408+
* std::string parsed_src = src; // this is where we will parse (filter during parsing)
409409
* std::vector<int> evts((size_t)estimated_size); // ensure we have a fighting change to acommodate the events
410410
* std::vector<char> arena(src.size()); // ensure we have a fighting change to acommodate the events
411411
* ParseEngine<extra::EventHandlerInts> parser(&handler);

src_extra/c4/yml/extra/ints_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void events_ints_print(csubstr parsed_yaml, csubstr arena, ievt::DataType const*
109109
bool safe = (evts[evtpos + 1] >= 0)
110110
&& (evts[evtpos + 2] >= 0)
111111
&& (evts[evtpos + 1] <= (int)region.len)
112-
&& ((evts[evtpos + 1] + evts[evtpos + 2]) <= (int)region.len);
112+
&& (evts[evtpos + 2] <= ((int)region.len - evts[evtpos + 1]));
113113
const char *str = safe ? (region.str + evts[evtpos + 1]) : "ERR!!!";
114114
int len = safe ? evts[evtpos + 2] : 6;
115115
printf(": %d [%d]~~~%.*s~~~", evts[evtpos+1], evts[evtpos+2], len, str);

0 commit comments

Comments
 (0)