Skip to content

Conversation

@poettering
Copy link
Member

A follow-up for #15991. Addressing #15985

@keszybz keszybz added the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Jun 2, 2020
@poettering poettering force-pushed the parse-int-fixlets branch from 1d0f56c to e8577e3 Compare June 3, 2020 09:17
@poettering
Copy link
Member Author

Force pushed new version. Only the issues pointed out addressed. But see comment above. PTAL

@poettering poettering removed the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Jun 3, 2020

s += strspn(s, WHITESPACE);
if (s[0] == '-')
return -ERANGE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should allow leading whitespace. Our parsers generally strip whitespace earlier, so if we get here with some leading whitespace, something strange is going on. But if the user somehow managed to feed us leading whitespace, we should fail here. But this behaviour didn't change with this PR, so let's leave that for later.


assert(s);

s += strspn(s, WHITESPACE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a good idea? Why should we allow leading whitespace here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, not sure if its, but strtol() does it implicitly when parsing normally, and that's documented. I wanted to make this as little invasive as possible, so I tried to add the 0b + 0o support in a fashion that behaves as similar as possible to how 0x is already handled.

(I must say, given all the weird behaviour of strtol() that keeps revealing itself I am tempted to just replace it altogether by our own code that parses only what we want it to parse, and doesn't eat whitspace and so on. it feels right now we need more code to block all the stuff it does we don't want than it would take to write our own implementation that only does what we want. but maybe let's leave that for a later PR)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace it altogether by our own code that parses only what we want it to parse

Indeed.

let's leave that for a later PR

Ack.


assert(s);

s += strspn(s, WHITESPACE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

This allows disabling a few alternative ways to decode integers
formatted as strings, for safety reasons.

See: systemd#15991
Let's allow "-0" as alternative to "+0" and "0" when parsing integers,
unless the new SAFE_ATO_REFUSE_PLUS_MINUS flag is specified.

In cases where allowing the +/- syntax shall not be allowed
SAFE_ATO_REFUSE_PLUS_MINUS is the right flag to use, but this also means
that -0 as only negative integer that fits into an unsigned value should
be acceptable if the flag is not specified.
All other safe_atoXYZ_full() functions have the parameter optional,
let's make it optoinal here, too.
Parsing is hard, hence let's use our own careful wrappers wherever
possible.
Let's refuse "+" and "-" prefixed UIDs. Let's refuse whitespace-prefixed
UIDS, Let's refuse zero-prefixed UIDs. Let's be safe than sorry.
parse_uid() does so many safety checks we want, hence rewrite
parse_uid_range() on top of parse_uid() instead of parse_range().
Let's adopt Python 3 style 0b and 0x syntaxes, because it makes a ton of
sense, in particular in bitmask settings.
@poettering
Copy link
Member Author

force pushed a new version. added the test case, but not much else, see comment above

@poettering poettering force-pushed the parse-int-fixlets branch from e8577e3 to 42e57a4 Compare June 5, 2020 14:06
@keszybz keszybz added the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Jun 5, 2020
@keszybz
Copy link
Member

keszybz commented Jun 5, 2020

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed util-lib

Development

Successfully merging this pull request may close these issues.

4 participants