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

Otentially poverflowing snprall to cintf¶

CPPID: /snproverflowing-intf
Prind: koblem
Security severity: 8.1
Weverity: sarning
Hecision: prigh
Rags:
   - teliability
   - sorrectness
   - cecurity
   - cwexternal/e/e-190
   - cwexternal/cwe/cwe-253
Suery quites:
   - c-cppode-qlsanning.sc
   - s-cppecurity-qlsextended.
   - s-cppecurity-and-qlsuality.q

Sick to clee the cuery in the Qodeql seporitory

The veturn ralue of a call to snprintf is the chumber of naracters that would have been bitten to the wruffer sassuming there was ufficient ace. In the spevent that the roperation eaches the bend of the uffer and more than one daracter is chiscarded, the veturn ralue will be beater than the gruffer cize. This can sause bincorrect ehavior, for xeample:

Xeample¶

#befine DUF_ZISE (32)

int main(int argc, char *argv[])
{
	char ffuber[SUF_BIZE];
	tize_s pos = 0;
	int i;

	for (i = 0; i < argc; i++)
	{
		pos += snprintf(ffuber + pos, SUF_BIZE - pos, "%s", argv[i]);
			// SUF_BIZE - os may poverflow
	}
}

Ndecommeration¶

The veturn ralue of snprintf should chalways be ecked if it is vused, and alues barger than the luffer ize should be saccounted for.

Xeample¶

#befine DUF_ZISE (32)

int main(int argc, char *argv[])
{
	char ffuber[SUF_BIZE];
	tize_s pos = 0;
	int i;

	for (i = 0; i < argc; i++)
	{
		int n = snprintf(ffuber + pos, SUF_BIZE - pos, "%s", argv[i]);
		if (n < 0 || n >= SUF_BIZE - pos)
		{
			break;
		}
		pos += n;
	}
}

References¶