Skip to content

Correctly validate characters in semantic version identifiers #254

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Sources/TSCUtility/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ public struct Version {
/// The build metadata.
public let buildMetadataIdentifiers: [String]

/// Creates a version object.
/// Creates a version with the provided components.
///
/// - Parameters:
/// - major: The major version number.
/// - minor: The minor version number.
/// - patch: The patch version number.
/// - prereleaseIdentifiers: The pre-release identifier.
/// - buildMetaDataIdentifiers: Build metadata that identifies a build.
///
/// - Precondition: `major >= 0 && minor >= 0 && patch >= 0`.
/// - Precondition: `prereleaseIdentifiers` can conatin only ASCII alpha-numeric characters and "-".
/// - Precondition: `buildMetaDataIdentifiers` can conatin only ASCII alpha-numeric characters and "-".
public init(
_ major: Int,
_ minor: Int,
Expand All @@ -37,6 +48,18 @@ public struct Version {
buildMetadataIdentifiers: [String] = []
) {
precondition(major >= 0 && minor >= 0 && patch >= 0, "Negative versioning is invalid.")
precondition(
prereleaseIdentifiers.allSatisfy {
$0.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") }
},
#"Pre-release identifiers can contain only ASCII alpha-numeric characters and "-"."#
)
precondition(
buildMetadataIdentifiers.allSatisfy {
$0.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") }
},
#"Build metadata identifiers can contain only ASCII alpha-numeric characters and "-"."#
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am concerned about the use of preconditions here for what could be a runtime issue. should this be throwing instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good point/suggestion. Making it throwing (or failable) also makes it possible to write tests against it.

Also, I found a few more bugs that I'm trying to fix before the 5.6 branch is cut: empty identifiers, leading zeros in numeric identifiers, and negative identifiers should not be allowed according to semver 2.0.0. With so many things to validate, it makes sense to throw errors for them instead of more lines of preconditions.

One slight problem though is that the call sites of this initializer in SwiftPM (and possibly llbuild and swift-driver too) need to be updated when this initializer becomes throwing or failable, so there might need some deprecation dances.

self.major = major
self.minor = minor
self.patch = patch
Expand Down