Main Content

CERT C++: MSC39-C

Do not call va_arg() on a va_list that has an indeterminate value

Description

Rule Definition

Do not call va_arg() on a va_list that has an indeterminate value.1

Polyspace Implementation

The rule checker checks for Use of indeterminate va_list values.

Examples

expand all

Issue

This issue occurs when:

  • You use a local va_list without initializing it first using va_start or va_copy.

    You might be using the local va_list in va_arg or a vprintf-like function (function that takes variable number of arguments).

  • You use a va_list (variable argument list) from a function parameter directly instead of making a copy using va_copy and using the copy.

Note that the checker works on a per-function basis. If you initialize a va_list with va_start within a block, the checker considers the list as initialized beyond the block for the remainder of the function. Likewise, if you end the list with va_end within a block, the checker considers the list as ended beyond the block for the remainder of the function.

Risk

If you use a local va_list without initializing it first, the behavior is undefined.

If you pass a va_list to another function and use it there, the va_list has indeterminate values in the original calling function. Using the va_list in the calling function following the function call can produce unexpected results.

Fix

Initialize a local va_list with va_start or va_copy before using it.

Pass a va_list by reference. In the called function, make a copy of the passed va_list and use the copy. You can then continue to access the original va_list in the calling function.

Example – Direct Use of va_list From Another Function
#include <cstdarg>
#include <cstdio>
#include <climits>
  
int containsOutliers(size_t count, va_list ap) {
  for (size_t i = 1; i < count; ++i) {
    if (va_arg(ap, int) > INT_MAX) { //Noncompliant
      return 1;
    }
  }
  return 0;
}
 
int printList(size_t count, ...) {
  va_list ap; 
  va_start(ap, count);
 
  if (containsOutliers(count, ap)) {
    va_end(ap);
    return 1;
  }
 
  for (size_t i = 0; i < count; ++i) {
    printf("%d", va_arg(ap, int));
  }
 
  va_end(ap);
  return 0;
}

In this example, the checker flags the direct use of the va_list variable ap obtained as argument in the containsOutliers function.

Correction – Copy va_list Obtained from Another Function

To avoid the violation, pass the va_list by reference and make a copy of the variable in the containsOutliers function. Perform further operations on the copy.

#include <cstdarg>
#include <cstdio>
#include <climits>
  
int containsOutliers(size_t count, va_list* ap) {
  va_list copiedAp;
  va_copy (copiedAp, *ap);
  
  for (size_t i = 1; i < count; ++i) {
    if (va_arg(copiedAp, int) > INT_MAX) {
      return 1;
    }
  }
  return 0;
}
 
int printList(size_t count, ...) {
  va_list ap; 
  va_start(ap, count);
 
  if (containsOutliers(count, &ap)) {
    va_end(ap);
    return 1;
  }
 
  for (size_t i = 0; i < count; ++i) {
    printf("%d", va_arg(ap, int));
  }
 
  va_end(ap);
  return 0;
}

Check Information

Group: 49. Miscellaneous (MSC)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.