[libunwind] [libunwind] Remove unnecessary strcpy dependency (PR #72043)

Michael Kenzel via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 12 15:12:12 PST 2023


================
@@ -143,7 +143,7 @@ _Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) {
   // Create a mock exception object for force unwinding.
   _Unwind_Exception ex;
   memset(&ex, '\0', sizeof(ex));
-  strcpy((char *)&ex.exception_class, "CLNGUNW");
+  memcpy(&ex.exception_class, "CLNGUNW", sizeof(ex.exception_class));
----------------
michael-kenzel wrote:

My reasoning was that if the string happened to be too long for some reason, we'd want to ensure that we don't write past the member. Either option, using the string length or member length, has the downside of not considering the other length. Ideally, the string would be some constant and we'd have a `_Static_assert` to ensure the sizes match. But I couldn't find a simple way to put this into plain C without making this code significantly more complex. I was also not quite sure what's the range of versions of C this is supposed to build under, which would have implications regarding whether `_Static_assert` can be used at all or is deprecated in favor of `static_assert`… One very simple solution (at least code-wise) would be to just do
```c
  _Unwind_Exception ex = {
    .exception_class = 0x574E55474E4C43  // "CLNGUNW"
  };
```
But this then assumes little endian and so would be a potential ABI break?

Overall, I think it's reasonable to assume that this string isn't just gonna change in a way that would make it not match the size of the member anymore. The current implementation was already broken in that case as well. Unfortunately, no compiler seems to issue a warning if you make the string not match the size of the member.

https://github.com/llvm/llvm-project/pull/72043


More information about the cfe-commits mailing list