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

syntax: Reject semver version individual parts in strings #14886

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

Xanewok
Copy link

@Xanewok Xanewok commented Feb 26, 2024

This seems like an oversight from the initial implementation that supported versions such as "^0.4.0" but which now has a side effect of converting individual string literals to their token values, allowing for syntax such as:

  • 0 "." 8 "." 0
  • "1".2.3"
  • "1"."2"."3"

and so on.

This fix aims to address the unintentional acceptance of such syntax, primarily to simplify the logic of external parsers and to more clearly establish what constitutes valid syntax for a crucial element like the version pragma statement.

I haven't written C++ in ages, so let me know if I'm doing anything dumb or if it could be written better.

Closes #14826

This seems like an oversight from the initial implementation that
supported versions such as "^0.4.0" but which now has a side effect of
converting individual string literals to their token values, allowing
for syntax such as:
- `0 "." 8 "." 0`
- '"1".2.3"`
- '"1"."2"."3"`

and so on.

This fix aims to address the unintentional acceptance of such syntax,
primarily to simplify the logic of external parsers and to more clearly
establish what constitutes valid syntax for a crucial element
like the version pragma statement.
Copy link

Thank you for your contribution to the Solidity compiler! A team member will follow up shortly.

If you haven't read our contributing guidelines and our review checklist before, please do it now, this makes the reviewing process and accepting your contribution smoother.

If you have any questions or need our help, feel free to post them in the PR or talk to us directly on the #solidity-dev channel on Matrix.

Copy link
Collaborator

@nikola-matic nikola-matic left a comment

Choose a reason for hiding this comment

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

Hi @Xanewok, even though this is technically breaking - it's such an obscure breakage, that we can treat it as a bugfix, and release it regularly. Can you please add a changelog entry, and take a look at the comments I've left. I'll spend a bit more time reviewing this later to see if and how the test coverage should be improved.

// Validate that the parsed version parts are either a single string literal or multiple bare tokens,
// i.e. "1.2.3" or 1.2.3 but not 1."2.3", "1".2.3 or 1"."2.3.
auto const partsEndPos = m_pos; // Points *after* the last version part
for (auto i = partsStartPos; i < partsEndPos; ++i)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you even this loop and the positional tracking? I think you can have a counter/boolean in the above while loop, and check whether currentToken() == Token::StringLiteral, and then error out as soon as you have more than 1?

Comment on lines +261 to +264
if (m_tokens[i] == Token::StringLiteral && partsStartPos != partsEndPos - 1)
{
solThrow(SemVerError, "String literals are only allowed as the only component in a version pragma.");
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also a side note - our style is to not use scoping braces {} in conditional/loop blocks if they're one liners, i.e., this if block would turn into:

Suggested change
if (m_tokens[i] == Token::StringLiteral && partsStartPos != partsEndPos - 1)
{
solThrow(SemVerError, "String literals are only allowed as the only component in a version pragma.");
}
if (m_tokens[i] == Token::StringLiteral && partsStartPos != partsEndPos - 1)
solThrow(SemVerError, "String literals are only allowed as the only component in a version pragma.");

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

Successfully merging this pull request may close these issues.

version pragma accepts partial string literals in any position
2 participants