Skip to content

Commit 09f852e

Browse files
committed
PR suggestion fixes.
1 parent 557e86b commit 09f852e

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

cpp/ql/lib/semmle/code/cpp/commons/DateTime.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class PackedTimeType extends Type {
1414
}
1515
}
1616

17-
private predicate timeType(string typeName) { typeName = ["_SYSTEMTIME", "SYSTEMTIME", "tm", "TIME_FIELDS", "PTIME_FIELDS"] }
17+
private predicate timeType(string typeName) {
18+
typeName = ["_SYSTEMTIME", "SYSTEMTIME", "tm", "TIME_FIELDS", "_TIME_FIELDS", "PTIME_FIELDS"]
19+
}
1820

1921
/**
2022
* A type that is used to represent times and dates in an 'unpacked' form, that is,
@@ -115,4 +117,4 @@ class TimeFieldsMonthFieldAccess extends MonthFieldAccess {
115117
*/
116118
class TimeFieldsYearFieldAccess extends YearFieldAccess {
117119
TimeFieldsYearFieldAccess() { this.getTarget().getName() = "Year" }
118-
}
120+
}

cpp/ql/src/Likely Bugs/Leap Year/LeapYear.qll

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,8 @@ final class ExprCheckCenturyComponentDiv400Inverted extends ExprCheckCenturyComp
180180
*/
181181
class ExprCheckCenturyComponent extends LogicalOrExpr {
182182
ExprCheckCenturyComponent() {
183-
exists(ExprCheckCenturyComponentDiv400 exprDiv400, ExprCheckCenturyComponentDiv100 exprDiv100 |
184-
this.getAnOperand() = exprDiv100 and
185-
this.getAnOperand() = exprDiv400
186-
)
183+
this.getAnOperand() instanceof ExprCheckCenturyComponentDiv100 and
184+
this.getAnOperand() instanceof ExprCheckCenturyComponentDiv400
187185
}
188186

189187
Expr getYearExpr() {
@@ -205,8 +203,8 @@ abstract class ExprCheckLeapYear extends Expr { }
205203
*/
206204
final class ExprCheckLeapYearFormA extends ExprCheckLeapYear, LogicalAndExpr {
207205
ExprCheckLeapYearFormA() {
208-
exists(Expr e, ExprCheckCenturyComponent centuryCheck |
209-
e = moduloCheckEQ_0(this.getLeftOperand(), 4) and
206+
exists(ExprCheckCenturyComponent centuryCheck |
207+
exists(moduloCheckEQ_0(this.getLeftOperand(), 4)) and
210208
centuryCheck = this.getAnOperand().getAChild*()
211209
)
212210
}
@@ -219,10 +217,11 @@ final class ExprCheckLeapYearFormA extends ExprCheckLeapYear, LogicalAndExpr {
219217
*/
220218
final class ExprCheckLeapYearFormB extends ExprCheckLeapYear, LogicalOrExpr {
221219
ExprCheckLeapYearFormB() {
222-
exists(VariableAccess va1, VariableAccess va2, VariableAccess va3 |
223-
va1 = moduloCheckEQ_0(this.getAnOperand(), 400) and
224-
va2 = moduloCheckNEQ_0(this.getAnOperand().(LogicalAndExpr).getAnOperand(), 100) and
225-
va3 = moduloCheckEQ_0(this.getAnOperand().(LogicalAndExpr).getAnOperand(), 4)
220+
exists(LogicalAndExpr land |
221+
exists(moduloCheckEQ_0(this.getAnOperand(), 400)) and
222+
land = this.getAnOperand() and
223+
exists(moduloCheckNEQ_0(land.getAnOperand(), 100)) and
224+
exists(moduloCheckEQ_0(land.getAnOperand(), 4))
226225
)
227226
}
228227
}
@@ -375,32 +374,24 @@ class DateCheckMonthFebruary extends Operation {
375374
/**
376375
* `stDate.wDay == 29`
377376
*/
378-
class DateCheckDay29 extends Operation {
379-
DateCheckDay29() {
380-
this.getOperator() = "==" and
381-
this.getAnOperand() instanceof DayFieldAccess and
382-
this.getAnOperand().(Literal).getValue() = "29"
383-
}
377+
class DateCheckDay29 extends EQExpr {
378+
DayFieldAccess dfa;
379+
380+
DateCheckDay29() { this.hasOperands(dfa, any(Literal lit | lit.getValue() = "29")) }
384381

385-
Expr getDateQualifier() { result = this.getAnOperand().(DayFieldAccess).getQualifier() }
382+
Expr getDateQualifier() { result = dfa.getQualifier() }
386383
}
387384

388385
/**
389386
* The combination of a February and Day 29 verification
390387
* `stDate.wMonth == 2 && stDate.wDay == 29`
391388
*/
392-
class DateFebruary29Check extends Operation {
393-
DateFebruary29Check() {
394-
this.getOperator() = "&&" and
395-
exists(DateCheckMonthFebruary checkFeb, DateCheckDay29 check29 |
396-
checkFeb = this.getAnOperand() and
397-
check29 = this.getAnOperand()
398-
)
399-
}
389+
class DateFebruary29Check extends LogicalAndExpr {
390+
DateCheckMonthFebruary checkFeb;
400391

401-
Expr getDateQualifier() {
402-
result = this.getAnOperand().(DateCheckMonthFebruary).getDateQualifier()
403-
}
392+
DateFebruary29Check() { this.hasOperands(checkFeb, any(DateCheckDay29 check29)) }
393+
394+
Expr getDateQualifier() { result = checkFeb.getDateQualifier() }
404395
}
405396

406397
/**
@@ -533,20 +524,20 @@ class TimeConversionFunction extends Function {
533524
TimeConversionFunction() {
534525
autoLeapYearCorrecting = false and
535526
(
536-
this.getQualifiedName() =
527+
this.getName() =
537528
[
538529
"FileTimeToSystemTime", "SystemTimeToFileTime", "SystemTimeToTzSpecificLocalTime",
539530
"SystemTimeToTzSpecificLocalTimeEx", "TzSpecificLocalTimeToSystemTime",
540531
"TzSpecificLocalTimeToSystemTimeEx", "RtlLocalTimeToSystemTime",
541-
"RtlTimeToSecondsSince1970", "_mkgmtime", "SetSystemTime", "VarUdateFromDate",
542-
"boost::gregorian::date::from_tm"
532+
"RtlTimeToSecondsSince1970", "_mkgmtime", "SetSystemTime", "VarUdateFromDate", "from_tm"
543533
]
544534
or
545-
this.getQualifiedName().matches("GetDateFormat%")
535+
// Matches all forms of GetDateFormat, e.g. GetDateFormatA/W/Ex
536+
this.getName().matches("GetDateFormat%")
546537
)
547538
or
548539
autoLeapYearCorrecting = true and
549-
this.getQualifiedName() =
540+
this.getName() =
550541
["mktime", "_mktime32", "_mktime64", "SystemTimeToVariantTime", "VariantTimeToSystemTime"]
551542
}
552543

cpp/ql/src/Likely Bugs/Leap Year/UncheckedLeapYearAfterYearModification.ql

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ class YearFieldAssignmentNode extends DataFlow::Node {
333333
YearFieldAccess access;
334334

335335
YearFieldAssignmentNode() {
336-
not this.getEnclosingCallable().getUnderlyingCallable() instanceof IgnorableFunction and
336+
exists(Function f |
337+
f = this.getEnclosingCallable().getUnderlyingCallable() and not f instanceof IgnorableFunction
338+
) and
337339
(
338340
this.asDefinition().(Assignment).getLValue() = access
339341
or
@@ -359,7 +361,11 @@ class YearFieldAssignmentNode extends DataFlow::Node {
359361
module OperationToYearAssignmentConfig implements DataFlow::ConfigSig {
360362
predicate isSource(DataFlow::Node n) { n.asExpr() instanceof OperationSource }
361363

362-
predicate isSink(DataFlow::Node n) { n instanceof YearFieldAssignmentNode }
364+
predicate isSink(DataFlow::Node n) {
365+
n instanceof YearFieldAssignmentNode and
366+
not isYearModifiedWithCheck(n) and
367+
not isControlledByMonthEqualityCheckNonFebruary(n.asExpr())
368+
}
363369

364370
predicate isBarrier(DataFlow::Node n) {
365371
exists(ArrayExpr arr | arr.getArrayOffset() = n.asExpr())
@@ -772,7 +778,14 @@ class LeapYearGuardCondition extends GuardCondition {
772778
* It is meant to capture the most common cases of false positives.
773779
*/
774780
module CandidateConstantToDayOrMonthAssignmentConfig implements DataFlow::ConfigSig {
775-
predicate isSource(DataFlow::Node source) { source.asExpr().getValue().toInt() in [1 .. 31] }
781+
predicate isSource(DataFlow::Node source) {
782+
source.asExpr().getValue().toInt() in [1 .. 31] and
783+
(
784+
exists(Assignment a | a.getRValue() = source.asExpr())
785+
or
786+
exists(Call c | c.getAnArgument() = source.asExpr())
787+
)
788+
}
776789

777790
predicate isSink(DataFlow::Node sink) {
778791
exists(Assignment a |
@@ -805,8 +818,6 @@ import OperationToYearAssignmentFlow::PathGraph
805818
from OperationToYearAssignmentFlow::PathNode src, OperationToYearAssignmentFlow::PathNode sink
806819
where
807820
OperationToYearAssignmentFlow::flowPath(src, sink) and
808-
not isYearModifiedWithCheck(sink.getNode()) and
809-
not isControlledByMonthEqualityCheckNonFebruary(sink.getNode().asExpr()) and
810821
// Check if a month is set in the same block as the year operation source
811822
// and the month value would indicate its set to any other month than february.
812823
// Finds if the source year node is in the same block as a source month block

0 commit comments

Comments
 (0)