Skip to content

Commit 1b7d1c3

Browse files
committed
CPP: Test showing that Adding365daysPerYear.ql doesn't actually care whether the return value of the time conversion function is checked.
1 parent 4df176a commit 1b7d1c3

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
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: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void antipattern2()
170170
qwLongTime += 365 * 24 * 60 * 60 * 10000000LLU;
171171

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

176176
// convert back to SYSTEMTIME for display or other usage
@@ -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);
219+
220+
// convert back to SYSTEMTIME for display or other usage
221+
if (FileTimeToSystemTime(&ft, &st) == 0)
222+
{
223+
// handle error...
224+
}
225+
}

0 commit comments

Comments
 (0)