nanopb-arduino icon indicating copy to clipboard operation
nanopb-arduino copied to clipboard

Set bytes_left in pb_stream_read

Open floriaanpost opened this issue 1 year ago • 0 comments

Thank you for this library!

If found a small bug (at least for newer versions of nanopb) that is easily solvable.

stream->bytes_left should be set to 0 if the stream is empty (see https://jpa.kapsi.fi/nanopb/docs/concepts.html section Input streams). Currently you get an io error when parsing the stream.

The change below fixes this issue. I am not sure if you are still maintaining this package, but if you are I don't mind creating a pull request.

static bool pb_stream_read(pb_istream_t *stream, pb_byte_t *buf, size_t count) {
    Stream *s = reinterpret_cast<Stream *>(stream->state);
    size_t read = s->readBytes(buf, count);
    if (read == 0)
        stream->bytes_left = 0;
    return read == count;
};

floriaanpost avatar Mar 05 '23 22:03 floriaanpost