在 2022-01-16 22:44, Pali Rohár 写道:
diff --git a/mingw-w64-crt/stdio/scanf2-argcount-template.c 
b/mingw-w64-crt/stdio/scanf2-argcount-template.c
new file mode 100644
index 000000000000..c07e11797c39
--- /dev/null
+++ b/mingw-w64-crt/stdio/scanf2-argcount-template.c
@@ -0,0 +1,22 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#include <stddef.h>
+
+size_t FUNC(const TYPE *format);
+size_t FUNC(const TYPE *format)
+{
+  size_t count = 0;
+  for (; *format; format++) {
+    if (*format != (TYPE)'%')
+      continue;
+    format++;
+    if (*format == (TYPE)'%' || *format == (TYPE)'*')
+      continue;
+    count++;
+  }
+  return count;
+}


I am afraid there are two issues about this function:

One is that it may read pass the end of the format string if it contains a stray `%` at the end (which however is undefined behavior) but I don't think it is a good idea to not handle it properly.

The other is that `%` does not necessarily start an argument specifier. For example `"abc%[01%]"` contains only one specifier for one argument, and this function produces a wrong result.


--
Best regards,
LIU Hao

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to