-
Notifications
You must be signed in to change notification settings - Fork 349
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
bigints allow type overflow when encoding #925
Comments
Are you expecting an error to be thrown or for it to just not encode the zero value (after overflow) because it's the default value?. |
Good question, I could not find compiler spec for protoc and the protoc c++ implementation dos exactly the same, so I would consider that the correct behaviour based on the reference implementation. On the other hand the rust implementation would catch that at compile-time, and this is a typescript library. Are there any guards for overflows or malformed input used in ts-proto currently? Maybe something like : const MAX_FIXED64 = 18_446_744_073_709_551_615n;
encode(message: MyMessage, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.ref > MAX_FIXED64) {
throw new Error("value for ref field in MyMessage is larger then MAX_FIXED64");
}
if (message.ref !== BigInt("0")) {
writer.uint32(9).fixed64(message.ref.toString());
}
}, |
We have somewhat similar detection for Number/long overflows: https://github.com/stephenh/ts-proto/blob/main/src/main.ts#L505
So @8de2fdb0 if you wanted to work on a PR that did similar runtime detection of "incoming bigint will be larger than fixed64", that would sgtm. I think it would be around in here: https://github.com/stephenh/ts-proto/blob/main/src/main.ts#L1246 But not 100% sure. Thanks! |
I opened #938 |
I have an issue with following generated snippet:
It uses
--ts_proto_opt=forceLong=bigint
and uses bigints as input.If I use following number as input for the
fixed64
the encoded protobuf value is 0So it basically overflows.
Is there any way to guard against this unexpected behaviour?
The text was updated successfully, but these errors were encountered: