From 16cac29f574e73d86eb54a8526a5129b8f18516c Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 12 Dec 2016 18:27:09 -0500 Subject: [PATCH] Small clarification to aid in debugging --- protobuf.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/protobuf.c b/protobuf.c index 9fc2f23..d42d370 100644 --- a/protobuf.c +++ b/protobuf.c @@ -13,7 +13,7 @@ int protobuf_encode_length_delimited(int field_number, enum WireType field_type, // push the field number and wire type together unsigned int field_no = field_number << 3; unsigned long long field = field_no | field_type; - size_t bytes_processed; + size_t bytes_processed = 0; *bytes_written = 0; // field type & number varint_encode(field, buffer, max_buffer_length, &bytes_processed); @@ -31,18 +31,23 @@ int protobuf_decode_length_delimited(const unsigned char* buffer, size_t buffer_ size_t pos = 0; *bytes_read = 0; // grab the field size - *results_length = varint_decode(&buffer[pos], buffer_length - pos, bytes_read); + size_t field_size = varint_decode(&buffer[pos], buffer_length - pos, bytes_read); pos += *bytes_read; // allocate memory - *results = malloc(sizeof(char) * (*results_length)); + *results = malloc(sizeof(char) * field_size); if ((*results) == NULL) return 0; + memset(*results, 0, field_size); + // copy the bytes - memcpy((*results), &buffer[pos], (*results_length)); - pos += (*results_length); + memcpy( *results, &buffer[pos], field_size); + pos += field_size; + + // set return values *bytes_read = pos; + *results_length = field_size; return 1; } @@ -110,6 +115,8 @@ int protobuf_decode_string(const unsigned char* buffer, size_t buffer_length, ch if ((*results) == NULL) return 0; + memset(*results, 0, length+1); + // copy the string memcpy((*results), &buffer[pos], length); // don't forget the null