Better handling for realpath() failures in windows_make_so() on Cygwin

Message ID 20240321072349.1160-1-orgads@gmail.com
State New
Headers
Series Better handling for realpath() failures in windows_make_so() on Cygwin |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Orgad Shaneh March 21, 2024, 7:22 a.m. UTC
  From: Jon Turney <jon.turney@dronecode.org.uk>

Fix a memory leak which would occur in the case when the result of realpath() is
greater than or equal to SO_NAME_MAX_PATH_SIZE.

Distinguish between realpath() failing (returning NULL), and returning a path
longer than SO_NAME_MAX_PATH_SIZE

Warn rather than stopping with an error in those cases.

Original patch from Tim Chick.  Memory leak fix by Corinna Vinschen.
---
 gdb/windows-nat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Tom Tromey March 21, 2024, 3:04 p.m. UTC | #1
>>>>> "Orgad" == Orgad Shaneh <orgads@gmail.com> writes:

Orgad> From: Jon Turney <jon.turney@dronecode.org.uk>
Orgad> Fix a memory leak which would occur in the case when the result of realpath() is
Orgad> greater than or equal to SO_NAME_MAX_PATH_SIZE.

Thanks for the patch.

Orgad> +      if (rname)
Orgad> +	free (rname);

No need to check for NULL when calling free -- and gdb uses 'xfree'
anyway.

However, it would be better to change this code to use gdb_realpath,
which returns a unique pointer.  Then the memory leak will be
impossible.  Could you try that instead?

thanks,
Tom
  

Patch

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index a90388922e2..29bfad5b060 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -886,13 +886,14 @@  windows_make_so (const char *name, LPVOID load_addr)
       if (rname && strlen (rname) < SO_NAME_MAX_PATH_SIZE)
 	{
 	  so->name = rname;
-	  free (rname);
 	}
       else
 	{
 	  warning (_("dll path for \"%s\" too long or inaccessible"), name);
 	  so->name = so->original_name;
 	}
+      if (rname)
+	free (rname);
     }
   /* Record cygwin1.dll .text start/end.  */
   size_t len = sizeof ("/cygwin1.dll") - 1;