Skip to content

Commit 3ba6509

Browse files
authored
Merge pull request #1847 from geoffw0/erafix8
CPP: Deal with two very similar Japanese era queries
2 parents 0e54709 + c4d74c3 commit 3ba6509

File tree

14 files changed

+99
-11
lines changed

14 files changed

+99
-11
lines changed

change-notes/1.23/analysis-cpp.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ The following changes in version 1.23 affect C/C++ analysis in all applications.
88

99
| **Query** | **Tags** | **Purpose** |
1010
|-----------------------------|-----------|--------------------------------------------------------------------|
11-
| Query name (`query id`) | tags | Message. |
11+
| Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) | reliability, japanese-era | This query is a combination of two old queries that were identical in purpose but separate as an implementation detail. This new query replaces Hard-coded Japanese era start date in call (`cpp/japanese-era/constructor-or-method-with-exact-era-date`) and Hard-coded Japanese era start date in struct (`cpp/japanese-era/struct-with-exact-era-date`). |
1212

1313
## Changes to existing queries
1414

1515
| **Query** | **Expected impact** | **Change** |
1616
|----------------------------|------------------------|------------------------------------------------------------------|
1717
| Query name (`query id`) | Expected impact | Message. |
18+
| Hard-coded Japanese era start date in call (`cpp/japanese-era/constructor-or-method-with-exact-era-date`) | Deprecated | This query has been deprecated. Use the new combined query Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) instead. |
19+
| Hard-coded Japanese era start date in struct (`cpp/japanese-era/struct-with-exact-era-date`) | Deprecated | This query has been deprecated. Use the new combined query Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) instead. |
1820

1921
## Changes to QL libraries
2022

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
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.
9+
</p>
10+
</overview>
11+
12+
<references>
13+
<li>
14+
<a href="https://blogs.msdn.microsoft.com/shawnste/2018/04/12/the-japanese-calendars-y2k-moment/">The Japanese Calendar's Y2K Moment</a>.
15+
</li>
16+
</references>
17+
</qhelp>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
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.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id cpp/japanese-era/exact-era-date
7+
* @precision medium
8+
* @tags reliability
9+
* japanese-era
10+
*/
11+
12+
import cpp
13+
import semmle.code.cpp.commons.DateTime
14+
15+
predicate assignedYear(Struct s, YearFieldAccess year, int value) {
16+
exists(Operation yearAssignment |
17+
s.getAField().getAnAccess() = year and
18+
yearAssignment.getAnOperand() = year and
19+
yearAssignment.getAnOperand().getValue().toInt() = value
20+
)
21+
}
22+
23+
predicate assignedMonth(Struct s, MonthFieldAccess month, int value) {
24+
exists(Operation monthAssignment |
25+
s.getAField().getAnAccess() = month and
26+
monthAssignment.getAnOperand() = month and
27+
monthAssignment.getAnOperand().getValue().toInt() = value
28+
)
29+
}
30+
31+
predicate assignedDay(Struct s, DayFieldAccess day, int value) {
32+
exists(Operation dayAssignment |
33+
s.getAField().getAnAccess() = day and
34+
dayAssignment.getAnOperand() = day and
35+
dayAssignment.getAnOperand().getValue().toInt() = value
36+
)
37+
}
38+
39+
predicate badStructInitialization(Element target, string message) {
40+
exists(StructLikeClass s, YearFieldAccess year, MonthFieldAccess month, DayFieldAccess day |
41+
assignedYear(s, year, 1989) and
42+
assignedMonth(s, month, 1) and
43+
assignedDay(s, day, 8) and
44+
target = year and
45+
message = "A time struct that is initialized with exact Japanese calendar era start date."
46+
)
47+
}
48+
49+
predicate badCall(Element target, string message) {
50+
exists(Call cc, int i |
51+
cc.getArgument(i).getValue().toInt() = 1989 and
52+
cc.getArgument(i + 1).getValue().toInt() = 1 and
53+
cc.getArgument(i + 2).getValue().toInt() = 8 and
54+
target = cc and
55+
message = "Call that appears to have hard-coded Japanese era start date as parameter."
56+
)
57+
}
58+
59+
from Element target, string message
60+
where
61+
badStructInitialization(target, message) or
62+
badCall(target, message)
63+
select target, message

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/**
2-
* @name Hard-coded Japanese era start date
2+
* @name Hard-coded Japanese era start date in call
33
* @description Japanese era changes can lead to code behaving differently. Avoid hard-coding Japanese era start dates.
44
* @kind problem
55
* @problem.severity warning
66
* @id cpp/japanese-era/constructor-or-method-with-exact-era-date
77
* @precision medium
88
* @tags reliability
99
* japanese-era
10+
* @deprecated This query is deprecated, use
11+
* Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`)
12+
* instead.
1013
*/
1114

1215
import cpp

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/**
2-
* @name Hard-coded Japanese era start date
2+
* @name Hard-coded Japanese era start date in struct
33
* @description Japanese era changes can lead to code behaving differently. Avoid hard-coding Japanese era start dates.
44
* @kind problem
55
* @problem.severity warning
66
* @id cpp/japanese-era/struct-with-exact-era-date
77
* @precision medium
88
* @tags reliability
99
* japanese-era
10+
* @deprecated This query is deprecated, use
11+
* Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`)
12+
* instead.
1013
*/
1114

1215
import cpp

cpp/ql/src/Likely Bugs/Memory Management/PotentialBufferOverflow.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
* security
1111
* external/cwe/cwe-676
1212
* @deprecated This query is deprecated, use
13-
* Security/CWE/CWE-120/OverrunWrite.ql and
14-
* Security/CWE/CWE-120/OverrunWriteFloat.ql instead.
13+
* Potentially overrunning write (`cpp/overrunning-write`) and
14+
* Potentially overrunning write with float to string conversion
15+
* (`cpp/overrunning-write-with-float) instead.
1516
*/
1617
import cpp
1718
import semmle.code.cpp.commons.Buffer

cpp/ql/src/Likely Bugs/OO/NonVirtualDestructor.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* @problem.severity warning
99
* @tags reliability
1010
* @deprecated This query is deprecated, and replaced by
11-
* jsf/4.10 Classes/AV Rule 78.ql, which has far fewer false
12-
* positives on typical code.
11+
* No virtual destructor (`cpp/jsf/av-rule-78`), which has far
12+
* fewer false positives on typical code.
1313
*/
1414

1515
import cpp

cpp/ql/test/query-tests/Likely Bugs/Japanese Era/ConstructorOrMethodWithExactDate.cpp renamed to cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/ConstructorOrMethodWithExactDate.cpp

File renamed without changes.

cpp/ql/test/query-tests/Likely Bugs/Japanese Era/ConstructorOrMethodWithExactEraDate.expected renamed to cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/JapaneseEraDate.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
| ConstructorOrMethodWithExactDate.cpp:27:31:27:53 | call to EraInfo | Call that appears to have hard-coded Japanese era start date as parameter. |
22
| ConstructorOrMethodWithExactDate.cpp:30:32:30:77 | call to EraInfo | Call that appears to have hard-coded Japanese era start date as parameter. |
33
| ConstructorOrMethodWithExactDate.cpp:36:32:36:55 | call to EraInfoFromDate | Call that appears to have hard-coded Japanese era start date as parameter. |
4+
| StructWithExactDate.cpp:31:13:31:19 | tm_year | A time struct that is initialized with exact Japanese calendar era start date. |
5+
| StructWithExactDate.cpp:46:8:46:12 | wYear | A time struct that is initialized with exact Japanese calendar era start date. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Best Practices/Magic Constants/JapaneseEraDate.ql

0 commit comments

Comments
 (0)