diff mbox series

[2/2] stdio: add test for scanf matches longer than INT_MAX (bug 27650)

Message ID 20210329180640.18722-1-hi@alyssa.is
State New
Headers show
Series stdio: fix vfscanf with matches longer than INT_MAX (bug 27650) | expand

Commit Message

Alyssa Ross March 29, 2021, 6:06 p.m. UTC
This doesn't test the wchar_t variants.  Won't run on s390 due to
using more than INT_MAX bytes of address space.
---
Applying this will need to wait until I have my copyright paperwork
sorted.

 stdio-common/Makefile             |  3 ++-
 stdio-common/tst-sscanf-bz27650.c | 42 +++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 stdio-common/tst-sscanf-bz27650.c
diff mbox series

Patch

diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index b2458ba4a6..281a154f5b 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -70,7 +70,8 @@  tests := tstscanf test_rdwr test-popen tstgetln test-fseek \
 	 tst-vfprintf-width-prec-alloc \
 	 tst-printf-fp-free \
 	 tst-printf-fp-leak \
-	 test-strerr
+	 test-strerr \
+	 tst-sscanf-bz27650
 
 
 test-srcs = tst-unbputc tst-printf tst-printfsz-islongdouble
diff --git a/stdio-common/tst-sscanf-bz27650.c b/stdio-common/tst-sscanf-bz27650.c
new file mode 100644
index 0000000000..4636d26323
--- /dev/null
+++ b/stdio-common/tst-sscanf-bz27650.c
@@ -0,0 +1,42 @@ 
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <support/blob_repeat.h>
+#include <support/check.h>
+
+/* Test that vfscanf can handle matches longer than INT_MAX bytes. */
+static int
+do_test (void)
+{
+  char next_char;
+  struct support_blob_repeat blob =
+    support_blob_repeat_allocate ("a", 1, INT_MAX + 3U);
+  char *s = blob.start;
+
+  if (s == NULL)
+    FAIL_UNSUPPORTED ("repeated blob allocation failed: %m");
+
+  s[INT_MAX + 1U] = 'b';
+  s[INT_MAX + 2U] = '\0';
+
+  errno = 0;
+  switch (sscanf (s, "%*[^b]%c", &next_char))
+    {
+    case EOF:
+      if (errno)
+	FAIL_EXIT1 ("sscanf: %m");
+      FAIL_EXIT1 ("EOF reached and no 'b' found");
+    case 0:
+      FAIL_EXIT1 ("pattern didn't match");
+    }
+
+  TEST_COMPARE (next_char, 'b');
+
+  return 0;
+}
+
+#include <support/test-driver.c>