Handles moving the data index forward, validating the arguments, and avoiding overflow or underflow.
| 86 | // Handles moving the data index forward, validating the arguments, and avoiding |
| 87 | // overflow or underflow. |
| 88 | Status IncrementOffset(int old_offset, size_t increment, size_t max_size, |
| 89 | int* new_offset) { |
| 90 | if (old_offset < 0) { |
| 91 | return errors::InvalidArgument("Negative offsets are not allowed: ", |
| 92 | old_offset); |
| 93 | } |
| 94 | if (old_offset > max_size) { |
| 95 | return errors::InvalidArgument("Initial offset is outside data range: ", |
| 96 | old_offset); |
| 97 | } |
| 98 | *new_offset = old_offset + increment; |
| 99 | if (*new_offset > max_size) { |
| 100 | return errors::InvalidArgument("Data too short when trying to read string"); |
| 101 | } |
| 102 | // See above for the check that the input offset is positive. If it's negative |
| 103 | // here then it means that there's been an overflow in the arithmetic. |
| 104 | if (*new_offset < 0) { |
| 105 | return errors::InvalidArgument("Offset too large, overflowed: ", |
| 106 | *new_offset); |
| 107 | } |
| 108 | return Status::OK(); |
| 109 | } |
| 110 | |
| 111 | Status ExpectText(const string& data, const string& expected_text, |
| 112 | int* offset) { |