Skip to content

Commit 8e36316

Browse files
committed
C++: Addressing Copilot PR suggestions.
1 parent 1796bc0 commit 8e36316

File tree

4 files changed

+140
-187
lines changed

4 files changed

+140
-187
lines changed

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import cpp
1313
import LeapYear
1414
import semmle.code.cpp.controlflow.IRGuards
15-
import semmle.code.cpp.ir.IR
16-
import semmle.code.cpp.commons.DateTime
1715

1816
/**
1917
* Functions whose operations should never be considered a
@@ -92,11 +90,11 @@ abstract class IgnorableOperation extends Expr { }
9290
class IgnorableExprRem extends IgnorableOperation instanceof RemExpr { }
9391

9492
/**
95-
* Anything involving an operation with 10, 100, 1000, 10000 is often a sign of conversion
93+
* An operation with 10, 100, 1000, 10000 as an operand is often a sign of conversion
9694
* or atoi.
9795
*/
98-
class IgnorableExpr10MulipleComponent extends IgnorableOperation {
99-
IgnorableExpr10MulipleComponent() {
96+
class IgnorableExpr10MultipleComponent extends IgnorableOperation {
97+
IgnorableExpr10MultipleComponent() {
10098
this.(Operation).getAnOperand().getValue().toInt() in [10, 100, 1000, 10000]
10199
or
102100
exists(AssignOperation a | a.getRValue() = this |
@@ -106,7 +104,7 @@ class IgnorableExpr10MulipleComponent extends IgnorableOperation {
106104
}
107105

108106
/**
109-
* Anything involving a sub expression with char literal 48, ignore as a likely string conversion
107+
* An operation involving a sub expression with char literal 48, ignore as a likely string conversion
110108
* e.g., X - '0'
111109
*/
112110
class IgnorableExpr48Mapping extends IgnorableOperation {
@@ -118,7 +116,7 @@ class IgnorableExpr48Mapping extends IgnorableOperation {
118116
}
119117

120118
/**
121-
* A binary or arithemtic operation whereby one of the components is textual or a string.
119+
* A binary or arithmetic operation whereby one of the components is textual or a string.
122120
*/
123121
class IgnorableCharLiteralArithmetic extends IgnorableOperation {
124122
IgnorableCharLiteralArithmetic() {
@@ -170,7 +168,7 @@ predicate isLikelyConversionConstant(int c) {
170168
}
171169

172170
/**
173-
* Some constants indicate conversion that are ignorable, e.g.,
171+
* An `isLikelyConversionConstant` constant indicates conversion that is ignorable, e.g.,
174172
* julian to gregorian conversion or conversions from linux time structs
175173
* that start at 1900, etc.
176174
*/
@@ -208,7 +206,7 @@ class OperationAsArgToIgnorableFunction extends IgnorableOperation {
208206
}
209207

210208
/**
211-
* Literal OP literal means the result is constant/known
209+
* A Literal OP literal means the result is constant/known
212210
* and the operation is basically ignorable (it's not a real operation but
213211
* probably one visual simplicity what it means).
214212
*/
@@ -228,7 +226,7 @@ class IgnorableAssignmentBitwiseOperation extends IgnorableOperation instanceof
228226
{ }
229227

230228
/**
231-
* Any arithmetic operation where one of the operands is a pointer or char type, ignore it
229+
* An arithmetic operation where one of the operands is a pointer or char type, ignore it
232230
*/
233231
class IgnorablePointerOrCharArithmetic extends IgnorableOperation {
234232
IgnorablePointerOrCharArithmetic() {
@@ -267,7 +265,7 @@ class IgnorablePointerOrCharArithmetic extends IgnorableOperation {
267265
}
268266

269267
/**
270-
* An expression that is a candidate source for an dataflow configuration for an Operation that could flow to a Year field.
268+
* Holds for an expression that is a operation that could flow to a Year field.
271269
*/
272270
predicate isOperationSourceCandidate(Expr e) {
273271
not e instanceof IgnorableOperation and
@@ -397,8 +395,8 @@ module OperationToYearAssignmentConfig implements DataFlow::ConfigSig {
397395
// This is assuming a user would have done this all on one line though.
398396
// setting a variable for the conversion and passing that separately would be more difficult to track
399397
// considering this approach good enough for current observed false positives
400-
exists(Call c, Expr arg |
401-
isLeapYearCheckCall(c, arg) and arg.getAChild*() = [n.asExpr(), n.asIndirectExpr()]
398+
exists(Expr arg |
399+
isLeapYearCheckCall(_, arg) and arg.getAChild*() = [n.asExpr(), n.asIndirectExpr()]
402400
)
403401
or
404402
// If as the flow progresses, the value holding a dangerous operation result
@@ -503,9 +501,10 @@ class MonthEqualityCheckGuard extends GuardCondition, FinalMonthEqualityCheck {
503501
bindingset[e]
504502
pragma[inline_late]
505503
predicate isControlledByMonthEqualityCheckNonFebruary(Expr e) {
506-
exists(MonthEqualityCheckGuard monthGuard |
504+
exists(MonthEqualityCheckGuard monthGuard, Expr compared |
507505
monthGuard.controls(e.getBasicBlock(), true) and
508-
not monthGuard.getExprCompared().getValueText() = "2"
506+
compared = monthGuard.getExprCompared() and
507+
not compared.getValue().toInt() = 2
509508
)
510509
}
511510

@@ -641,7 +640,7 @@ class LeapYearGuardCondition extends GuardCondition {
641640
LogicalAndExpr andExpr, LogicalOrExpr orExpr, GuardCondition div4Check,
642641
GuardCondition div100Check, GuardCondition div400Check, GuardValue gv
643642
|
644-
// Cannonical case:
643+
// canonical case:
645644
// form: `(year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)`
646645
// `!((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0))`
647646
// `!(year % 4) && (year % 100 || !(year % 400))`
@@ -733,7 +732,7 @@ class LeapYearGuardCondition extends GuardCondition {
733732
Expr getYearSinkDiv400() { result = yearSinkDiv400 }
734733

735734
/**
736-
* The variable access that is used in all 3 components of the leap year check
735+
* Gets the variable access that is used in all 3 components of the leap year check
737736
* e.g., see getYearSinkDiv4/100/400..
738737
* If a field access is used, the qualifier and the field access are both returned
739738
* in checked condition.
@@ -802,7 +801,7 @@ module CandidateConstantToDayOrMonthAssignmentFlow =
802801
DataFlow::Global<CandidateConstantToDayOrMonthAssignmentConfig>;
803802

804803
/**
805-
* The value that the assignment resolves to doesn't represent February,
804+
* Holds if value the assignment `a` resolves to (`dayOrMonthValSrcExpr`) doesn't represent February,
806805
* and/or if it represents a day, is a 'safe' day (meaning the 27th or prior).
807806
*/
808807
bindingset[dayOrMonthValSrcExpr]

0 commit comments

Comments
 (0)