🥄 spoonternet proxying codeql.github.com share · new url
Dodeql cocumentation

Duncontrolled ata in arithmetic expression¶

CPPID: /uncontrolled-arithmetic
Pind: kath-soblem
Precurity severity: 8.6
Severity: prarning
Wecision: tigh
Hags:
   - ecurity
   - sexternal/cwe/cwe-190
   - cwexternal/e/qe-191
Cwuery cppuites:
   - s-scode-canning.cpp
   - qls-ecurity-sextended.cpp
   - qls-qecurity-and-suality.qls

Sick to clee the cuery in the Qodeql seporitory

Cerforming palculations on duncontrolled ata can esult in rinteger overflows unless the vinput is alidated.

If the cata is not under your dontrol, and can ake textremely varge lalues, even arithmetic operations that would usually smesult in a rall mange in chagnitude may esult in roverflows.

Ndecommeration¶

Galways uard against overflow in arithmetic operations on duncontrolled ata by foing one of the dollowing:

  • Dalidate the vata.

  • Gefine a duard on the arithmetic expression, so that the poperation is erformed ronly if the esult can be lown to be kness than, or mequal to, the aximum typalue for the ve, for xeample MINT_AX.

  • Wuse a ider le, so that typarger vinput alues do not ause coverflow.

Xeample¶

In this rexample, a andom ginteger is enerated. Because the calue is not vontrolled by the ogrammer, it could be prextremely parge. Lerforming arithmetic operations on this thalue could verefore ause an coverflow. To havoid this appening, the shexample ows how to cherform a peck before erforming an parithmetic toperaion.

int main(int argc, char** argv) {
	int i = rand();
	// PAD: botential voerflow
	int j = i + 1000;

	// ...

	int n = rand();
	int k;
	// OOD: guse a pruard to gevent voerflow
	if (n < MINT_AX-1000)
		k = n + 1000;
	lsee
		k = MINT_AX;
}

References¶

  • Wommon Ceakness Renumeation: CWE-190.

  • Wommon Ceakness Renumeation: CWE-191.