mirror of
https://github.com/github/codeql.git
synced 2026-04-25 08:45:14 +02:00
QL code and tests for C#/C++/JavaScript.
This commit is contained in:
51
javascript/ql/src/StandardLibrary/ParseIntRadix.qhelp
Normal file
51
javascript/ql/src/StandardLibrary/ParseIntRadix.qhelp
Normal file
@@ -0,0 +1,51 @@
|
||||
<!DOCTYPE qhelp PUBLIC
|
||||
"-//Semmle//qhelp//EN"
|
||||
"qhelp.dtd">
|
||||
<qhelp>
|
||||
<overview>
|
||||
<p>
|
||||
On some platforms, the builtin function <code>parseInt</code> parses strings starting with the digit
|
||||
<code>0</code> as octal values (unless an explicit radix is provided). This can lead to unexpected
|
||||
results when parsing decimal numbers that may be zero-padded, such as dates.
|
||||
</p>
|
||||
|
||||
</overview>
|
||||
<recommendation>
|
||||
|
||||
<p>
|
||||
Provide an explicit radix as the second parameter to <code>parseInt</code>.
|
||||
</p>
|
||||
|
||||
</recommendation>
|
||||
<example>
|
||||
|
||||
<p>
|
||||
In the following example, <code>parseInt</code> is used to convert the contents of a field in an HTML
|
||||
form to a number:
|
||||
</p>
|
||||
|
||||
<sample src="examples/ParseIntRadix.js" />
|
||||
|
||||
<p>
|
||||
Now assume that a user has entered a zero-padded decimal number, say <code>09</code>, into the form.
|
||||
Since the first digit is a zero, older versions of <code>parseInt</code> interpret this value as an
|
||||
octal number. When they then encounter <code>9</code> (which is not an octal digit), they will stop
|
||||
parsing and discard the rest of the string, returning the value <code>0</code>, which is probably not
|
||||
what was expected.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
To avoid this problem, an explicit radix parameter should be parsed as follows:
|
||||
</p>
|
||||
|
||||
<sample src="examples/ParseIntRadixGood.js" />
|
||||
|
||||
</example>
|
||||
<references>
|
||||
|
||||
|
||||
<li>D. Crockford, <i>JavaScript: The Good Parts</i>, Appendix A.7. O'Reilly, 2008.</li>
|
||||
|
||||
|
||||
</references>
|
||||
</qhelp>
|
||||
20
javascript/ql/src/StandardLibrary/ParseIntRadix.ql
Normal file
20
javascript/ql/src/StandardLibrary/ParseIntRadix.ql
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @name Call to parseInt without radix
|
||||
* @description Calls to the 'parseInt' function should always specify a radix to avoid accidentally
|
||||
* parsing a number as octal.
|
||||
* @kind problem
|
||||
* @problem.severity recommendation
|
||||
* @id js/parseint-without-radix
|
||||
* @tags reliability
|
||||
* maintainability
|
||||
* external/cwe/cwe-676
|
||||
* @precision very-high
|
||||
* @deprecated This is no longer a problem with modern browsers. Deprecated since 1.17.
|
||||
*/
|
||||
|
||||
import javascript
|
||||
|
||||
from DataFlow::CallNode parseInt
|
||||
where parseInt = DataFlow::globalVarRef("parseInt").getACall() and
|
||||
parseInt.getNumArgument() = 1
|
||||
select parseInt, "Missing radix parameter."
|
||||
@@ -0,0 +1,11 @@
|
||||
var adder = {
|
||||
sum: 0,
|
||||
add: function(x) {
|
||||
this.sum += x;
|
||||
},
|
||||
addAll: function(xs) {
|
||||
xs.forEach(function(x) {
|
||||
this.sum += x;
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
var adder = {
|
||||
sum: 0,
|
||||
add: function(x) {
|
||||
this.sum += x;
|
||||
},
|
||||
addAll: function(xs) {
|
||||
xs.forEach(function(x) {
|
||||
this.sum += x;
|
||||
}, this);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
var adder = {
|
||||
sum: 0,
|
||||
add: function(x) {
|
||||
this.sum += x;
|
||||
},
|
||||
addAll: function(xs) {
|
||||
var self = this;
|
||||
xs.forEach(function(x) {
|
||||
self.sum += x;
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
var day = parseInt(form.day.value);
|
||||
@@ -0,0 +1 @@
|
||||
var day = parseInt(form.day.value, 10);
|
||||
Reference in New Issue
Block a user