- int unstuffed_bytes = (*unstuff_choice)(buf, src->bytes, bytes_to_unstuff);
- assert(unstuffed_bytes != 0);
- if (unstuffed_bytes > 0) {
- // Fast path: No markers in the data. We can basically just
- // return it.
- if (end_marker) {
- src->bytes_available = 1;
- src->bytes[0] = 0xff;
- } else {
- src->bytes_available = 0;
- src->bytes[0] = 0;
+ // Fast path: No markers in the data, we don't have more data than we
+ // need. We can basically just return it.
+ if (bytes_to_unstuff <= len) {
+ int unstuffed_bytes = (*unstuff_choice)(buf, src->bytes, bytes_to_unstuff);
+ assert(unstuffed_bytes != 0);
+ if (unstuffed_bytes > 0) {
+ // Fast path: No markers in the data. We can basically just
+ // return it.
+ if (end_marker) {
+ src->bytes_available = 1;
+ src->bytes[0] = 0xff;
+ } else {
+ src->bytes_available = 0;
+ }
+ return unstuffed_bytes;