Skip to content

Commit 3a111df

Browse files
author
Colin Robertson
committed
Fix 2145 improve links, add example
1 parent 9ca8acb commit 3a111df

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

docs/code-quality/c26485.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,35 @@ ms.assetid: 8915ad2d-7fd6-4bbc-abe4-0b3292ea2170
88
---
99
# C26485 NO_ARRAY_TO_POINTER_DECAY
1010

11-
Like C26481, this check helps to enforce the rule I.13: *Do not pass an array as a single pointer* by detecting places where static array type information gets lost due to decay to a raw pointer. zstring and czstring types are not excluded.
11+
> warning C26485: Expression '*array-name*': No array to pointer decay (bounds.3).
1212
1313
## Remarks
1414

15-
C26481 and C26485 come from the [Bounds Safety Profile](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md) rules implemented in the first release of the C++ Core Guidelines Checker. They are applicable to raw pointers category since they help to avoid unsafe use of raw pointers.
15+
Like [C26481](c26481.md), this check helps to enforce the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md) rule [I.13](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Ri-array): *Do not pass an array as a single pointer* by detecting places where static array type information gets lost due to decay to a raw pointer. `zstring` and `czstring` types are not excluded.
16+
17+
C26481 and C26485 come from the [Bounds Safety Profile](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#SS-bounds) rules implemented in the first release of the C++ Core Guidelines Checker. They are applicable to the raw pointers category since they help to avoid unsafe use of raw pointers.
18+
19+
## Example
20+
21+
This sample results in two warnings for array to pointer decay in the call to `memcpy`.
22+
23+
```cpp
24+
// c26485_bad.cpp
25+
// compile using:
26+
// set Esp.Extensions=CppCoreCheck.dll
27+
// cl /W4 /EHsc /permissive- /analyze /analyze:plugin EspXEngine.dll /analyze:ruleset "%VSINSTALLDIR%\Team Tools\Static Analysis Tools\Rule Sets\CppCoreCheckBoundsRules.ruleset" c26485_bad.cpp
28+
#include <cstring>
29+
constexpr int array_length = 10;
30+
31+
int main() noexcept
32+
{
33+
int const from_array[array_length] = { 4, 3, 2, 1, 0, 9, 8, 7, 6, 5 };
34+
int to_array[array_length] = {};
35+
36+
if (nullptr != memcpy(to_array, from_array, sizeof(int) * array_length))
37+
return 0;
38+
return 1;
39+
}
40+
```
41+
42+
To address this issue, avoid calls that take pointer parameters, but don't manage bounds information. Use of such functions is often error-prone. Prefer C++ standard library calls to C runtime functions. An explicit cast to the decayed pointer type prevents the warning, but it doesn't prevent buggy code.

0 commit comments

Comments
 (0)