Don't use buf_size as input and output in encode

pull/4750/head
Alistair Veitch 9 years ago
parent c45d088ae7
commit ff14b44154
  1. 17
      include/grpc/census.h
  2. 14
      src/core/census/tag_set.c
  3. 19
      test/core/census/tag_set_test.c

@ -438,19 +438,20 @@ int census_tag_set_get_tag_by_key(const census_tag_set *tags, const char *key,
@param tags tag set to be encoded @param tags tag set to be encoded
@param buffer pointer to buffer. This address will be used to encode the @param buffer pointer to buffer. This address will be used to encode the
printable tags. printable tags.
@param buf_size On input, will be a pointer to total buffer size. On output, @param buf_size number of available bytes in buffer.
will be set to total number of bytes consumed by printable @param print_buf_size Will be set to the number of bytes consumed by
tags. printable tags.
@param bin_buf_size on output, will be set to the number of bytes used to @param bin_buf_size Will be set to the number of bytes used to encode the
encode the binary tags. binary tags.
@return A pointer to the binary tag's encoded, or NULL if the buffer was @return A pointer to the binary tag's encoded, or NULL if the buffer was
insufficiently large to hold the encoded tags. Thus, if successful, insufficiently large to hold the encoded tags. Thus, if successful,
printable tags are encoded into printable tags are encoded into
[buffer, buffer + *buf_size) and binary tags into [buffer, buffer + *print_buf_size) and binary tags into
[returned-ptr, returned-ptr + *bin_buf_size) (and the return value [returned-ptr, returned-ptr + *bin_buf_size) (and the return value
should be buffer + *buf_size) */ should be buffer + *print_buf_size) */
char *census_tag_set_encode(const census_tag_set *tags, char *buffer, char *census_tag_set_encode(const census_tag_set *tags, char *buffer,
size_t *buf_size, size_t *bin_buf_size); size_t buf_size, size_t *print_buf_size,
size_t *bin_buf_size);
/* Decode tag set buffers encoded with census_tag_set_encode_*(). Returns NULL /* Decode tag set buffers encoded with census_tag_set_encode_*(). Returns NULL
if there is an error in parsing either buffer. */ if there is an error in parsing either buffer. */

@ -454,19 +454,19 @@ static size_t tag_set_encode(const struct tag_set *tags, char *buffer,
} }
char *census_tag_set_encode(const census_tag_set *tags, char *buffer, char *census_tag_set_encode(const census_tag_set *tags, char *buffer,
size_t *buf_size, size_t *bin_buf_size) { size_t buf_size, size_t *print_buf_size,
size_t p_buf_size = size_t *bin_buf_size) {
tag_set_encode(&tags->tags[PROPAGATED_TAGS], buffer, *buf_size); *print_buf_size =
if (p_buf_size == 0) { tag_set_encode(&tags->tags[PROPAGATED_TAGS], buffer, buf_size);
if (*print_buf_size == 0) {
return NULL; return NULL;
} }
char *b_buffer = buffer + p_buf_size; char *b_buffer = buffer + *print_buf_size;
*bin_buf_size = tag_set_encode(&tags->tags[PROPAGATED_BINARY_TAGS], b_buffer, *bin_buf_size = tag_set_encode(&tags->tags[PROPAGATED_BINARY_TAGS], b_buffer,
*buf_size - p_buf_size); buf_size - *print_buf_size);
if (*bin_buf_size == 0) { if (*bin_buf_size == 0) {
return NULL; return NULL;
} }
*buf_size = p_buf_size;
return b_buffer; return b_buffer;
} }

@ -325,15 +325,18 @@ static void encode_decode_test(void) {
char buffer[BUF_SIZE]; char buffer[BUF_SIZE];
struct census_tag_set *cts = struct census_tag_set *cts =
census_tag_set_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL); census_tag_set_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
size_t bsize = 2; // buffer size too small size_t print_bsize;
size_t bin_bsize = 0; size_t bin_bsize;
GPR_ASSERT(census_tag_set_encode(cts, buffer, &bsize, &bin_bsize) == NULL); // Test with too small a buffer
bsize = BUF_SIZE; GPR_ASSERT(census_tag_set_encode(cts, buffer, 2, &print_bsize, &bin_bsize) ==
char *b_buffer = census_tag_set_encode(cts, buffer, &bsize, &bin_bsize); NULL);
GPR_ASSERT(b_buffer != NULL && bsize > 0 && bin_bsize > 0 && char *b_buffer =
bsize + bin_bsize <= BUF_SIZE && b_buffer == buffer + bsize); census_tag_set_encode(cts, buffer, BUF_SIZE, &print_bsize, &bin_bsize);
GPR_ASSERT(b_buffer != NULL && print_bsize > 0 && bin_bsize > 0 &&
print_bsize + bin_bsize <= BUF_SIZE &&
b_buffer == buffer + print_bsize);
census_tag_set *cts2 = census_tag_set *cts2 =
census_tag_set_decode(buffer, bsize, b_buffer, bin_bsize); census_tag_set_decode(buffer, print_bsize, b_buffer, bin_bsize);
GPR_ASSERT(cts2 != NULL); GPR_ASSERT(cts2 != NULL);
const census_tag_set_create_status *status = const census_tag_set_create_status *status =
census_tag_set_get_create_status(cts2); census_tag_set_get_create_status(cts2);

Loading…
Cancel
Save