iamandi / nanopb

Automatically exported from code.google.com/p/nanopb
zlib License
0 stars 0 forks source link

pb_ostream_from_buffer and pb_istream_from_buffer return variable allocated on the stack #95

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use pb_ostream_from_buffer() or pb_istream_from_buffer() to get a stream
2. Call any additional functions that use the stack
3.

The resulting pb_*stream_t was allocated on the stack and then returned to the 
calling function. If any additional functions use the stack, this returned 
stream may be over-written.

I'd suggest re-writing the pb_istream_from_buffer() function as follows:

void pb_istream_from_buffer(uint8_t *buf, size_t bufsize, pb_istream_t *stream)
{
#ifdef PB_BUFFER_ONLY
    stream.callback = NULL;
#else
    stream.callback = &buf_read;
#endif
    stream.state = buf;
    stream.bytes_left = bufsize;
#ifndef PB_NO_ERRMSG
    stream.errmsg = NULL;
#endif
}

This puts the onus on the caller to provide space for the stream structure. 
They can either have a static stream structure or allocate it on the stack if 
they desire and will be sure that nothing will over-write this structure.

Original issue reported on code.google.com by d.as...@gmail.com on 12 Dec 2013 at 3:58

GoogleCodeExporter commented 9 years ago
No, it does not return a pointer to variable allocated on stack. It returns a 
structure as a value (copied on return).

See here for discussion:
https://code.google.com/p/nanopb/issues/detail?id=24

Original comment by Petteri.Aimonen on 12 Dec 2013 at 8:31