Skip to content

Replace more strcpy with safe string copy - #131308

Open
am11 wants to merge 6 commits into
dotnet:mainfrom
am11:chore/mop-strcpy
Open

Replace more strcpy with safe string copy#131308
am11 wants to merge 6 commits into
dotnet:mainfrom
am11:chore/mop-strcpy

Conversation

@am11

@am11 am11 commented Jul 24, 2026

Copy link
Copy Markdown
Member

Follow up #130547.

@am11 am11 mentioned this pull request Jul 24, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 24, 2026
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

Comment thread src/coreclr/gc/unix/cgroup.cpp Outdated
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);

@jkotas jkotas Jul 24, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's same guarantees as before; none.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR title is "Replace more strcpy with safe string copy". Why is this safe string copy then?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strcpy -> memcpy/strcpy_s, because strcpy is being flagged by clang on OpenBSD warning: strcpy() is almost always misused, please use strlcpy().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how it is harder to read than original given #130547 is merged?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@am11
am11 force-pushed the chore/mop-strcpy branch from 6a2d59d to c0f7062 Compare July 24, 2026 11:29
@am11
am11 force-pushed the chore/mop-strcpy branch from c0f7062 to 7ea1219 Compare July 24, 2026 12:00
@am11
am11 requested a review from jkoritzinsky July 24, 2026 15:50
@am11

am11 commented Jul 24, 2026

Copy link
Copy Markdown
Member Author

BA failure is #131256.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 strcpy call sites with memcpy (often using strlen(...) + 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 strlen in allocation/copy sequences.
  • Added a couple of small robustness tweaks (e.g., guarding malloc results before copying in corerun.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.

Comment thread src/coreclr/utilcode/debug.cpp Outdated
Comment thread src/coreclr/ildasm/dres.cpp Outdated
Comment thread src/coreclr/interpreter/methodset.cpp
am11 and others added 2 commits August 13, 2026 10:07
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we gain anything here when the source is a string constant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-VM-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants