Skip to content

Commit

Permalink
Refer to a shared definition for max buffered bytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
haberman committed Aug 19, 2015
1 parent 146a9c2 commit 1e87095
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
11 changes: 5 additions & 6 deletions tests/pb/test_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,14 @@ uint32_t Hash(const string& proto, const string* expected_output, size_t seam1,
}

void CheckBytesParsed(const upb::pb::Decoder& decoder, size_t ofs) {
// We could have parsed as many as 10 bytes fewer than what the decoder
// previously accepted, since we can buffer up to 12 partial bytes internally
// before accumulating an entire value.
const int MAX_BUFFERED = 14;

// We can't have parsed more data than the decoder callback is telling us it
// parsed.
ASSERT(decoder.BytesParsed() <= ofs);
ASSERT(ofs <= (decoder.BytesParsed() + MAX_BUFFERED));

// The difference between what we've decoded and what the decoder has accepted
// represents the internally buffered amount. This amount should not exceed
// this value which comes from decoder.int.h.
ASSERT(ofs <= (decoder.BytesParsed() + UPB_DECODER_MAX_RESIDUAL_BYTES));
}

static bool parse(VerboseParserEnvironment* env,
Expand Down
7 changes: 7 additions & 0 deletions upb/pb/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts)
UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted,
upb_pbdecodermethod, upb_refcounted)

/* The maximum number of bytes we are required to buffer internally between
* calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte
* varint, less one because we are buffering an incomplete value.
*
* Should only be used by unit tests. */
#define UPB_DECODER_MAX_RESIDUAL_BYTES 14

#ifdef __cplusplus

/* The parameters one uses to construct a DecoderMethod.
Expand Down
7 changes: 2 additions & 5 deletions upb/pb/decoder.int.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,8 @@ struct upb_pbdecoder {
/* Overall stream offset of "buf." */
uint64_t bufstart_ofs;

/* Buffer for residual bytes not parsed from the previous buffer.
* The maximum number of residual bytes we require is 12; a five-byte
* unknown tag plus a ten-byte value, less one because the value
* is only a partial value. */
char residual[14];
/* Buffer for residual bytes not parsed from the previous buffer. */
char residual[UPB_DECODER_MAX_RESIDUAL_BYTES];
char *residual_end;

/* Bytes of data that should be discarded from the input beore we start
Expand Down

0 comments on commit 1e87095

Please sign in to comment.