# GNU Compiler Collection
# Debug info, warnings as errors
- env.Append(CFLAGS = '-ansi -g -O0 -Wall -Werror --coverage -fstack-protector-all')
+ env.Append(CFLAGS = '-ansi -pedantic -g -O0 -Wall -Werror --coverage -fstack-protector-all')
env.Append(LINKFLAGS = '--coverage')
# More strict checks on the nanopb core
- env.Append(CORECFLAGS = '-pedantic -Wextra -Wcast-qual -Wlogical-op -Wconversion')
+ env.Append(CORECFLAGS = '-Wextra -Wcast-qual -Wlogical-op -Wconversion')
elif 'clang' in env['CC']:
# CLang
- env.Append(CFLAGS = '-ansi -g -O0 -Wall -Werror')
- env.Append(CORECFLAGS = '-pedantic -Wextra -Wcast-qual -Wconversion')
+ env.Append(CFLAGS = '-ansi -pedantic -g -O0 -Wall -Werror')
+ env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion')
elif 'cl' in env['CC']:
# Microsoft Visual C++
env.Append(LINKFLAGS = '/DEBUG')
# More strict checks on the nanopb core
- env.Append(CORECFLAGS = '/W4 /Za')
+ env.Append(CORECFLAGS = '/W4')
# PB_RETURN_ERROR triggers C4127 because of while(0)
env.Append(CFLAGS = '/wd4127')
#include <string.h>
#include <pb_encode.h>
#include "alltypes_legacy.h"
+#include "test_helpers.h"
int main(int argc, char **argv)
{
}
alltypes.end = 1099;
-
- uint8_t buffer[1024];
- pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
-
- /* Now encode it and check if we succeeded. */
- if (pb_encode(&stream, AllTypes_fields, &alltypes))
- {
- fwrite(buffer, 1, stream.bytes_written, stdout);
- return 0; /* Success */
- }
- else
- {
- fprintf(stderr, "Encoding failed!\n");
- return 1; /* Failure */
+
+ {
+ uint8_t buffer[1024];
+ pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+
+ /* Now encode it and check if we succeeded. */
+ if (pb_encode(&stream, AllTypes_fields, &alltypes))
+ {
+ SET_BINARY_MODE(stdout);
+ fwrite(buffer, 1, stream.bytes_written, stdout);
+ return 0; /* Success */
+ }
+ else
+ {
+ fprintf(stderr, "Encoding failed!\n");
+ return 1; /* Failure */
+ }
}
}
#include <stdio.h>
#include <pb_decode.h>
#include "person.pb.h"
+#include "test_helpers.h"
/* This function is called once from main(), it handles
the decoding and printing.
int main()
{
- /* Maximum size is specified to prevent infinite length messages from
- * hanging this in the fuzz test.
- */
- pb_istream_t stream = {&callback, stdin, 10000};
+ pb_istream_t stream = {&callback, NULL, SIZE_MAX};
+ stream.state = stdin;
+ SET_BINARY_MODE(stdin);
+
if (!print_person(&stream))
{
printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
#include <stdio.h>
#include <pb_encode.h>
#include "person.pb.h"
+#include "test_helpers.h"
/* This binds the pb_ostream_t into the stdout stream */
bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
}};
/* Prepare the stream, output goes directly to stdout */
- pb_ostream_t stream = {&streamcallback, stdout, SIZE_MAX, 0};
+ pb_ostream_t stream = {&streamcallback, NULL, SIZE_MAX, 0};
+ stream.state = stdout;
+ SET_BINARY_MODE(stdout);
/* Now encode it and check if we succeeded. */
if (pb_encode(&stream, Person_fields, &person))
#include <stdio.h>
#include <pb_decode.h>
#include "callbacks.pb.h"
+#include "test_helpers.h"
bool print_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
if (!pb_decode_fixed64(stream, &value))
return false;
- printf((char*)*arg, (long long)value);
+ printf((char*)*arg, (long)value);
return true;
}
int main()
{
uint8_t buffer[1024];
- size_t length = fread(buffer, 1, 1024, stdin);
- pb_istream_t stream = pb_istream_from_buffer(buffer, length);
-
+ size_t length;
+ pb_istream_t stream;
/* Note: empty initializer list initializes the struct with all-0.
* This is recommended so that unused callbacks are set to NULL instead
* of crashing at runtime.
*/
- TestMessage testmessage = {};
+ TestMessage testmessage = {{{NULL}}};
+
+ SET_BINARY_MODE(stdin);
+ length = fread(buffer, 1, 1024, stdin);
+ stream = pb_istream_from_buffer(buffer, length);
testmessage.submsg.stringvalue.funcs.decode = &print_string;
testmessage.submsg.stringvalue.arg = "submsg {\n stringvalue: \"%s\"\n";
testmessage.submsg.fixed32value.funcs.decode = &print_fixed32;
testmessage.submsg.fixed32value.arg = " fixed32value: %ld\n";
testmessage.submsg.fixed64value.funcs.decode = &print_fixed64;
- testmessage.submsg.fixed64value.arg = " fixed64value: %lld\n}\n";
+ testmessage.submsg.fixed64value.arg = " fixed64value: %ld\n}\n";
testmessage.stringvalue.funcs.decode = &print_string;
testmessage.stringvalue.arg = "stringvalue: \"%s\"\n";
#include <string.h>
#include <pb_encode.h>
#include "callbacks.pb.h"
+#include "test_helpers.h"
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
bool encode_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
+ uint32_t value = 42;
+
if (!pb_encode_tag_for_field(stream, field))
return false;
- uint32_t value = 42;
return pb_encode_fixed32(stream, &value);
}
bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
+ uint64_t value = 42;
+
if (!pb_encode_tag_for_field(stream, field))
return false;
- uint64_t value = 42;
return pb_encode_fixed64(stream, &value);
}
int main()
{
uint8_t buffer[1024];
- pb_ostream_t stream = pb_ostream_from_buffer(buffer, 1024);
- TestMessage testmessage = {};
+ pb_ostream_t stream;
+ TestMessage testmessage = {{{NULL}}};
+
+ stream = pb_ostream_from_buffer(buffer, 1024);
testmessage.stringvalue.funcs.encode = &encode_string;
testmessage.int32value.funcs.encode = &encode_int32;
if (!pb_encode(&stream, TestMessage_fields, &testmessage))
return 1;
+ SET_BINARY_MODE(stdout);
if (fwrite(buffer, stream.bytes_written, 1, stdout) != 1)
return 2;
#include <pb_decode.h>
#include "alltypes.pb.h"
#include "extensions.pb.h"
+#include "test_helpers.h"
#define TEST(x) if (!(x)) { \
printf("Test " #x " failed.\n"); \
int main(int argc, char **argv)
{
uint8_t buffer[1024];
- size_t count = fread(buffer, 1, sizeof(buffer), stdin);
- pb_istream_t stream = pb_istream_from_buffer(buffer, count);
-
- AllTypes alltypes = {};
+ size_t count;
+ pb_istream_t stream;
+ AllTypes alltypes = {0};
int32_t extensionfield1;
- pb_extension_t ext1 = {&AllTypes_extensionfield1, &extensionfield1, NULL};
- alltypes.extensions = &ext1;
-
- ExtensionMessage extensionfield2 = {};
- pb_extension_t ext2 = {&ExtensionMessage_AllTypes_extensionfield2, &extensionfield2, NULL};
+ pb_extension_t ext1;
+ ExtensionMessage extensionfield2;
+ pb_extension_t ext2;
+
+ /* Read the message data */
+ SET_BINARY_MODE(stdin);
+ count = fread(buffer, 1, sizeof(buffer), stdin);
+ stream = pb_istream_from_buffer(buffer, count);
+
+ /* Add the extensions */
+ alltypes.extensions = &ext1;
+
+ ext1.type = &AllTypes_extensionfield1;
+ ext1.dest = &extensionfield1;
ext1.next = &ext2;
+ ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
+ ext2.dest = &extensionfield2;
+ ext2.next = NULL;
+
+ /* Decode the message */
if (!pb_decode(&stream, AllTypes_fields, &alltypes))
{
printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
return 1;
}
+ /* Check that the extensions decoded properly */
TEST(extensionfield1 == 12345)
TEST(strcmp(extensionfield2.test1, "test") == 0)
TEST(extensionfield2.test2 == 54321)
#include <pb_encode.h>
#include "alltypes.pb.h"
#include "extensions.pb.h"
+#include "test_helpers.h"
int main(int argc, char **argv)
{
- AllTypes alltypes = {};
+ uint8_t buffer[1024];
+ pb_ostream_t stream;
+ AllTypes alltypes = {0};
int32_t extensionfield1 = 12345;
- pb_extension_t ext1 = {&AllTypes_extensionfield1, &extensionfield1, NULL};
+ pb_extension_t ext1;
+ ExtensionMessage extensionfield2 = {"test", 54321};
+ pb_extension_t ext2;
+
+ /* Set up the extensions */
alltypes.extensions = &ext1;
- ExtensionMessage extensionfield2 = {"test", 54321};
- pb_extension_t ext2 = {&ExtensionMessage_AllTypes_extensionfield2, &extensionfield2, NULL};
+ ext1.type = &AllTypes_extensionfield1;
+ ext1.dest = &extensionfield1;
ext1.next = &ext2;
- uint8_t buffer[1024];
- pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+ ext2.type = &ExtensionMessage_AllTypes_extensionfield2;
+ ext2.dest = &extensionfield2;
+ ext2.next = NULL;
+
+ /* Set up the output stream */
+ stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
- /* Now encode it and check if we succeeded. */
+ /* Now encode the message and check if we succeeded. */
if (pb_encode(&stream, AllTypes_fields, &alltypes))
{
+ SET_BINARY_MODE(stdout);
fwrite(buffer, 1, stream.bytes_written, stdout);
return 0; /* Success */
}
int main()
{
- uint8_t buffer[512] = {};
+ uint8_t buffer[512];
/* Create a message with one missing field */
{
- MissingField msg = {};
+ MissingField msg = {0};
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+
if (!pb_encode(&stream, MissingField_fields, &msg))
{
printf("Encode failed.\n");
/* Test that it decodes properly if we don't require that field */
{
- MissingField msg = {};
+ MissingField msg = {0};
pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
if (!pb_decode(&stream, MissingField_fields, &msg))
/* Test that it does *not* decode properly if we require the field */
{
- AllFields msg = {};
+ AllFields msg = {0};
pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
if (pb_decode(&stream, AllFields_fields, &msg))