Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad489db

Browse files
author
Denis Levin
committed
Corrections related to the review comments.
1 parent 1b8117b commit ad489db

13 files changed

Lines changed: 142 additions & 146 deletions

cpp/ql/src/Likely Bugs/JapaneseEra/ConstructorOrMethodWithExactEraDate.qhelp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<qhelp>
55
<overview>
66
<p>
7-
When eras change, date and time conversions that rely on hard-coded era start date need to be reviewed. Conversions relying on Japanese dates in the current era can produce an ambiguous date.
7+
When eras change, date and time conversions that rely on a hard-coded era start date need to be reviewed. Conversions relying on Japanese dates in the current era can produce an ambiguous date.
8+
The values for the current Japanese era dates should be read from a source that will be updated, such as the Windows registry.
89
</p>
910
</overview>
1011

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* @name ConstructingJapaneseEraStartDate
3-
* @description Japanese era changes can lead to code behaving differently. Aviod hard-coding Japanese era start dates. The values should be read from registry.
2+
* @name Hard-coded Japanese era start date
3+
* @description Japanese era changes can lead to code behaving differently. Avoid hard-coding Japanese era start dates.
44
* @kind problem
55
* @problem.severity warning
6-
* @id cpp/JapaneseEra/Constructor-Or-Method-With-Exact-Era-Date
6+
* @id cpp/japanese-era/constructor-or-method-with-exact-era-date
77
* @precision medium
88
* @tags reliability
99
* japanese-era
@@ -14,4 +14,4 @@ from Call cc, int i
1414
where cc.getArgument(i).getValue().toInt() = 1989 and
1515
cc.getArgument(i+1).getValue().toInt() = 1 and
1616
cc.getArgument(i+2).getValue().toInt() = 8
17-
select cc, "Call that appears to have hard-coded Japanese era start date as parameter"
17+
select cc, "Call that appears to have hard-coded Japanese era start date as parameter."

cpp/ql/src/Likely Bugs/JapaneseEra/StructWithExactEraDate.qhelp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<qhelp>
55
<overview>
66
<p>
7-
When eras change, date and time conversions that rely on hard-coded era start date need to be reviewed. Conversions relying on Japanese dates in the current era can produce an ambiguous date.
7+
When eras change, date and time conversions that rely on a hard-coded era start date need to be reviewed. Conversions relying on Japanese dates in the current era can produce an ambiguous date.
8+
The values for the current Japanese era dates should be read from a source that will be updated, such as the Windows registry.
89
</p>
910
</overview>
1011

cpp/ql/src/Likely Bugs/JapaneseEra/StructWithExactEraDate.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* @name StructWithJapaneseEraStartDate
3-
* @description Japanese era changes can lead to code behaving differently. Aviod hard-coding Japanese era start dates. The values should be read from registry.
2+
* @name Hard-coded Japanese era start date
3+
* @description Japanese era changes can lead to code behaving differently. Avoid hard-coding Japanese era start dates.
44
* @kind problem
55
* @problem.severity warning
6-
* @id cpp/JapaneseEra/Struct-With-Exact-Era-Date
6+
* @id cpp/japanese-era/struct-with-exact-era-date
77
* @precision medium
88
* @tags reliability
99
* japanese-era
@@ -17,4 +17,4 @@ from StructLikeClass s, YearFieldAccess year, MonthFieldAccess month, DayFieldAc
1717
where s.getAField().getAnAccess () = year and yearAssignment.getAnOperand() = year and yearAssignment.getAnOperand().getValue().toInt() = 1989 and
1818
s.getAField().getAnAccess () = month and monthAssignment.getAnOperand() = month and monthAssignment.getAnOperand().getValue().toInt() = 1 and
1919
s.getAField().getAnAccess () = day and dayAssignment.getAnOperand() = day and dayAssignment.getAnOperand().getValue().toInt() = 8
20-
select year, "A time struct that is initialized with exact Japanese calendar era start date"
20+
select year, "A time struct that is initialized with exact Japanese calendar era start date."
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @name Year field changed using an arithmetic operation is used on an unchekced time conversion function
3-
* @description A year field changed using an arithmetic operation is used on a time conversion function, but the return value of the function is not check for success or failure
2+
* @name Year field changed using an arithmetic operation is used on an unchecked time conversion function
3+
* @description A year field changed using an arithmetic operation is used on a time conversion function, but the return value of the function is not checked for success or failure.
44
* @kind problem
55
* @problem.severity error
66
* @id cpp/leap-year/adding-365-days-per-year
@@ -13,9 +13,8 @@ import cpp
1313
import LeapYear
1414
import semmle.code.cpp.dataflow.DataFlow
1515

16-
1716
from Expr source, Expr sink, PossibleYearArithmeticOperationCheckConfiguration config
1817
where config.hasFlow(DataFlow::exprNode(source), DataFlow::exprNode(sink))
1918
select sink, "This arithmetic operation $@ uses a constant value of 365 ends up modifying the date/time located at $@, without considering leap year scenarios."
20-
, source, source.toString()
21-
, sink, sink.toString()
19+
, source, source.toString()
20+
, sink, sink.toString()

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
"-//Semmle//qhelp//EN"
33
"qhelp.dtd">
44
<qhelp>
5-
<overview>
6-
<p>The Gregorian Calendar, which has become the internationally accepted civil calendar, the leap year rule is: Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400.</p>
7-
<p>A leap year bug occurs when software (in any language) is written without consideration of leap year logic, or with flawed logic to calculate leap years; which typically results in incorrect results.</p>
8-
<p>The impact of these bugs may range from almost unnoticeable bugs such as an incorrect date, to severe bugs that affect reliability, availability or even the security of the affected system.</p>
9-
</overview>
5+
<overview>
6+
<p>The leap year rule for the Gregorian calendar, which has become the internationally accepted civil calendar, is: every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400.</p>
7+
<p>A leap year bug occurs when software (in any language) is written without consideration of leap year logic, or with flawed logic to calculate leap years; which typically results in incorrect results.</p>
8+
<p>The impact of these bugs may range from almost unnoticeable bugs such as an incorrect date, to severe bugs that affect reliability, availability or even the security of the affected system.</p>
9+
</overview>
1010

11-
<references>
12-
<li>U.S. Naval Observatory Website - <a href="https://aa.usno.navy.mil/faq/docs/calendars.php"> Introduction to Calendars</a></li>
13-
<li>Wikipedia - <a href="https://en.wikipedia.org/wiki/Leap_year_bug"> Leap year bug</a> </li>
14-
<li>Microsoft Azure blog - <a href="https://azure.microsoft.com/en-us/blog/is-your-code-ready-for-the-leap-year/"> Is your code ready for the leap year?</a> </li>
15-
</references>
11+
<references>
12+
<li>U.S. Naval Observatory Website - <a href="https://aa.usno.navy.mil/faq/docs/calendars.php"> Introduction to Calendars</a></li>
13+
<li>Wikipedia - <a href="https://en.wikipedia.org/wiki/Leap_year_bug"> Leap year bug</a> </li>
14+
<li>Microsoft Azure blog - <a href="https://azure.microsoft.com/en-us/blog/is-your-code-ready-for-the-leap-year/"> Is your code ready for the leap year?</a> </li>
15+
</references>
16+
</qhelp>

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Provides a library for helping create leap year realted queries
2+
* Provides a library for helping create leap year related queries
33
*/
44
import cpp
55
import semmle.code.cpp.dataflow.DataFlow
@@ -8,7 +8,6 @@ import semmle.code.cpp.commons.DateTime
88

99
/**
1010
* Get the top-level BinaryOperation enclosing the expression e
11-
* Not
1211
*/
1312
BinaryOperation getATopLevelBinaryOperationExpression(Expr e)
1413
{
@@ -73,7 +72,7 @@ abstract class LeapYearFieldAccess extends YearFieldAccess {
7372
* Holds if the field access is a modification,
7473
* and it involves an arithmetic operation.
7574
* In order to avoid false positives, the operation does not includes values that are normal for year normalization.
76-
*
75+
*
7776
* 1900 - struct tm counts years since 1900
7877
* 1980/80 - FAT32 epoch
7978
*/
Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Year field changed using an arithmetic operation without checking for leap year
3-
* @description A that field that represents a year is being modified by an arithmetic operation, but no proper check for leap year can be detected afterwards.
3+
* @description A field that represents a year is being modified by an arithmetic operation, but no proper check for leap years can be detected afterwards.
44
* @kind problem
55
* @problem.severity error
66
* @id cpp/leap-year/unchecked-after-arithmetic-year-modification
@@ -14,48 +14,45 @@ import LeapYear
1414

1515
from Variable var, LeapYearFieldAccess yfa
1616
where
17-
exists( VariableAccess va |
18-
yfa.getQualifier() = va
19-
and var.getAnAccess() = va
20-
// The year is modified with an arithmetic operation. Avoid values that are likely false positives
21-
and yfa.isModifiedByArithmeticOperationNotForNormalization()
22-
// Avoid false positives
23-
and not (
24-
// If there is a local check for leap year after the modification
25-
exists( LeapYearFieldAccess yfacheck |
26-
yfacheck.getQualifier() = var.getAnAccess()
27-
and yfacheck.isUsedInCorrectLeapYearCheck()
28-
and yfacheck = yfa.getASuccessor*()
29-
)
30-
// If there is a data flow from the variable that was modified to a function that seems to check for leap year
31-
or exists(
32-
VariableAccess source,
33-
ChecksForLeapYearFunctionCall fc,
34-
LeapYearCheckConfiguration config |
35-
source = var.getAnAccess()
36-
and config.hasFlow( DataFlow::exprNode(source), DataFlow::exprNode(fc.getAnArgument()))
37-
)
38-
// If there is a data flow from the field that was modified to a function that seems to check for leap year
39-
or exists(
40-
VariableAccess vacheck,
41-
YearFieldAccess yfacheck,
42-
ChecksForLeapYearFunctionCall fc,
43-
LeapYearCheckConfiguration config |
44-
vacheck = var.getAnAccess()
45-
and yfacheck.getQualifier() = vacheck
46-
and config.hasFlow( DataFlow::exprNode(yfacheck), DataFlow::exprNode(fc.getAnArgument()))
47-
)
48-
// If there is a successor or predecessor that sets the month = 1
49-
or exists(
50-
MonthFieldAccess mfa, AssignExpr ae |
51-
mfa.getQualifier() = var.getAnAccess()
52-
and mfa.isModified()
53-
and ( mfa = yfa.getASuccessor*()
54-
or yfa = mfa.getASuccessor*())
55-
and ae = mfa.getEnclosingElement()
56-
and ae.getAnOperand().getValue().toInt() = 1
57-
)
17+
exists(VariableAccess va |
18+
yfa.getQualifier() = va
19+
and var.getAnAccess() = va
20+
// The year is modified with an arithmetic operation. Avoid values that are likely false positives
21+
and yfa.isModifiedByArithmeticOperationNotForNormalization()
22+
// Avoid false positives
23+
and not (
24+
// If there is a local check for leap year after the modification
25+
exists( LeapYearFieldAccess yfacheck |
26+
yfacheck.getQualifier() = var.getAnAccess()
27+
and yfacheck.isUsedInCorrectLeapYearCheck()
28+
and yfacheck = yfa.getASuccessor*()
5829
)
59-
)
30+
// If there is a data flow from the variable that was modified to a function that seems to check for leap year
31+
or exists(VariableAccess source,
32+
ChecksForLeapYearFunctionCall fc,
33+
LeapYearCheckConfiguration config |
34+
source = var.getAnAccess()
35+
and config.hasFlow( DataFlow::exprNode(source), DataFlow::exprNode(fc.getAnArgument()))
36+
)
37+
// If there is a data flow from the field that was modified to a function that seems to check for leap year
38+
or exists(VariableAccess vacheck,
39+
YearFieldAccess yfacheck,
40+
ChecksForLeapYearFunctionCall fc,
41+
LeapYearCheckConfiguration config |
42+
vacheck = var.getAnAccess()
43+
and yfacheck.getQualifier() = vacheck
44+
and config.hasFlow( DataFlow::exprNode(yfacheck), DataFlow::exprNode(fc.getAnArgument()))
45+
)
46+
// If there is a successor or predecessor that sets the month = 1
47+
or exists(MonthFieldAccess mfa, AssignExpr ae |
48+
mfa.getQualifier() = var.getAnAccess()
49+
and mfa.isModified()
50+
and (mfa = yfa.getASuccessor*()
51+
or yfa = mfa.getASuccessor*())
52+
and ae = mfa.getEnclosingElement()
53+
and ae.getAnOperand().getValue().toInt() = 1
54+
)
55+
)
56+
)
6057
select yfa
6158
, "Field $@ on variable $@ has been modified, but no appropriate check for LeapYear was found.", yfa.getTarget(), yfa.getTarget().toString(), var, var.toString()

cpp/ql/src/Likely Bugs/Leap Year/UncheckedLeapYearAfterYearModificationBad.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ SYSTEMTIME st;
22
FILETIME ft;
33
GetSystemTime(&st);
44

5-
// Flawed logic will result in invalid date
5+
// Flawed logic may result in invalid date
66
st.wYear++;
77

88
// The following code may fail

cpp/ql/src/Likely Bugs/Leap Year/UncheckedReturnValueForTimeFunctions.qhelp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<overview>
66
<include src="LeapYear.qhelp" />
77

8-
<p>When using a function that transform a date structure, and the year on the input argument for teh API has been manipulated, it is important to check for the return value of the function to make sure it succeeded.</p>
8+
<p>When using a function that transforms a date structure, and the year on the input argument for the API has been manipulated, it is important to check for the return value of the function to make sure it succeeded.</p>
99
<p>Otherwise, the function may have failed, and the output parameter may contain invalid data that can cause any number of problems on the affected system.</p>
10-
<p>The following is a list of the function that this query covers:</p>
10+
<p>The following is a list of the functions that this query covers:</p>
1111
<list>
1212
<li><code>FileTimeToSystemTime</code></li>
1313
<li><code>SystemTimeToFileTime</code></li>
@@ -19,10 +19,10 @@
1919
<li><code>RtlTimeToSecondsSince1970</code></li>
2020
<li><code>_mkgmtime</code></li>
2121
</list>
22-
22+
2323
</overview>
2424
<recommendation>
25-
<p>When calling an API that transforms a date variable that was manipulated, always check for the return value to verify if the API call succeeded or failed.</p>
25+
<p>When calling an API that transforms a date variable that was manipulated, always check for the return value to verify that the API call succeeded.</p>
2626

2727
</recommendation>
2828
<example>

0 commit comments

Comments
 (0)