Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Message-Id: <ae4f840bb48367d0a9aabeb5f12102caef2c71e9.1685536319.git.Jens.Gustedt@inria.fr>
Date: Wed, 31 May 2023 16:05:43 +0200
From: Jens Gustedt <Jens.Gustedt@...ia.fr>
To: musl@...ts.openwall.com
Subject: [C23 scanf 3/3] C23: implement wN and wfN specifiers for scanf functions

---
 src/stdio/vfscanf.c  | 30 ++++++++++++++++++++++++++++++
 src/stdio/vfwscanf.c | 30 ++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/src/stdio/vfscanf.c b/src/stdio/vfscanf.c
index f72ac9c9..f8ace92f 100644
--- a/src/stdio/vfscanf.c
+++ b/src/stdio/vfscanf.c
@@ -41,6 +41,15 @@ static void store_int(void *dest, int size, unsigned long long i)
 	}
 }
 
+static int getint(unsigned char const**s) {
+	int i=0;
+	if (**s != '0')	for (; isdigit(**s); (*s)++) {
+		if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
+		else i = 10*i + (**s-'0');
+	}
+	return i;
+}
+
 static void *arg_n(va_list ap, unsigned int n)
 {
 	void *p;
@@ -57,6 +66,7 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
 {
 	int width;
 	int size;
+	int fast16;
 	int alloc = 0;
 	int base;
 	const unsigned char *p;
@@ -150,6 +160,26 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
 		case 'L':
 			size = SIZE_L;
 			break;
+		case 'w':
+			// See if "fast" is requested. Difference is only
+			// relevant for a fast type of minimum width 16.
+			fast16 = SIZE_h;
+			if (*p == 'f') {
+				fast16 = SIZE_def;
+				++p;
+			}
+			switch (getint(&p)) {
+			default: goto fmt_fail;
+			case 8:  size = SIZE_hh; break;
+			case 16: size = fast16; break;
+			case 32: size = SIZE_def; break;
+#if UINTPTR_MAX >= UINT64_MAX
+			case 64: size = SIZE_l; break;
+#else
+			case 64: size = SIZE_ll; break;
+#endif
+			}
+			break;
 		case 'b': case 'd': case 'i': case 'o': case 'u': case 'x':
 		case 'a': case 'e': case 'f': case 'g':
 		case 'A': case 'E': case 'F': case 'G': case 'X':
diff --git a/src/stdio/vfwscanf.c b/src/stdio/vfwscanf.c
index 17f5a2f9..6fe2749c 100644
--- a/src/stdio/vfwscanf.c
+++ b/src/stdio/vfwscanf.c
@@ -41,6 +41,15 @@ static void store_int(void *dest, int size, unsigned long long i)
 	}
 }
 
+static int getint(wchar_t const**s) {
+	int i=0;
+	if (**s != L'0') for (i=0; iswdigit(**s); (*s)++) {
+		if (i > INT_MAX/10U || **s-L'0' > INT_MAX-10*i) i = -1;
+		else i = 10*i + (**s-L'0');
+	}
+	return i;
+}
+
 static void *arg_n(va_list ap, unsigned int n)
 {
 	void *p;
@@ -87,6 +96,7 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
 {
 	int width;
 	int size;
+	int fast16;
 	int alloc;
 	const wchar_t *p;
 	int c, t;
@@ -173,6 +183,26 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
 		case 'L':
 			size = SIZE_L;
 			break;
+		case 'w':
+			// See if "fast" is requested. Difference is only
+			// relevant for a fast type of minimum width 16.
+			fast16 = SIZE_h;
+			if (*p == 'f') {
+				fast16 = SIZE_def;
+				++p;
+			}
+			switch (getint(&p)) {
+			default: goto fmt_fail;
+			case 8:  size = SIZE_hh; break;
+			case 16: size = fast16; break;
+			case 32: size = SIZE_def; break;
+#if UINTPTR_MAX >= UINT64_MAX
+			case 64: size = SIZE_l; break;
+#else
+			case 64: size = SIZE_ll; break;
+#endif
+			}
+			break;
 		case 'b': case 'd': case 'i': case 'o': case 'u': case 'x':
 		case 'a': case 'e': case 'f': case 'g':
 		case 'A': case 'E': case 'F': case 'G': case 'X':
-- 
2.34.1

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.