33 */
44
55private import codeql.ruby.AST
6+ private import codeql.util.SemVer
67
78/**
89 * Provides classes and predicates for Gemfiles, including version constraint logic.
@@ -138,7 +139,7 @@ module Gemfile {
138139 exists ( int thisMajor , int thisMinor , int otherMajor , int otherMinor |
139140 thisMajor = this .getVersion ( ) .getMajor ( ) and
140141 thisMinor = this .getVersion ( ) .getMinor ( ) and
141- exists ( string maj , string mi | normalizeSemver ( other , _ , maj , mi , _) |
142+ exists ( string maj , string mi | exists ( padSemVer ( other , maj , mi , _) ) |
142143 otherMajor = maj .toInt ( ) and otherMinor = mi .toInt ( )
143144 )
144145 |
@@ -171,26 +172,26 @@ module Gemfile {
171172
172173 Version ( ) {
173174 this = any ( Gem c ) .getAVersionConstraint ( ) .getVersionString ( ) and
174- normalized = normalizeSemver ( this )
175+ normalized = padSemVer ( this )
175176 }
176177
177178 /**
178179 * Holds if this version is strictly before the version defined by `other`.
179180 */
180181 bindingset [ other]
181- predicate before ( string other ) { normalized < normalizeSemver ( other ) }
182+ predicate before ( string other ) { normalized < padSemVer ( other ) }
182183
183184 /**
184185 * Holds if this versino is equal to the version defined by `other`.
185186 */
186187 bindingset [ other]
187- predicate equal ( string other ) { normalized = normalizeSemver ( other ) }
188+ predicate equal ( string other ) { normalized = padSemVer ( other ) }
188189
189190 /**
190191 * Holds if this version is strictly after the version defined by `other`.
191192 */
192193 bindingset [ other]
193- predicate after ( string other ) { normalized > normalizeSemver ( other ) }
194+ predicate after ( string other ) { normalized > padSemVer ( other ) }
194195
195196 /**
196197 * Holds if this version defines a patch number.
@@ -212,43 +213,4 @@ module Gemfile {
212213 */
213214 int getPatch ( ) { result = getPatch ( normalized ) .toInt ( ) }
214215 }
215-
216- /**
217- * Normalizes a SemVer string such that the lexicographical ordering
218- * of two normalized strings is consistent with the SemVer ordering.
219- *
220- * Pre-release information and build metadata is not supported.
221- */
222- bindingset [ orig]
223- private predicate normalizeSemver (
224- string orig , string normalized , string major , string minor , string patch
225- ) {
226- major = getMajor ( orig ) and
227- (
228- minor = getMinor ( orig )
229- or
230- not exists ( getMinor ( orig ) ) and minor = "0"
231- ) and
232- (
233- patch = getPatch ( orig )
234- or
235- not exists ( getPatch ( orig ) ) and patch = "0"
236- ) and
237- normalized = leftPad ( major ) + "." + leftPad ( minor ) + "." + leftPad ( patch )
238- }
239-
240- bindingset [ orig]
241- private string normalizeSemver ( string orig ) { normalizeSemver ( orig , result , _, _, _) }
242-
243- bindingset [ s]
244- private string getMajor ( string s ) { result = s .regexpCapture ( "(\\d+).*" , 1 ) }
245-
246- bindingset [ s]
247- private string getMinor ( string s ) { result = s .regexpCapture ( "(\\d+)\\.(\\d+).*" , 2 ) }
248-
249- bindingset [ s]
250- private string getPatch ( string s ) { result = s .regexpCapture ( "(\\d+)\\.(\\d+)\\.(\\d+).*" , 3 ) }
251-
252- bindingset [ str]
253- private string leftPad ( string str ) { result = ( "000" + str ) .suffix ( str .length ( ) ) }
254216}
0 commit comments