[v2,5/6] stdlibc: Simplify realpath via scratch_buffer_dupfree

Message ID 20201228135944.1018303-5-adhemerval.zanella@linaro.org
State Superseded
Headers
Series None |

Commit Message

Adhemerval Zanella Netto Dec. 28, 2020, 1:59 p.m. UTC
  From: Paul Eggert <eggert@cs.ucla.edu>

Checked on x86_64-linux-gnu.
---
 stdlib/canonicalize.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
  

Patch

diff --git a/stdlib/canonicalize.c b/stdlib/canonicalize.c
index 24a027b83f..69f978da78 100644
--- a/stdlib/canonicalize.c
+++ b/stdlib/canonicalize.c
@@ -414,24 +414,14 @@  error:
 error_nomem:
   scratch_buffer_free (&extra_buffer);
   scratch_buffer_free (&link_buffer);
-  if (failed || rname == resolved)
-    scratch_buffer_free (rname_buf);
-
-  if (failed)
-    return NULL;
 
-  if (rname == resolved)
-    return rname;
-  idx_t rname_size = dest - rname;
-  if (rname == rname_on_stack)
+  if (failed || rname == resolved)
     {
-      rname = malloc (rname_size);
-      if (rname == NULL)
-        return NULL;
-      return memcpy (rname, rname_on_stack, rname_size);
+      scratch_buffer_free (rname_buf);
+      return failed ? NULL : resolved;
     }
-  char *result = realloc (rname, rname_size);
-  return result != NULL ? result : rname;
+
+  return scratch_buffer_dupfree (rname_buf, dest - rname);
 }
 
 /* Return the canonical absolute name of file NAME.  A canonical name