CWE-685: Function Call With Incorrect Number of Arguments

Learn about CWE-685 (Function Call With Incorrect Number of Arguments), its security impact, exploitation methods, and prevention guidelines.

What is Function Call With Incorrect Number of Arguments?

• Overview: Function Call With Incorrect Number of Arguments (CWE-685) occurs when a function, procedure, or routine is called with more or fewer arguments than it expects, leading to undefined behavior and potential vulnerabilities.

• Exploitation Methods:

  • Attackers can exploit this vulnerability by triggering unexpected behavior in the software, potentially causing the program to crash or execute malicious code.
  • Common attack patterns include crafting input that causes the program to call functions incorrectly, leading to buffer overflows or memory corruption.

• Security Impact:

  • Direct consequences of successful exploitation include program crashes, data corruption, or execution of arbitrary code.
  • Potential cascading effects include further vulnerabilities being exposed, unauthorized access, or privilege escalation.
  • Business impact can involve system downtime, data loss, reputational damage, and increased security costs.

• Prevention Guidelines:

  • Specific code-level fixes involve ensuring that function calls match the expected number of arguments and using compiler warnings to catch mismatches.
  • Security best practices include thorough code reviews, comprehensive testing, and adherence to language-specific guidelines for function calls.
  • Recommended tools and frameworks include static analysis tools to identify argument mismatches and using language features or libraries that enforce correct argument passing.

Corgea can automatically detect and fix Function Call With Incorrect Number of Arguments in your codebase. Try Corgea free today.

Technical Details

Likelihood of Exploit: Not specified

Affected Languages: C, Perl

Affected Technologies: Not specified

Vulnerable Code Example

// Example of a function call with an incorrect number of arguments
#include <stdio.h>

// A simple function that expects two integer arguments
void processNumbers(int a, int b) {
    printf("Processing numbers: %d and %d\n", a, b);
}

int main() {
    int x = 5;
    // Vulnerable function call: passing only one argument instead of two
    processNumbers(x);  // Incorrect number of arguments
    return 0;
}

Explanation:

  • The processNumbers function is defined to take two integer arguments.
  • In the main function, processNumbers is called with only one argument (x), which is incorrect.
  • This mismatch results in undefined behavior, potentially causing the program to crash or produce unexpected results because the second parameter b remains uninitialized and may contain garbage values.

How to fix Function Call With Incorrect Number of Arguments?

To fix this vulnerability, ensure that the number of arguments passed in a function call matches the number of parameters defined in the function signature. A mismatch can lead to undefined behavior, so carefully check both the definition and the call site of the function. If the function must handle a variable number of arguments, consider using variadic functions, but be cautious with their complexity and ensure correct handling of arguments.

Fixed Code Example

// Corrected code to match the function call with the function definition
#include <stdio.h>

// A simple function that expects two integer arguments
void processNumbers(int a, int b) {
    printf("Processing numbers: %d and %d\n", a, b);
}

int main() {
    int x = 5;
    int y = 10;
    // Fixed function call: passing two arguments as expected by the function
    processNumbers(x, y);  // Correct number of arguments
    return 0;
}

Explanation:

  • The processNumbers function still expects two integer arguments.
  • In the main function, both x and y are now passed as arguments to processNumbers, aligning with the function's signature.
  • This ensures correct execution and prevents undefined behavior by matching the function call with its definition.
  • By correctly passing both required arguments, the program runs as intended, displaying both numbers without risking undefined behavior.
Corgea Logo

Find this vulnerability and fix it with Corgea

Scan your codebase for CWE-685: Function Call With Incorrect Number of Arguments and get remediation guidance

Start for free and no credit card needed.