1 #include <pb_decode.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <assert.h>
5 #include "test_helpers.h"
6 #include "test.pb.h"
7
stream_callback(pb_istream_t * stream,uint8_t * buf,size_t count)8 bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
9 {
10 FILE *file = (FILE*)stream->state;
11 size_t len = fread(buf, 1, count, file);
12
13 if (len == count)
14 {
15 return true;
16 }
17 else
18 {
19 stream->bytes_left = 0;
20 return false;
21 }
22 }
23
main()24 int main()
25 {
26 pb_istream_t stream = {&stream_callback, NULL, SIZE_MAX};
27 MyMessage msg = MyMessage_init_default;
28 bool status;
29 stream.state = stdin;
30 SET_BINARY_MODE(stdin);
31
32 set_max_alloc_bytes(512);
33
34 status = pb_decode(&stream, MyMessage_fields, &msg);
35 assert(!status);
36 assert(strcmp(stream.errmsg, "realloc failed") == 0);
37 return 0;
38 }
39
40