Skip to content

Commit cdcc716

Browse files
authored
Merge pull request #1867 from geoffw0/erafix9
CPP: Add date to JapaneseEraDate.ql
2 parents bd32931 + 84112d3 commit cdcc716

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

change-notes/1.23/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The following changes in version 1.23 affect C/C++ analysis in all applications.
1717
| Query name (`query id`) | Expected impact | Message. |
1818
| 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. |
1919
| 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. |
20+
| Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) | More correct results | This query now checks for the beginning date of the Reiwa era (1st May 2019). |
2021

2122
## Changes to QL libraries
2223

cpp/ql/src/Best Practices/Magic Constants/JapaneseEraDate.ql

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,31 @@ predicate assignedDay(Struct s, DayFieldAccess day, int value) {
3636
)
3737
}
3838

39+
predicate eraDate(int year, int month, int day) {
40+
year = 1989 and month = 1 and day = 8
41+
or
42+
year = 2019 and month = 5 and day = 1
43+
}
44+
45+
3946
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
47+
exists(
48+
StructLikeClass s, YearFieldAccess year, MonthFieldAccess month, DayFieldAccess day,
49+
int yearValue, int monthValue, int dayValue
50+
|
51+
eraDate(yearValue, monthValue, dayValue) and
52+
assignedYear(s, year, yearValue) and
53+
assignedMonth(s, month, monthValue) and
54+
assignedDay(s, day, dayValue) and
4455
target = year and
4556
message = "A time struct that is initialized with exact Japanese calendar era start date."
4657
)
4758
}
4859

4960
predicate badCall(Element target, string message) {
5061
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
62+
eraDate(cc.getArgument(i).getValue().toInt(), cc.getArgument(i + 1).getValue().toInt(),
63+
cc.getArgument(i + 2).getValue().toInt()) and
5464
target = cc and
5565
message = "Call that appears to have hard-coded Japanese era start date as parameter."
5666
)

cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/ConstructorOrMethodWithExactDate.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ int Main()
3737

3838
// GOOD: method call with the same parameters in a different order (we only track year, month, day)
3939
EraInfo * pDateTimeUtil4 = EraInfo::EraInfoFromDate(1, 2, 8, 1, 1989, L"\u5e73\u6210");
40+
41+
// BAD: constructor creating a EraInfo with exact Reiwa era start date
42+
EraInfo * pDateTimeUtil5 = new EraInfo(2019, 5, 1);
4043
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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+
| ConstructorOrMethodWithExactDate.cpp:42:32:42:54 | call to EraInfo | Call that appears to have hard-coded Japanese era start date as parameter. |
45
| StructWithExactDate.cpp:31:13:31:19 | tm_year | A time struct that is initialized with exact Japanese calendar era start date. |
56
| StructWithExactDate.cpp:46:8:46:12 | wYear | A time struct that is initialized with exact Japanese calendar era start date. |
7+
| StructWithExactDate.cpp:60:9:60:13 | wYear | A time struct that is initialized with exact Japanese calendar era start date. |

cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/StructWithExactDate.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ int main()
5252
st1.wMonth = 1;
5353
st1.wYear = 1990;
5454

55+
56+
// BAD: Creation of SYSTEMTIME stuct corresponding to the beginning of Reiwa era
57+
SYSTEMTIME st2;
58+
st2.wDay = 1;
59+
st2.wMonth = 5;
60+
st2.wYear = 2019;
61+
5562
return 0;
5663
}
5764

0 commit comments

Comments
 (0)