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

api: Fix usage of int.parse that implicitly accepts hexadecimal #326

Merged
merged 1 commit into from
Oct 18, 2023

Conversation

gnprice
Copy link
Member

@gnprice gnprice commented Oct 17, 2023

Calling just int.parse(s), without a radix: argument, invokes a special behavior where int.parse not only accepts decimal strings like "42", but also hexadecimal strings like "0x2a".

That's a bit unexpected. In any case it's definitely not something we want when interpreting any part of the Zulip API.

Fix the one place we had this in our own code. There remain two places it appears in the code generated by json_serializable; mark those with TODO comments. It'd be nice to fix those too, but realistically this quirk is unlikely to ever cause a problem, so it's not worth a lot of effort to resolve.

(Note that this doesn't affect the bulk of places we have an int in the API types, because most of those are handled by jsonDecode before the json_serializable-generated code ever sees them. It only affects the keys of Map<int, …> structures.)

It looks like there's no existing thread in the json_serializable tracker for this issue. The most closely related is from where the handling of Map<int, …> types was added in the first place:
google/json_serializable.dart#434

Calling just `int.parse(s)`, without a `radix:` argument, invokes
a special behavior where `int.parse` not only accepts decimal strings
like "42", but also hexadecimal strings like "0x2a".

That's a bit unexpected.  In any case it's definitely not something
we want when interpreting any part of the Zulip API.

Fix the one place we had this in our own code.  There remain two
places it appears in the code generated by `json_serializable`;
mark those with TODO comments.  It'd be nice to fix those too,
but realistically this quirk is unlikely to ever cause a problem,
so it's not worth a lot of effort to resolve.

(Note that this doesn't affect the bulk of places we have an int
in the API types, because most of those are handled by jsonDecode
before the `json_serializable`-generated code ever sees them.
It only affects the keys of `Map<int, …>` structures.)

It looks like there's no existing thread in the `json_serializable`
tracker for this issue.  The most closely related is from where the
handling of `Map<int, …>` types was added in the first place:
  google/json_serializable.dart#434
@chrisbobbe chrisbobbe merged commit f547771 into zulip:main Oct 18, 2023
1 check passed
@chrisbobbe
Copy link
Collaborator

Thanks, LGTM! Merged.

@gnprice gnprice deleted the pr-intparse branch October 18, 2023 21:19
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

Successfully merging this pull request may close these issues.

2 participants