[newlib-cygwin] Cygwin: dladdr: use proper max size of dli_fname.

Takashi Yano tyan0@sourceware.org
Thu May 1 13:47:50 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=38772dd5a3c76ff109ac99391f53845b88f3d8ae

commit 38772dd5a3c76ff109ac99391f53845b88f3d8ae
Author: Jeremy Drake via Cygwin-patches <cygwin-patches@cygwin.com>
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 <cygwin@jdrake.com>

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;


More information about the Cygwin-cvs mailing list