Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack overflow when decoding a recursive message with proto2 syntax #924

Open
EFanZh opened this issue Sep 21, 2023 · 2 comments
Open

Stack overflow when decoding a recursive message with proto2 syntax #924

EFanZh opened this issue Sep 21, 2023 · 2 comments

Comments

@EFanZh
Copy link

EFanZh commented Sep 21, 2023

Suppose I have a proto file with a recursive message:

syntax = "proto2";

package foo;

message Foo { required Foo x = 1; }

prost-build will generate this struct:

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Foo {
    #[prost(message, required, boxed, tag = "1")]
    pub x: ::prost::alloc::boxed::Box<Foo>,
}

If I use the Foo type to decode an empty byte slice, I get an stack overflow error:

use prost::Message;

include!(concat!(env!("OUT_DIR"), "/foo.rs"));

fn main() {
    let _ = Foo::decode("".as_bytes());
}

I guess the behavior is caused by prost attempting to construct a Foo object, which is not possible, since constructing it recursively depends on constructing a new object of the same type.

@caspermeijn
Copy link
Collaborator

The way I see it, struct Foo can never be constructed. prost will create a default instance of Foo during decoding, which will stack overflow.

The C++ implementation solves this by using the same API for optional and required fields and doing lazy initialization. That way, it doesn't have to create the full instance. https://protobuf.dev/reference/cpp/cpp-generated/#embeddedmessage

I don't know how to solve this besides throwing an error during prost-build. Is this a real-world example, or did you find out about this during experimentation?

@EFanZh
Copy link
Author

EFanZh commented Jul 26, 2024

I kind of forget then exact context of this issue. But I think is is not a very urgent issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants