Skip to content

Commit d432cd5

Browse files
committed
Address review comment
1 parent 1925c20 commit d432cd5

4 files changed

Lines changed: 39 additions & 55 deletions

File tree

go/ql/lib/semmle/go/dependencies/SemVer.qll

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ overlay[local?]
55
module;
66

77
import semmle.go.dependencies.Dependencies
8+
private import codeql.util.SemVer
89

910
/**
1011
* A SemVer-formatted version string in a dependency.
@@ -17,63 +18,42 @@ class DependencySemVer extends string {
1718

1819
DependencySemVer() {
1920
this = dep.getDepVersion() and
20-
normalized = normalizeSemver(this)
21+
normalized = normalizeSemVer(this)
2122
}
2223

2324
/**
2425
* Holds if this version may be before `last`.
2526
*/
2627
bindingset[last]
27-
predicate maybeBefore(string last) { normalized < normalizeSemver(last) }
28+
predicate maybeBefore(string last) { normalized < normalizeSemVer(last) }
2829

2930
/**
3031
* Holds if this version may be after `first`.
3132
*/
3233
bindingset[first]
33-
predicate maybeAfter(string first) { normalizeSemver(first) < normalized }
34+
predicate maybeAfter(string first) { normalizeSemVer(first) < normalized }
3435

3536
/**
3637
* Holds if this version may be between `first` (inclusive) and `last` (exclusive).
3738
*/
3839
bindingset[first, last]
3940
predicate maybeBetween(string first, string last) {
40-
normalizeSemver(first) <= normalized and
41-
normalized < normalizeSemver(last)
41+
normalizeSemVer(first) <= normalized and
42+
normalized < normalizeSemVer(last)
4243
}
4344

4445
/**
4546
* Holds if this version is equivalent to `other`.
4647
*/
4748
bindingset[other]
48-
predicate is(string other) { normalized = normalizeSemver(other) }
49+
predicate is(string other) { normalized = normalizeSemVer(other) }
4950

5051
/**
5152
* Gets the dependency that uses this string.
5253
*/
5354
Dependency getDependency() { result = dep }
5455
}
5556

56-
bindingset[str]
57-
private string leftPad(string str) { result = ("000" + str).suffix(str.length()) }
58-
59-
/**
60-
* Normalizes a SemVer string such that the lexicographical ordering
61-
* of two normalized strings is consistent with the SemVer ordering.
62-
*
63-
* Pre-release information and build metadata is not yet supported.
64-
*/
65-
bindingset[orig]
66-
private string normalizeSemver(string orig) {
67-
exists(string pattern, string major, string minor, string patch |
68-
pattern = "v?(\\d+)\\.(\\d+)\\.(\\d+)(\\D.*)?" and
69-
major = orig.regexpCapture(pattern, 1) and
70-
minor = orig.regexpCapture(pattern, 2) and
71-
patch = orig.regexpCapture(pattern, 3)
72-
|
73-
result = leftPad(major) + "." + leftPad(minor) + "." + leftPad(patch)
74-
)
75-
}
76-
7757
/**
7858
* A version string in a dependency that has a SemVer, but also contains a git commit SHA.
7959
*

javascript/ql/lib/semmle/javascript/dependencies/SemVer.qll

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import semmle.javascript.dependencies.Dependencies
6+
private import codeql.util.SemVer
67

78
/**
89
* A SemVer-formatted version string in a dependency.
@@ -15,59 +16,38 @@ class DependencySemVer extends string {
1516

1617
DependencySemVer() {
1718
dep.info(_, this) and
18-
normalized = normalizeSemver(this)
19+
normalized = normalizeSemVer(this)
1920
}
2021

2122
/**
2223
* Holds if this version may be before `last`.
2324
*/
2425
bindingset[last]
25-
predicate maybeBefore(string last) { normalized < normalizeSemver(last) }
26+
predicate maybeBefore(string last) { normalized < normalizeSemVer(last) }
2627

2728
/**
2829
* Holds if this version may be after `first`.
2930
*/
3031
bindingset[first]
31-
predicate maybeAfter(string first) { normalizeSemver(first) < normalized }
32+
predicate maybeAfter(string first) { normalizeSemVer(first) < normalized }
3233

3334
/**
3435
* Holds if this version may be between `first` (inclusive) and `last` (exclusive).
3536
*/
3637
bindingset[first, last]
3738
predicate maybeBetween(string first, string last) {
38-
normalizeSemver(first) <= normalized and
39-
normalized < normalizeSemver(last)
39+
normalizeSemVer(first) <= normalized and
40+
normalized < normalizeSemVer(last)
4041
}
4142

4243
/**
4344
* Holds if this version is equivalent to `other`.
4445
*/
4546
bindingset[other]
46-
predicate is(string other) { normalized = normalizeSemver(other) }
47+
predicate is(string other) { normalized = normalizeSemVer(other) }
4748

4849
/**
4950
* Gets the dependency that uses this string.
5051
*/
5152
Dependency getDependency() { result = dep }
5253
}
53-
54-
bindingset[str]
55-
private string leftPad(string str) { result = ("000" + str).suffix(str.length()) }
56-
57-
/**
58-
* Normalizes a SemVer string such that the lexicographical ordering
59-
* of two normalized strings is consistent with the SemVer ordering.
60-
*
61-
* Pre-release information and build metadata is not yet supported.
62-
*/
63-
bindingset[orig]
64-
private string normalizeSemver(string orig) {
65-
exists(string pattern, string major, string minor, string patch |
66-
pattern = "(\\d+)\\.(\\d+)\\.(\\d+)" and
67-
major = orig.regexpCapture(pattern, 1) and
68-
minor = orig.regexpCapture(pattern, 2) and
69-
patch = orig.regexpCapture(pattern, 3)
70-
|
71-
result = leftPad(major) + "." + leftPad(minor) + "." + leftPad(patch)
72-
)
73-
}

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ private import codeql.rust.elements.internal.CallExprImpl::Impl as CallExprImpl
4848
private import codeql.rust.internal.CachedStages
4949
private import codeql.rust.frameworks.stdlib.Builtins as Builtins
5050
private import codeql.util.Option
51+
private import codeql.util.SemVer
5152

5253
private newtype TNamespace =
5354
TTypeNamespace() or
@@ -572,7 +573,7 @@ class CrateItemNode extends NamedItemNode instanceof Crate {
572573
predicate isLatestVersion(string name) {
573574
this =
574575
max(CrateItemNode c, string ver |
575-
name = c.getName() and ver = c.(Crate).getVersion()
576+
name = c.getName() and ver = normalizeSemVer(c.(Crate).getVersion())
576577
|
577578
c order by ver
578579
)

shared/util/codeql/util/SemVer.qll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Provides logic for working SemVer (Semantic Versioning).
3+
*/
4+
bindingset[str]
5+
private string leftPad(string str) { result = ("000" + str).suffix(str.length()) }
6+
7+
/**
8+
* Normalizes a SemVer string such that the lexicographical ordering
9+
* of two normalized strings is consistent with the SemVer ordering.
10+
*
11+
* Pre-release information and build metadata is not yet supported.
12+
*/
13+
bindingset[orig]
14+
string normalizeSemVer(string orig) {
15+
exists(string pattern, string major, string minor, string patch |
16+
pattern = "v?(\\d+)\\.(\\d+)\\.(\\d+)(\\D.*)?" and
17+
major = orig.regexpCapture(pattern, 1) and
18+
minor = orig.regexpCapture(pattern, 2) and
19+
patch = orig.regexpCapture(pattern, 3)
20+
|
21+
result = leftPad(major) + "." + leftPad(minor) + "." + leftPad(patch)
22+
)
23+
}

0 commit comments

Comments
 (0)