[PATCH] D146382: Change strcpy to strncpy

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 19 08:11:04 PDT 2023


nikic requested changes to this revision.
nikic added inline comments.
This revision now requires changes to proceed.


================
Comment at: llvm/lib/Object/COFFImportFile.cpp:89
     B.resize(Pos + S.length() + 1);
-    strcpy(reinterpret_cast<char *>(&B[Pos]), S.c_str());
+    strncpy(reinterpret_cast<char *>(&B[Pos]), S.c_str(), S.size());
     Pos += S.length() + 1;
----------------
This isn't really a sensible way to use strncpy -- in fact, I'm pretty sure that this is buggy and should be `S.size() + 1`, otherwise we're not copying the trailing null byte, which is important in this context.

Explicitly specifying the length does make sense (as we already know it here), but in that case we should just use memcpy, or, to make it more idiomatic for C++, std::copy.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146382/new/

https://reviews.llvm.org/D146382



More information about the llvm-commits mailing list