Small clarification to aid in debugging

This commit is contained in:
John Jones 2016-12-12 18:27:09 -05:00
parent ff50be52e6
commit 16cac29f57

View file

@ -13,7 +13,7 @@ int protobuf_encode_length_delimited(int field_number, enum WireType field_type,
// push the field number and wire type together // push the field number and wire type together
unsigned int field_no = field_number << 3; unsigned int field_no = field_number << 3;
unsigned long long field = field_no | field_type; unsigned long long field = field_no | field_type;
size_t bytes_processed; size_t bytes_processed = 0;
*bytes_written = 0; *bytes_written = 0;
// field type & number // field type & number
varint_encode(field, buffer, max_buffer_length, &bytes_processed); 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; size_t pos = 0;
*bytes_read = 0; *bytes_read = 0;
// grab the field size // 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; pos += *bytes_read;
// allocate memory // allocate memory
*results = malloc(sizeof(char) * (*results_length)); *results = malloc(sizeof(char) * field_size);
if ((*results) == NULL) if ((*results) == NULL)
return 0; return 0;
memset(*results, 0, field_size);
// copy the bytes // copy the bytes
memcpy((*results), &buffer[pos], (*results_length)); memcpy( *results, &buffer[pos], field_size);
pos += (*results_length); pos += field_size;
// set return values
*bytes_read = pos; *bytes_read = pos;
*results_length = field_size;
return 1; return 1;
} }
@ -110,6 +115,8 @@ int protobuf_decode_string(const unsigned char* buffer, size_t buffer_length, ch
if ((*results) == NULL) if ((*results) == NULL)
return 0; return 0;
memset(*results, 0, length+1);
// copy the string // copy the string
memcpy((*results), &buffer[pos], length); memcpy((*results), &buffer[pos], length);
// don't forget the null // don't forget the null