forked from nanopb/nanopb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored main decode loop to remove check against bytes_left
This is part of the upcoming fast decode commit. The fast path didn"t compare against bytes_left in the main loop, which caused a number of tests to fail as unexpected end-of-stream errors were raised on the stream. This change hoists the check for bytes_left from pb_decode_varint32_eof, up into pb_decode_tag, such that eof is set before an error is raised when called with bytes_left == 0. This simplifies the main loop from - while (stream->bytes_left) if (!pb_decode_tag()) ... to - while (pb_decode_tag()) ... This is also important because bytes_left isn"t guaranteed to be valid for callback streams. This created an edge case where callback streams would set an "io error" when eof was hit, regardless of if decoding was successful or not. This error could then mask actual errors generated after the main loop (e.g. missing required fields). A workaround and accompanying regression test was added for this, and is being further tracked in issue nanopb#1017.
- Loading branch information
Showing
4 changed files
with
101 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Regression test for #1017: | ||
# Test that missing required fields are correctly reported by all streams. | ||
|
||
Import("env") | ||
|
||
env.NanopbProto("test.proto") | ||
|
||
p = env.Program(["test.c", | ||
"test.pb.c", | ||
"$COMMON/pb_decode.o", | ||
"$COMMON/pb_common.o"]) | ||
|
||
env.RunTest(p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <assert.h> | ||
#include <pb_decode.h> | ||
#include "unittests.h" | ||
#include "test.pb.h" | ||
#include <stdio.h> | ||
|
||
const uint8_t input_data[] = { | ||
0x08, 0x01, | ||
}; | ||
|
||
const size_t input_len = sizeof(input_data); | ||
|
||
bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count) | ||
{ | ||
size_t cursor = (size_t)(uintptr_t)stream->state; | ||
|
||
if ((cursor + count) > input_len) | ||
{ | ||
stream->bytes_left = 0; | ||
return false; | ||
} | ||
|
||
memcpy(buf, &input_data[cursor], count); | ||
cursor += count; | ||
|
||
*(uintptr_t *)(&stream->state) = (uintptr_t)cursor; | ||
|
||
return true; | ||
} | ||
|
||
int main() | ||
{ | ||
int status = 0; | ||
|
||
/* test buffer stream */ | ||
{ | ||
TestMessage msg = TestMessage_init_zero; | ||
pb_istream_t stream = pb_istream_from_buffer(input_data, input_len); | ||
TEST(pb_decode(&stream, TestMessage_fields, &msg)); | ||
TEST(msg.foo == 0x1); | ||
TEST(stream.errmsg == NULL); | ||
} | ||
|
||
/* test callback stream */ | ||
{ | ||
TestMessage msg = TestMessage_init_zero; | ||
pb_istream_t stream = {&stream_callback, 0, SIZE_MAX}; | ||
TEST(pb_decode(&stream, TestMessage_fields, &msg)); | ||
TEST(msg.foo == 0x1); | ||
TEST(stream.errmsg == NULL); | ||
} | ||
|
||
return status; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
syntax = "proto2"; | ||
|
||
message TestMessage { | ||
required int32 foo = 1; | ||
optional int32 bar = 2; | ||
} |