[newlib-cygwin/cygwin-3_6-branch] Cygwin: dladdr: use proper max size of dli_fname.

Takashi Yano via Cygwin-cvs <[email protected]>
Newsgroups gmane.os.cygwin.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=2b98cc8877f9f30ce85d975374168e5552ba7854

commit 2b98cc8877f9f30ce85d975374168e5552ba7854
Author: Jeremy Drake via Cygwin-patches <[email protected]>
Date:   Wed Apr 30 12:45:56 2025 -0700

    Cygwin: dladdr: use proper max size of dli_fname.
    
    The DL_info::dli_fname member is actually PATH_MAX bytes, so specify
    that (larger) size to cygwin_conv_path rather than MAX_PATH.
    
    Also, use a tmp_pathbuf for the GetModuleFileNameW buffer, so that any
    buffer size limitation will definitely be due to the size of dli_fname,
    and add a static_assert of the size of dli_fname so we can be sure we're
    using the right size constant here.
    
    Fixes: c8432a01c840 ("Implement dladdr() (partially)")
    Addresses: https://github.com/rust-lang/backtrace-rs/pull/704#issuecomment-2833782574
    Signed-off-by: Jeremy Drake <[email protected]>
    (cherry picked from commit 38772dd5a3c76ff109ac99391f53845b88f3d8ae)

Diff:
---
 winsup/cygwin/dlfcn.cc | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/dlfcn.cc b/winsup/cygwin/dlfcn.cc
index 10bd0ac1f..9b6bb55b3 100644
--- a/winsup/cygwin/dlfcn.cc
+++ b/winsup/cygwin/dlfcn.cc
@@ -421,14 +421,16 @@ dladdr (const void *addr, Dl_info *info)
   /* Get the module filename.  This pathname may be in short-, long- or //?/
      format, depending on how it was specified when loaded, but we assume this
      is always an absolute pathname. */
-  WCHAR fname[MAX_PATH];
-  DWORD length = GetModuleFileNameW (hModule, fname, MAX_PATH);
-  if ((length == 0) || (length == MAX_PATH))
+  tmp_pathbuf tp;
+  PWCHAR fname = tp.w_get ();
+  DWORD length = GetModuleFileNameW (hModule, fname, NT_MAX_PATH);
+  if ((length == 0) || (length == NT_MAX_PATH))
     return 0;
 
   /* Convert to a cygwin pathname */
+  static_assert (sizeof (info->dli_fname) == PATH_MAX);
   ssize_t conv = cygwin_conv_path (CCP_WIN_W_TO_POSIX | CCP_ABSOLUTE, fname,
-				   info->dli_fname, MAX_PATH);
+				   info->dli_fname, PATH_MAX);
   if (conv)
     return 0;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.