aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2019-08-22 11:31:24 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2019-08-27 14:47:20 +0300
commite29e6f4ebadef5e1ed9be1799e2dfbb12a26af67 (patch)
tree02e218489ad326c6fec5b877a35b4d0b3102c4bf
parent0101f7cdaa7be255875de06b9313c1a6e03fcb4d (diff)
downloadnanopb-c-e29e6f4ebadef5e1ed9be1799e2dfbb12a26af67.tar.gz
Don't call stream callback with count=0 (#421)
This could happen with zero-length submessages and strings. While technically valid, it is an useless call and a rare corner case that can easily lead to bugs in the callback implementation.
-rw-r--r--pb_decode.c3
-rw-r--r--pb_encode.c2
2 files changed, 4 insertions, 1 deletions
diff --git a/pb_decode.c b/pb_decode.c
index 2c652f1..a7caeff 100644
--- a/pb_decode.c
+++ b/pb_decode.c
@@ -99,6 +99,9 @@ static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t co
bool checkreturn pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
{
+ if (count == 0)
+ return true;
+
#ifndef PB_BUFFER_ONLY
if (buf == NULL && stream->callback != buf_read)
{
diff --git a/pb_encode.c b/pb_encode.c
index 221c0ea..64b62b4 100644
--- a/pb_encode.c
+++ b/pb_encode.c
@@ -100,7 +100,7 @@ pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
{
- if (stream->callback != NULL)
+ if (count > 0 && stream->callback != NULL)
{
if (stream->bytes_written + count > stream->max_size)
PB_RETURN_ERROR(stream, "stream full");