c5d972
commit 2bbd07c715275eb6c616988925738a0517180d57
c5d972
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
c5d972
Date:   Fri Dec 17 18:35:44 2021 +0530
c5d972
c5d972
    fortify: Fix spurious warning with realpath
c5d972
    
c5d972
    The length and object size arguments were swapped around for realpath.
c5d972
    Also add a smoke test so that any changes in this area get caught in
c5d972
    future.
c5d972
    
c5d972
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
c5d972
    Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
c5d972
c5d972
diff --git a/debug/Makefile b/debug/Makefile
c5d972
index 81361438fc3d2aa9..b43f42ee3851f360 100644
c5d972
--- a/debug/Makefile
c5d972
+++ b/debug/Makefile
c5d972
@@ -108,6 +108,7 @@ CFLAGS-tst-longjmp_chk2.c += -fexceptions -fasynchronous-unwind-tables
c5d972
 CPPFLAGS-tst-longjmp_chk2.c += -D_FORTIFY_SOURCE=1
c5d972
 CFLAGS-tst-longjmp_chk3.c += -fexceptions -fasynchronous-unwind-tables
c5d972
 CPPFLAGS-tst-longjmp_chk3.c += -D_FORTIFY_SOURCE=1
c5d972
+CPPFLAGS-tst-realpath-chk.c += -D_FORTIFY_SOURCE=2
c5d972
 
c5d972
 # We know these tests have problems with format strings, this is what
c5d972
 # we are testing.  Disable that warning.  They are also testing
c5d972
@@ -155,7 +156,7 @@ tests = backtrace-tst tst-longjmp_chk tst-chk1 tst-chk2 tst-chk3 \
c5d972
 	tst-lfschk1 tst-lfschk2 tst-lfschk3 test-strcpy_chk test-stpcpy_chk \
c5d972
 	tst-chk4 tst-chk5 tst-chk6 tst-chk7 tst-chk8 tst-lfschk4 tst-lfschk5 \
c5d972
 	tst-lfschk6 tst-longjmp_chk2 tst-backtrace2 tst-backtrace3 \
c5d972
-	tst-backtrace4 tst-backtrace5 tst-backtrace6
c5d972
+	tst-backtrace4 tst-backtrace5 tst-backtrace6 tst-realpath-chk
c5d972
 
c5d972
 ifeq ($(have-ssp),yes)
c5d972
 tests += tst-ssp-1
c5d972
diff --git a/debug/tst-realpath-chk.c b/debug/tst-realpath-chk.c
c5d972
new file mode 100644
c5d972
index 0000000000000000..a8fcb327c43fb34d
c5d972
--- /dev/null
c5d972
+++ b/debug/tst-realpath-chk.c
c5d972
@@ -0,0 +1,37 @@
c5d972
+/* Smoke test to verify that realpath does not cause spurious warnings.
c5d972
+   Copyright The GNU Toolchain Authors.
c5d972
+   This file is part of the GNU C Library.
c5d972
+
c5d972
+   The GNU C Library is free software; you can redistribute it and/or
c5d972
+   modify it under the terms of the GNU Lesser General Public
c5d972
+   License as published by the Free Software Foundation; either
c5d972
+   version 2.1 of the License, or (at your option) any later version.
c5d972
+
c5d972
+   The GNU C Library is distributed in the hope that it will be useful,
c5d972
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
c5d972
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
c5d972
+   Lesser General Public License for more details.
c5d972
+
c5d972
+   You should have received a copy of the GNU Lesser General Public
c5d972
+   License along with the GNU C Library; if not, see
c5d972
+   <https://www.gnu.org/licenses/>.  */
c5d972
+
c5d972
+#include <limits.h>
c5d972
+#include <stdlib.h>
c5d972
+
c5d972
+#include <support/check.h>
c5d972
+#include <support/support.h>
c5d972
+
c5d972
+static int
c5d972
+do_test (void)
c5d972
+{
c5d972
+#ifdef PATH_MAX
c5d972
+  char buf[PATH_MAX + 1];
c5d972
+  char *res = realpath (".", buf);
c5d972
+  TEST_VERIFY (res == buf);
c5d972
+#endif
c5d972
+
c5d972
+  return 0;
c5d972
+}
c5d972
+
c5d972
+#include <support/test-driver.c>
c5d972
diff --git a/stdlib/bits/stdlib.h b/stdlib/bits/stdlib.h
c5d972
index 7ea364a276497720..81ec9bdb32215e3b 100644
c5d972
--- a/stdlib/bits/stdlib.h
c5d972
+++ b/stdlib/bits/stdlib.h
c5d972
@@ -42,7 +42,7 @@ __NTH (realpath (const char *__restrict __name, char *__restrict __resolved))
c5d972
     return __realpath_alias (__name, __resolved);
c5d972
 
c5d972
 #if defined _LIBC_LIMITS_H_ && defined PATH_MAX
c5d972
-  if (__glibc_unsafe_len (sz, sizeof (char), PATH_MAX))
c5d972
+  if (__glibc_unsafe_len (PATH_MAX, sizeof (char), sz))
c5d972
     return __realpath_chk_warn (__name, __resolved, sz);
c5d972
 #endif
c5d972
   return __realpath_chk (__name, __resolved, sz);