Skip to content

Commit 7eee4f2

Browse files
committed
Adding LeapYear.qll and Adding365DaysPerYear ql and help.
1 parent 99fa75b commit 7eee4f2

File tree

3 files changed

+576
-0
lines changed

3 files changed

+576
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<include src="LeapYear.inc.qhelp" />
7+
8+
<p>When performing arithmetic operations on a variable that represents a date, leap years must be taken into account.
9+
It is not safe to assume that a year is 365 days long.</p>
10+
</overview>
11+
12+
<recommendation>
13+
<p>Determine whether the time span in question contains a leap day, then perform the calculation using the correct number
14+
of days. Alternatively, use an established library routine that already contains correct leap year logic.</p>
15+
</recommendation>
16+
17+
<references>
18+
<li>NASA / Goddard Space Flight Center - <a href="https://eclipse.gsfc.nasa.gov/SEhelp/calendars.html">Calendars</a></li>
19+
<li>Wikipedia - <a href="https://en.wikipedia.org/wiki/Leap_year_bug"> Leap year bug</a> </li>
20+
<li>Microsoft Azure blog - <a href="https://azure.microsoft.com/en-us/blog/is-your-code-ready-for-the-leap-year/"> Is your code ready for the leap year?</a> </li>
21+
</references>
22+
</qhelp>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @name Arithmetic operation assumes 365 days per year
3+
* @description When an arithmetic operation modifies a date by a constant
4+
* value of 365, it may be a sign that leap years are not taken
5+
* into account.
6+
* @kind problem
7+
* @problem.severity error
8+
* @id cpp/leap-year/adding-365-days-per-year
9+
* @precision medium
10+
* @tags leap-year
11+
* correctness
12+
* security
13+
*/
14+
15+
import cpp
16+
import LeapYear
17+
import semmle.code.cpp.dataflow.new.DataFlow
18+
19+
from Expr source, Expr sink
20+
where
21+
PossibleYearArithmeticOperationCheckFlow::flow(DataFlow::exprNode(source),
22+
DataFlow::exprNode(sink))
23+
select sink,
24+
"$@: This arithmetic operation $@ uses a constant value of 365 ends up modifying the date/time located at $@, without considering leap year scenarios.",
25+
sink.getEnclosingFunction(), sink.getEnclosingFunction().toString(), source, source.toString(),
26+
sink, sink.toString()

0 commit comments

Comments
 (0)