about summary refs log tree commit diff
path: root/sysdeps/x86_64/x32/tst-size_t-strncat.c
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2022-12-01 16:37:11 -0800
committerH.J. Lu <hjl.tools@gmail.com>2022-12-02 08:18:10 -0800
commitf566b028524149ddfebe7f9770a3befb13b81a13 (patch)
tree7ddae400809cd9e2902af7fcd59e3bf22597809c /sysdeps/x86_64/x32/tst-size_t-strncat.c
parent6ae0737d430900fff02569548a92bdca49622a15 (diff)
downloadglibc-f566b028524149ddfebe7f9770a3befb13b81a13.tar.gz
glibc-f566b028524149ddfebe7f9770a3befb13b81a13.tar.xz
glibc-f566b028524149ddfebe7f9770a3befb13b81a13.zip
x86-64 strncat: Properly handle the length parameter [BZ# 24097]
On x32, the size_t parameter may be passed in the lower 32 bits of a
64-bit register with the non-zero upper 32 bits.  The string/memory
functions written in assembly can only use the lower 32 bits of a
64-bit register as length or must clear the upper 32 bits before using
the full 64-bit register for length.

This pach fixes strncat for x32.  Tested on x86-64 and x32.  On x86-64,
libc.so is the same with and without the fix.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
Diffstat (limited to 'sysdeps/x86_64/x32/tst-size_t-strncat.c')
-rw-r--r--sysdeps/x86_64/x32/tst-size_t-strncat.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/sysdeps/x86_64/x32/tst-size_t-strncat.c b/sysdeps/x86_64/x32/tst-size_t-strncat.c
new file mode 100644
index 0000000000..eda93f5af9
--- /dev/null
+++ b/sysdeps/x86_64/x32/tst-size_t-strncat.c
@@ -0,0 +1,59 @@
+/* Test strncat with size_t in the lower 32 bits of 64-bit register.
+   Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#define TEST_NAME "strncat"
+#include "test-size_t.h"
+
+IMPL (strncat, 1)
+
+typedef char *(*proto_t) (char *, const char*, size_t);
+
+static void *
+__attribute__ ((noinline, noclone))
+do_strncat (parameter_t a, parameter_t b)
+{
+  return CALL (&b, a.p, b.p, a.len);
+}
+
+static int
+test_main (void)
+{
+  test_init ();
+
+  parameter_t dest = { { page_size - 1 }, buf1 };
+  parameter_t src = { { 0 }, buf2 };
+
+  int ret = 0;
+  FOR_EACH_IMPL (impl, 0)
+    {
+      src.fn = impl->fn;
+      buf1[0] = '\0';
+      do_strncat (dest, src);
+      int res = strncmp (dest.p, src.p, dest.len);
+      if (res)
+	{
+	  error (0, 0, "Wrong result in function %s: %i != 0",
+		 impl->name, res);
+	  ret = 1;
+	}
+    }
+
+  return ret ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
+#include <support/test-driver.c>