Replace more strcpy with safe string copy - #131308
Conversation
|
Azure Pipelines: Successfully started running 6 pipeline(s). 10 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @agocke |
| cgroupPathLength = parent_directory_end - mem_limit_filename; | ||
|
|
||
| strcpy(parent_directory_end, CGROUP2_MEMORY_LIMIT_FILENAME); | ||
| memcpy(parent_directory_end, CGROUP2_MEMORY_LIMIT_FILENAME, strlen(CGROUP2_MEMORY_LIMIT_FILENAME) + 1); |
There was a problem hiding this comment.
What guarantees that parent_directory_end has enough space? This pattern is likely to trigger static analyzers.
(not commenting on all places with this issue)
There was a problem hiding this comment.
It's same guarantees as before; none.
There was a problem hiding this comment.
The PR title is "Replace more strcpy with safe string copy". Why is this safe string copy then?
There was a problem hiding this comment.
strcpy -> memcpy/strcpy_s, because strcpy is being flagged by clang on OpenBSD warning: strcpy() is almost always misused, please use strlcpy().
There was a problem hiding this comment.
I think Jan's point is that if we're changing this code at all, we should change it to something better, not to something that has the same problem and is somewhat harder to read than the original.
There was a problem hiding this comment.
Not sure how it is harder to read than original given #130547 is merged?
There was a problem hiding this comment.
I was fine with #130547 since it was an open-coded strdup, no length arithmetic, length is computed on one line and used a few lines later in a very straightforward way. In hindsight, I should have suggested to just use strdup instead of open coding it. We use strdup in number of other places. Could you please replace it with strdup as part of these changes?
This code is not a straightforward like that.
We have SafeStringCopy in pal_utilities.h to solve this problem. Can we promote it or some variant of it to be usable everywhere? Would it be possible to standardize on strcpy_s and polyfill strcpy_s when it is not available - do we build on any platforms like that?
|
BA failure is #131256. |
There was a problem hiding this comment.
Pull request overview
This PR continues the effort to eliminate strcpy usage across CoreCLR, NativeAOT, and related tools by replacing it with length-aware copies (primarily memcpy) and minor refactors to compute/carry explicit buffer lengths.
Changes:
- Replaced multiple
strcpycall sites withmemcpy(often usingstrlen(...) + 1) across VM/JIT/GC/NativeAOT/host/tooling code. - Introduced explicit length variables in a few places to make copy sizes clearer and avoid repeated
strlenin allocation/copy sequences. - Added a couple of small robustness tweaks (e.g., guarding
mallocresults before copying incorerun.cpp).
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/coreclr/vm/gdbjit.cpp | Replaces strcpy with memcpy in GDB JIT debug-info string materialization paths. |
| src/coreclr/utilcode/debug.cpp | Replaces strcpy with memcpy when building the assert expression+stacktrace display buffer. |
| src/coreclr/utilcode/check.cpp | Uses memcpy for copying dynamically allocated CHECK failure messages. |
| src/coreclr/nativeaot/Runtime/unix/PalUnix.cpp | Uses computed length + memcpy in PalCopyTCharAsChar. |
| src/coreclr/nativeaot/Runtime/unix/cgroupcpu.cpp | Reworks cgroup path construction to use explicit lengths and memcpy instead of strcpy/strcat. |
| src/coreclr/nativeaot/Runtime/RhConfig.cpp | Refactors env-var name construction and switches embedded string copies to memcpy. |
| src/coreclr/nativeaot/Runtime/clrgc.enabled.cpp | Replaces strcpy concatenation with length-based memcpy copies. |
| src/coreclr/jit/fgdiagnostic.cpp | Replaces strcpy with memcpy in escape-substitution copying. |
| src/coreclr/interpreter/methodset.cpp | Copies config string via memcpy instead of strcpy. |
| src/coreclr/inc/outstring.h | Updates <string.h> include comment (no longer calls out strcpy). |
| src/coreclr/ildasm/dres.cpp | Replaces strcat/strcpy usage with manual memcpy for indentation/string building in resource dumping. |
| src/coreclr/ilasm/method.hpp | Updates commented-out code to reference strcpy_s. |
| src/coreclr/hosts/corerun/corerun.cpp | Uses length-based memcpy and guards malloc before copying core paths. |
| src/coreclr/gc/unix/cgroup.cpp | Uses mount-length caching + memcpy for initial copy and replaces a strcpy with memcpy. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| cgroupPathLength = parent_directory_end - mem_limit_filename; | ||
|
|
||
| strcpy(parent_directory_end, CGROUP2_MEMORY_LIMIT_FILENAME); | ||
| memcpy(parent_directory_end, CGROUP2_MEMORY_LIMIT_FILENAME, strlen(CGROUP2_MEMORY_LIMIT_FILENAME) + 1); |
There was a problem hiding this comment.
Do we gain anything here when the source is a string constant?
Follow up #130547.