メインコンテンツ

CERT C: Rec. API02-C

Functions that read or write to or from an array should take an argument to specify the source or target size

Since R2026a

Description

Functions that read or write to or from an array should take an argument to specify the source or target size1

Polyspace Implementation

The rule checker checks for Use of unbounded string function.

Examples

expand all

This issue occurs if you use any of these C standard library string functions in your code:

  • strcpy, strncpy

  • strcmp

  • strxfrm

  • strdup, strndup

  • strcoll

  • strcat

  • strchr, strrchr

  • strspn, strcspn

  • strpbrk

  • strstr

  • strtok

  • strlen

  • memchr

  • memset, memcpy, memchr, memmove

  • atoi, atof, atol, atoll

  • strtol, strtoll, strtoul, strtoull, strtof, strtod, stdtold

Risk

These C standard library string functions do not check the bounds of the buffer when reading from it or writing to it. These functions can accidentally read or write a buffer beyond its end, which is undefined behavior.

Fix

Instead of these unbounded C-string functions, use functions that specify the length of source and target buffers. The C standard Annex K defines bound checking versions of standard C library string functions. If Annex K is not available, implement your own string functions that perform bound checking.

Example

In this example, Polyspace® reports a violation on the use of the unbounded strcpy() function.

#include <string.h>

void foo ( const char * ptr2char )
{
   char str [ 10 ];
   strcpy ( str, ptr2char );   // Noncompliant   
}

Correction

To fix this issue, implement a version of strncpy() that performs bound checks on source and destination buffers.

#include <stdio.h>

char *improved_strncpy(char * restrict destination, size_t destSize, const char * restrict source, size_t sourceSize, size_t numelToCopy) {
    
    //...
}

void foo(const char *ptr2char, size_t numel) {
    char str[10];
    improved_strncpy(str, sizeof(str), ptr2char, numel , 10);
}

Check Information

Group: Rec. 13. Application Programming Interfaces (API)
PQL Name: std.cert.API02_C

Version History

Introduced in R2026a


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.