JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A buffer is used as a C string, but does not contain a NULL terminating character.
| Class Name | Unterminated C String | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | security | ||||||||||||||||||||||||||||||
| Mnemonic | MISC.MEM.NTERM.CSTRING | ||||||||||||||||||||||||||||||
| Categories |
|
||||||||||||||||||||||||||||||
| Availability | Available for C and C++. |
||||||||||||||||||||||||||||||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Unterminated C String" |
#include <string.h>
#include <stdlib.h>
void f(void)
{
char buf[10];
char buf2[10];
const char *val = getenv("foo");
if( !val )
return;
strncpy( buf, val, sizeof(buf) - 1 );
strcpy( buf2, buf ); // Warning issued here because strncpy didn't terminate
}
CodeSonar ships with library models that allow it to recognize a large number of functions that have undefined or undesirable behavior when passed an unterminated string. Some examples are shown in the table below. If one of these functions is called with an unterminated string in the relevant parameter position, a warning will be issued.
If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Unterminated C String warnings.
The functions that CodeSonar considers to be capable of outputting unterminated strings are described below.
| Functions that can trigger warnings include... | |
|---|---|
| Apache Portable Runtime (APR) | apr_cpystrn(), apr_pmemdup() |
| gcc Builtins | __builtin___fprintf_chk(), __builtin___memcpy_chk(), __builtin___strncat_chk() |
| LDAP | ldap_search_ext() |
| libc | execl(), fprintf(), strcat() |
| libintl | dcgettext(), textdomain() |
| libpq | PQexec(), PQprepare() |
| Mac OS X | copyin() |
| ODBC | SQLExecDirectA(), SQLExecDirectUNIX(), SQLExecDirectW() |
| OpenSSL | CRYPTO_strdup() |
| Qt | fatal, qMemCopy() |
| C++ Standard Template Library (STL) | __throw_domain_error(), __throw_invalid_argument() |
| Win32 | PathResolve(), UnicodeToBytes(), wcscpy(), |
CodeSonar ships with library models that allow it to recognize a large number of functions that are capable of outputting unterminated strings. Some examples are shown in the table below.
If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be treated as outputting unterminated strings in the same circumstances.
Additionally, CodeSonar may determine that directly writing non-NULL characters into a string has caused the string to be unterminated.
| Functions that can output an unterminated string include... | |
|---|---|
| Apache Portable Runtime (APR) | apr_cpystrn(), apr_pmemdup() |
| C++ operators | operator new() |
| FreeRTOS | pvPortMalloc() |
| gcc Builtins | __builtin___memcpy_chk(), __builtin___strncpy_chk() |
| libc | copylist(), memcpy(), strdup() |
| Linux Kernel | copy_from_user(), kmalloc() |
| Mac OS X | copyin(), kalloc() |
| Nucleus | DMCE_Allocate_Memory(), PMCE_Create_Partition_Pool() |
| ODBC | SQLNativeSqlA(), SQLNativeSqlUNIX(), SQLNativeSqlW() |
| OpenSSL | CRYPTO_malloc(), CRYPTO_strdup() |
| Qt | qMalloc(), qMemCopy() |
| VxWorks | memPartAlignedAlloc(), msgQCreate() |
| Win32 | GlobalAlloc(), _fsopen(), wcsncpy() |
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.