94084c
commit d084965adc7baa8ea804427cccf973cea556d697
94084c
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
94084c
Date:   Mon Jan 24 21:36:41 2022 +0530
94084c
94084c
    realpath: Avoid overwriting preexisting error (CVE-2021-3998)
94084c
    
94084c
    Set errno and failure for paths that are too long only if no other error
94084c
    occurred earlier.
94084c
    
94084c
    Related: BZ #28770
94084c
    
94084c
    Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
94084c
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
94084c
    (cherry picked from commit 84d2d0fe20bdf94feed82b21b4d7d136db471f03)
94084c
94084c
diff --git a/stdlib/canonicalize.c b/stdlib/canonicalize.c
94084c
index 7a23a51b3a395eb3..e2d4244fc7b8fa25 100644
94084c
--- a/stdlib/canonicalize.c
94084c
+++ b/stdlib/canonicalize.c
94084c
@@ -404,7 +404,7 @@ error:
94084c
     {
94084c
       if (dest - rname <= get_path_max ())
94084c
 	rname = strcpy (resolved, rname);
94084c
-      else
94084c
+      else if (!failed)
94084c
 	{
94084c
 	  failed = true;
94084c
 	  __set_errno (ENAMETOOLONG);