Skip to content

Commit b348803

Browse files
authored
Merge pull request #1594 from geoffw0/erafix7
CPP: Add test coverage for LeapYearFieldAccess.isModifiedByArithmeticOperation.
2 parents ab07128 + 67eb37f commit b348803

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
| test.cpp:173:2:173:52 | ... = ... | This arithmetic operation $@ uses a constant value of 365 ends up modifying the date/time located at $@, without considering leap year scenarios. | test.cpp:170:2:170:47 | ... += ... | ... += ... | test.cpp:173:2:173:52 | ... = ... | ... = ... |
22
| test.cpp:174:2:174:46 | ... = ... | This arithmetic operation $@ uses a constant value of 365 ends up modifying the date/time located at $@, without considering leap year scenarios. | test.cpp:170:2:170:47 | ... += ... | ... += ... | test.cpp:174:2:174:46 | ... = ... | ... = ... |
33
| test.cpp:193:2:193:24 | ... = ... | This arithmetic operation $@ uses a constant value of 365 ends up modifying the date/time located at $@, without considering leap year scenarios. | test.cpp:193:2:193:24 | ... = ... | ... = ... | test.cpp:193:2:193:24 | ... = ... | ... = ... |
4+
| test.cpp:217:2:217:52 | ... = ... | This arithmetic operation $@ uses a constant value of 365 ends up modifying the date/time located at $@, without considering leap year scenarios. | test.cpp:214:2:214:47 | ... += ... | ... += ... | test.cpp:217:2:217:52 | ... = ... | ... = ... |
5+
| test.cpp:218:2:218:46 | ... = ... | This arithmetic operation $@ uses a constant value of 365 ends up modifying the date/time located at $@, without considering leap year scenarios. | test.cpp:214:2:214:47 | ... += ... | ... += ... | test.cpp:218:2:218:46 | ... = ... | ... = ... |

cpp/ql/test/query-tests/Likely Bugs/Leap Year/Adding365DaysPerYear/test.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ void antipattern2()
170170
qwLongTime += 365 * 24 * 60 * 60 * 10000000LLU;
171171

172172
// copy back to a FILETIME
173-
ft.dwLowDateTime = (DWORD)(qwLongTime & 0xFFFFFFFF);
174-
ft.dwHighDateTime = (DWORD)(qwLongTime >> 32);
173+
ft.dwLowDateTime = (DWORD)(qwLongTime & 0xFFFFFFFF); // BAD
174+
ft.dwHighDateTime = (DWORD)(qwLongTime >> 32); // BAD
175175

176176
// convert back to SYSTEMTIME for display or other usage
177177
FileTimeToSystemTime(&ft, &st);
@@ -197,3 +197,29 @@ time_t mkTime(int days)
197197

198198
return t;
199199
}
200+
201+
void checkedExample()
202+
{
203+
// get the current time as a FILETIME
204+
SYSTEMTIME st; FILETIME ft;
205+
GetSystemTime(&st);
206+
SystemTimeToFileTime(&st, &ft);
207+
208+
// convert to a quadword (64-bit integer) to do arithmetic
209+
ULONGLONG qwLongTime;
210+
qwLongTime = (((ULONGLONG)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
211+
212+
// add a year by calculating the ticks in 365 days
213+
// (which may be incorrect when crossing a leap day)
214+
qwLongTime += 365 * 24 * 60 * 60 * 10000000LLU;
215+
216+
// copy back to a FILETIME
217+
ft.dwLowDateTime = (DWORD)(qwLongTime & 0xFFFFFFFF); // GOOD [FALSE POSITIVE]
218+
ft.dwHighDateTime = (DWORD)(qwLongTime >> 32); // GOOD [FALSE POSITIVE]
219+
220+
// convert back to SYSTEMTIME for display or other usage
221+
if (FileTimeToSystemTime(&ft, &st) == 0)
222+
{
223+
// handle error...
224+
}
225+
}

cpp/ql/test/query-tests/Likely Bugs/Leap Year/UncheckedLeapYearAfterYearModification/UncheckedLeapYearAfterYearModification.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
| test.cpp:636:11:636:17 | tm_year | Field $@ on variable $@ has been modified, but no appropriate check for LeapYear was found. | test.cpp:56:6:56:12 | tm_year | tm_year | test.cpp:628:12:628:19 | timeinfo | timeinfo |
1010
| test.cpp:640:5:640:9 | wYear | Field $@ on variable $@ has been modified, but no appropriate check for LeapYear was found. | test.cpp:12:7:12:11 | wYear | wYear | test.cpp:629:13:629:14 | st | st |
1111
| test.cpp:642:5:642:9 | wYear | Field $@ on variable $@ has been modified, but no appropriate check for LeapYear was found. | test.cpp:12:7:12:11 | wYear | wYear | test.cpp:629:13:629:14 | st | st |
12+
| test.cpp:718:5:718:9 | wYear | Field $@ on variable $@ has been modified, but no appropriate check for LeapYear was found. | test.cpp:12:7:12:11 | wYear | wYear | test.cpp:712:13:712:14 | st | st |
13+
| test.cpp:731:5:731:9 | wYear | Field $@ on variable $@ has been modified, but no appropriate check for LeapYear was found. | test.cpp:12:7:12:11 | wYear | wYear | test.cpp:725:13:725:14 | st | st |
14+
| test.cpp:732:5:732:9 | wYear | Field $@ on variable $@ has been modified, but no appropriate check for LeapYear was found. | test.cpp:12:7:12:11 | wYear | wYear | test.cpp:725:13:725:14 | st | st |
15+
| test.cpp:733:5:733:9 | wYear | Field $@ on variable $@ has been modified, but no appropriate check for LeapYear was found. | test.cpp:12:7:12:11 | wYear | wYear | test.cpp:725:13:725:14 | st | st |
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
| test.cpp:317:2:317:21 | call to SystemTimeToFileTime | Return value of $@ function should be verified to check for any error because variable $@ is not guaranteed to be safe. | test.cpp:63:1:63:20 | SystemTimeToFileTime | SystemTimeToFileTime | test.cpp:309:13:309:14 | st | st |
22
| test.cpp:330:2:330:21 | call to SystemTimeToFileTime | Return value of $@ function should be verified to check for any error because variable $@ is not guaranteed to be safe. | test.cpp:63:1:63:20 | SystemTimeToFileTime | SystemTimeToFileTime | test.cpp:322:13:322:14 | st | st |
33
| test.cpp:341:2:341:21 | call to SystemTimeToFileTime | Return value of $@ function should be verified to check for any error because variable $@ is not guaranteed to be safe. | test.cpp:63:1:63:20 | SystemTimeToFileTime | SystemTimeToFileTime | test.cpp:333:62:333:63 | st | st |
4+
| test.cpp:720:2:720:21 | call to SystemTimeToFileTime | Return value of $@ function should be verified to check for any error because variable $@ is not guaranteed to be safe. | test.cpp:63:1:63:20 | SystemTimeToFileTime | SystemTimeToFileTime | test.cpp:712:13:712:14 | st | st |
5+
| test.cpp:735:2:735:21 | call to SystemTimeToFileTime | Return value of $@ function should be verified to check for any error because variable $@ is not guaranteed to be safe. | test.cpp:63:1:63:20 | SystemTimeToFileTime | SystemTimeToFileTime | test.cpp:725:13:725:14 | st | st |

cpp/ql/test/query-tests/Likely Bugs/Leap Year/UncheckedLeapYearAfterYearModification/test.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ void CorrectPattern_check4()
445445
if (fixDate(st.wDay, st.wMonth, st.wYear))
446446
{
447447
// move back a day when landing on Feb 29 in an non-leap year
448-
st.wDay = 28;
448+
st.wDay = 28; // GOOD [FALSE POSITIVE]
449449
}
450450

451451
// Safe to use
@@ -680,3 +680,57 @@ void mkDateTest(int year)
680680
}
681681
// ...
682682
}
683+
684+
void unmodified1()
685+
{
686+
SYSTEMTIME st;
687+
FILETIME ft;
688+
WORD w;
689+
690+
GetSystemTime(&st);
691+
692+
w = st.wYear;
693+
694+
SystemTimeToFileTime(&st, &ft); // GOOD - no modification
695+
}
696+
697+
void unmodified2()
698+
{
699+
SYSTEMTIME st;
700+
FILETIME ft;
701+
WORD *w_ptr;
702+
703+
GetSystemTime(&st);
704+
705+
w_ptr = &(st.wYear);
706+
707+
SystemTimeToFileTime(&st, &ft); // GOOD - no modification
708+
}
709+
710+
void modified3()
711+
{
712+
SYSTEMTIME st;
713+
FILETIME ft;
714+
WORD *w_ptr;
715+
716+
GetSystemTime(&st);
717+
718+
st.wYear = st.wYear + 1; // BAD
719+
720+
SystemTimeToFileTime(&st, &ft);
721+
}
722+
723+
void modified4()
724+
{
725+
SYSTEMTIME st;
726+
FILETIME ft;
727+
WORD *w_ptr;
728+
729+
GetSystemTime(&st);
730+
731+
st.wYear++; // BAD
732+
st.wYear++; // BAD
733+
st.wYear++; // BAD
734+
735+
SystemTimeToFileTime(&st, &ft);
736+
}

0 commit comments

Comments
 (0)