diff --git a/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/ConstructorOrMethodWithExactDate.cpp b/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/ConstructorOrMethodWithExactDate.cpp new file mode 100644 index 00000000000..a1eef2e3b90 --- /dev/null +++ b/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/ConstructorOrMethodWithExactDate.cpp @@ -0,0 +1,40 @@ +class EraInfo +{ +public: + EraInfo() { + + }; + + EraInfo(int year, int month, int day) { + + }; + + EraInfo(int Era, int foo, int year, int month, int day, const wchar_t * eraName) + { + + } + + static EraInfo * EraInfoFromDate(int Era, int foo, int year, int month, int day, wchar_t * eraName) + { + return new EraInfo(Era, foo, year, month, day, eraName); + } +}; + +int Main() +{ + + // BAD: constructor creating a EraInfo with exact Heisei era start date + EraInfo * pDateTimeUtil = new EraInfo(1989, 1, 8); + + // BAD: constructor creating a EraInfo with exact Heisei era start date + EraInfo * pDateTimeUtil1 = new EraInfo(1, 2, 1989, 1, 8, L"\u5e73\u6210"); + + // Good: constructor creating a EraInfo with another date + EraInfo * pDateTimeUtil2 = new EraInfo(1, 2, 1900, 1, 1, L"foo"); + + // BAD: method call passing exact Haisei era start date as parameters + EraInfo * pDateTimeUtil3 = EraInfo::EraInfoFromDate(1, 2, 1989, 1, 8, L"\u5e73\u6210"); + + // GOOD: method call with the same parameters in a different order (we only track year, month, day) + EraInfo * pDateTimeUtil4 = EraInfo::EraInfoFromDate(1, 2, 8, 1, 1989, L"\u5e73\u6210"); +} \ No newline at end of file diff --git a/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/JapaneseEraDate.expected b/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/JapaneseEraDate.expected new file mode 100644 index 00000000000..920b0ecd86c --- /dev/null +++ b/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/JapaneseEraDate.expected @@ -0,0 +1,5 @@ +| ConstructorOrMethodWithExactDate.cpp:27:31:27:53 | call to EraInfo | Call that appears to have hard-coded Japanese era start date as parameter. | +| ConstructorOrMethodWithExactDate.cpp:30:32:30:77 | call to EraInfo | Call that appears to have hard-coded Japanese era start date as parameter. | +| ConstructorOrMethodWithExactDate.cpp:36:32:36:55 | call to EraInfoFromDate | Call that appears to have hard-coded Japanese era start date as parameter. | +| StructWithExactDate.cpp:31:13:31:19 | tm_year | A time struct that is initialized with exact Japanese calendar era start date. | +| StructWithExactDate.cpp:46:8:46:12 | wYear | A time struct that is initialized with exact Japanese calendar era start date. | diff --git a/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/JapaneseEraDate.qlref b/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/JapaneseEraDate.qlref new file mode 100644 index 00000000000..4240387a36c --- /dev/null +++ b/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/JapaneseEraDate.qlref @@ -0,0 +1 @@ +Best Practices/Magic Constants/JapaneseEraDate.ql diff --git a/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/StructWithExactDate.cpp b/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/StructWithExactDate.cpp new file mode 100644 index 00000000000..9c2b9eb0640 --- /dev/null +++ b/cpp/ql/test/query-tests/Best Practices/Magic Constants/Japanese Era/StructWithExactDate.cpp @@ -0,0 +1,57 @@ +typedef unsigned short WORD; + +struct tm +{ + int tm_sec; // seconds after the minute - [0, 60] including leap second + int tm_min; // minutes after the hour - [0, 59] + int tm_hour; // hours since midnight - [0, 23] + int tm_mday; // day of the month - [1, 31] + int tm_mon; // months since January - [0, 11] + int tm_year; // years since 1900 + int tm_wday; // days since Sunday - [0, 6] + int tm_yday; // days since January 1 - [0, 365] + int tm_isdst; // daylight savings time flag +}; + +typedef struct _SYSTEMTIME { + WORD wYear; + WORD wMonth; + WORD wDayOfWeek; + WORD wDay; + WORD wHour; + WORD wMinute; + WORD wSecond; + WORD wMilliseconds; +} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME; + +int main() +{ + // BAD: Creation of tm stuct corresponding to the beginning of Heisei era + tm *timeTm = new tm(); + timeTm->tm_year = 1989; + timeTm->tm_mon = 1; + timeTm->tm_mday = 8; + + + // GOOD: Creation of tm stuct with different date + tm *timeTm1 = new tm(); + timeTm1->tm_year = 1988; + timeTm1->tm_mon = 1; + timeTm1->tm_mday = 1; + + // BAD: Creation of SYSTEMTIME stuct corresponding to the beginning of Heisei era + SYSTEMTIME st; + st.wDay = 8; + st.wMonth = 1; + st.wYear = 1989; + + + // GOOD: Creation of SYSTEMTIME stuct with a different date + SYSTEMTIME st1; + st1.wDay = 1; + st1.wMonth = 1; + st1.wYear = 1990; + + return 0; +} +