Blame SOURCES/0014-common-include-Add-ASCII-only-ctype-header-and-ascii.patch

60545c
From cb3d83d0606d5267752895151bb3c229c48d6fb6 Mon Sep 17 00:00:00 2001
60545c
From: "Richard W.M. Jones" <rjones@redhat.com>
60545c
Date: Tue, 19 May 2020 12:03:23 +0100
60545c
Subject: [PATCH 14/19] common/include: Add ASCII-only ctype header and
60545c
 ascii_is* functions.
60545c
60545c
Our existing uses of <ctype.h> were not necessarily safe if the locale
60545c
was changed.
60545c
60545c
Also I removed the unnecessary use of isascii, deprecated by POSIX.1-2008.
60545c
60545c
(cherry picked from commit 9f34db74786fdc92b290a7d47e4b003bd84fec69)
60545c
---
60545c
 .gitignore                           |  1 +
60545c
 common/include/Makefile.am           |  6 +++
60545c
 common/include/ascii-ctype.h         | 60 ++++++++++++++++++++++++++
60545c
 common/include/test-ascii-ctype.c    | 63 ++++++++++++++++++++++++++++
60545c
 plugins/partitioning/partition-gpt.c | 12 +++---
60545c
 plugins/sh/Makefile.am               |  1 +
60545c
 plugins/sh/call.c                    |  6 +--
60545c
 server/backend.c                     |  7 ++--
60545c
 server/public.c                      |  4 +-
60545c
 9 files changed, 146 insertions(+), 14 deletions(-)
60545c
 create mode 100644 common/include/ascii-ctype.h
60545c
 create mode 100644 common/include/test-ascii-ctype.c
60545c
60545c
diff --git a/.gitignore b/.gitignore
60545c
index e25bd99b..523894b7 100644
60545c
--- a/.gitignore
60545c
+++ b/.gitignore
60545c
@@ -26,6 +26,7 @@ Makefile.in
60545c
 /aclocal.m4
60545c
 /autom4te.cache
60545c
 /common/bitmap/test-bitmap
60545c
+/common/include/test-ascii-ctype
60545c
 /common/include/test-byte-swapping
60545c
 /common/include/test-current-dir-name
60545c
 /common/include/test-isaligned
60545c
diff --git a/common/include/Makefile.am b/common/include/Makefile.am
60545c
index 4482de37..d7b0d7a8 100644
60545c
--- a/common/include/Makefile.am
60545c
+++ b/common/include/Makefile.am
60545c
@@ -34,6 +34,7 @@ include $(top_srcdir)/common-rules.mk
60545c
 # These headers contain only common code shared by the core server,
60545c
 # plugins and/or filters.  They are not installed.
60545c
 EXTRA_DIST = \
60545c
+	ascii-ctype.h \
60545c
 	byte-swapping.h \
60545c
 	exit-with-parent.h \
60545c
 	get-current-dir-name.h \
60545c
@@ -50,6 +51,7 @@ EXTRA_DIST = \
60545c
 # Unit tests.
60545c
 
60545c
 TESTS = \
60545c
+	test-ascii-ctype \
60545c
 	test-byte-swapping \
60545c
 	test-current-dir-name \
60545c
 	test-isaligned \
60545c
@@ -62,6 +64,10 @@ TESTS = \
60545c
 	$(NULL)
60545c
 check_PROGRAMS = $(TESTS)
60545c
 
60545c
+test_ascii_ctype_SOURCES = test-ascii-ctype.c ascii-ctype.h
60545c
+test_ascii_ctype_CPPFLAGS = -I$(srcdir)
60545c
+test_ascii_ctype_CFLAGS = $(WARNINGS_CFLAGS)
60545c
+
60545c
 test_byte_swapping_SOURCES = test-byte-swapping.c byte-swapping.h
60545c
 test_byte_swapping_CPPFLAGS = -I$(srcdir)
60545c
 test_byte_swapping_CFLAGS = $(WARNINGS_CFLAGS)
60545c
diff --git a/common/include/ascii-ctype.h b/common/include/ascii-ctype.h
60545c
new file mode 100644
60545c
index 00000000..5e8bf237
60545c
--- /dev/null
60545c
+++ b/common/include/ascii-ctype.h
60545c
@@ -0,0 +1,60 @@
60545c
+/* nbdkit
60545c
+ * Copyright (C) 2013-2020 Red Hat Inc.
60545c
+ *
60545c
+ * Redistribution and use in source and binary forms, with or without
60545c
+ * modification, are permitted provided that the following conditions are
60545c
+ * met:
60545c
+ *
60545c
+ * * Redistributions of source code must retain the above copyright
60545c
+ * notice, this list of conditions and the following disclaimer.
60545c
+ *
60545c
+ * * Redistributions in binary form must reproduce the above copyright
60545c
+ * notice, this list of conditions and the following disclaimer in the
60545c
+ * documentation and/or other materials provided with the distribution.
60545c
+ *
60545c
+ * * Neither the name of Red Hat nor the names of its contributors may be
60545c
+ * used to endorse or promote products derived from this software without
60545c
+ * specific prior written permission.
60545c
+ *
60545c
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
60545c
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
60545c
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
60545c
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
60545c
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
60545c
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60545c
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
60545c
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
60545c
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
60545c
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
60545c
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60545c
+ * SUCH DAMAGE.
60545c
+ */
60545c
+
60545c
+/* Normal ctype functions are affected by the current locale.  For
60545c
+ * example isupper() might recognize Ä in some but not all locales.
60545c
+ * These functions match only 7 bit ASCII characters.
60545c
+ */
60545c
+
60545c
+#ifndef NBDKIT_ASCII_CTYPE_H
60545c
+#define NBDKIT_ASCII_CTYPE_H
60545c
+
60545c
+#define ascii_isalnum(c) (ascii_isalpha (c) || ascii_isdigit (c))
60545c
+
60545c
+#define ascii_isalpha(c)                                        \
60545c
+  (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
60545c
+
60545c
+#define ascii_isdigit(c)                        \
60545c
+  ((c) >= '0' && (c) <= '9')
60545c
+
60545c
+#define ascii_isspace(c)                                                \
60545c
+  ((c) == '\t' || (c) == '\n' || (c) == '\f' || (c) == '\r' || (c) == ' ')
60545c
+
60545c
+#define ascii_isxdigit(c)                                               \
60545c
+  ((c) == '0' || (c) == '1' || (c) == '2' || (c) == '3' || (c) == '4' || \
60545c
+   (c) == '5' || (c) == '6' || (c) == '7' || (c) == '8' || (c) == '9' || \
60545c
+   (c) == 'a' || (c) == 'b' || (c) == 'c' ||                            \
60545c
+   (c) == 'd' || (c) == 'e' || (c) == 'f' ||                            \
60545c
+   (c) == 'A' || (c) == 'B' || (c) == 'C' ||                            \
60545c
+   (c) == 'D' || (c) == 'E' || (c) == 'F')
60545c
+
60545c
+#endif /* NBDKIT_ASCII_CTYPE_H */
60545c
diff --git a/common/include/test-ascii-ctype.c b/common/include/test-ascii-ctype.c
60545c
new file mode 100644
60545c
index 00000000..edf27aa6
60545c
--- /dev/null
60545c
+++ b/common/include/test-ascii-ctype.c
60545c
@@ -0,0 +1,63 @@
60545c
+/* nbdkit
60545c
+ * Copyright (C) 2020 Red Hat Inc.
60545c
+ *
60545c
+ * Redistribution and use in source and binary forms, with or without
60545c
+ * modification, are permitted provided that the following conditions are
60545c
+ * met:
60545c
+ *
60545c
+ * * Redistributions of source code must retain the above copyright
60545c
+ * notice, this list of conditions and the following disclaimer.
60545c
+ *
60545c
+ * * Redistributions in binary form must reproduce the above copyright
60545c
+ * notice, this list of conditions and the following disclaimer in the
60545c
+ * documentation and/or other materials provided with the distribution.
60545c
+ *
60545c
+ * * Neither the name of Red Hat nor the names of its contributors may be
60545c
+ * used to endorse or promote products derived from this software without
60545c
+ * specific prior written permission.
60545c
+ *
60545c
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
60545c
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
60545c
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
60545c
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
60545c
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
60545c
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60545c
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
60545c
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
60545c
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
60545c
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
60545c
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60545c
+ * SUCH DAMAGE.
60545c
+ */
60545c
+
60545c
+#include <config.h>
60545c
+
60545c
+#include <stdio.h>
60545c
+#include <stdlib.h>
60545c
+#include <assert.h>
60545c
+
60545c
+#include "ascii-ctype.h"
60545c
+
60545c
+int
60545c
+main (void)
60545c
+{
60545c
+  assert (ascii_isspace (' '));
60545c
+  assert (ascii_isspace ('\t'));
60545c
+  assert (ascii_isspace ('\n'));
60545c
+  assert (! ascii_isspace ('a'));
60545c
+
60545c
+  assert (ascii_isalpha ('a'));
60545c
+  assert (ascii_isalpha ('Z'));
60545c
+  assert (ascii_isalpha ('z'));
60545c
+  assert (! ascii_isalpha (' '));
60545c
+  assert (! ascii_isalpha ('0'));
60545c
+  { const char *s = "Ä"; assert (! ascii_isalpha (s[0])); }
60545c
+  { const char *s = "®"; assert (! ascii_isalpha (s[0])); }
60545c
+
60545c
+  assert (ascii_isdigit ('0'));
60545c
+  assert (ascii_isdigit ('9'));
60545c
+  { const char *s = "Ø"; assert (! ascii_isdigit (s[0])); } /* U+00D8 */
60545c
+  { const char *s = "9"; assert (! ascii_isdigit (s[0])); } /* U+FF19 */
60545c
+
60545c
+  exit (EXIT_SUCCESS);
60545c
+}
60545c
diff --git a/plugins/partitioning/partition-gpt.c b/plugins/partitioning/partition-gpt.c
60545c
index 75b4643a..819e9abe 100644
60545c
--- a/plugins/partitioning/partition-gpt.c
60545c
+++ b/plugins/partitioning/partition-gpt.c
60545c
@@ -36,12 +36,12 @@
60545c
 #include <stdlib.h>
60545c
 #include <stdbool.h>
60545c
 #include <string.h>
60545c
-#include <ctype.h>
60545c
 #include <inttypes.h>
60545c
 #include <assert.h>
60545c
 
60545c
 #include <nbdkit-plugin.h>
60545c
 
60545c
+#include "ascii-ctype.h"
60545c
 #include "byte-swapping.h"
60545c
 
60545c
 #include "efi-crc32.h"
60545c
@@ -244,19 +244,19 @@ parse_guid (const char *str, char *out)
60545c
     return -1;
60545c
 
60545c
   for (i = 0; i < 8; ++i)
60545c
-    if (!isxdigit (str[i]))
60545c
+    if (!ascii_isxdigit (str[i]))
60545c
       return -1;
60545c
   for (i = 9; i < 13; ++i)
60545c
-    if (!isxdigit (str[i]))
60545c
+    if (!ascii_isxdigit (str[i]))
60545c
       return -1;
60545c
   for (i = 14; i < 18; ++i)
60545c
-    if (!isxdigit (str[i]))
60545c
+    if (!ascii_isxdigit (str[i]))
60545c
       return -1;
60545c
   for (i = 19; i < 23; ++i)
60545c
-    if (!isxdigit (str[i]))
60545c
+    if (!ascii_isxdigit (str[i]))
60545c
       return -1;
60545c
   for (i = 24; i < 36; ++i)
60545c
-    if (!isxdigit (str[i]))
60545c
+    if (!ascii_isxdigit (str[i]))
60545c
       return -1;
60545c
 
60545c
   /* The first, second and third blocks are parsed as little endian,
60545c
diff --git a/plugins/sh/Makefile.am b/plugins/sh/Makefile.am
60545c
index 445cdcd5..1f42b64c 100644
60545c
--- a/plugins/sh/Makefile.am
60545c
+++ b/plugins/sh/Makefile.am
60545c
@@ -48,6 +48,7 @@ nbdkit_sh_plugin_la_SOURCES = \
60545c
 
60545c
 nbdkit_sh_plugin_la_CPPFLAGS = \
60545c
 	-I$(top_srcdir)/include \
60545c
+	-I$(top_srcdir)/common/include \
60545c
 	-I$(top_srcdir)/common/utils \
60545c
 	$(NULL)
60545c
 nbdkit_sh_plugin_la_CFLAGS = $(WARNINGS_CFLAGS)
60545c
diff --git a/plugins/sh/call.c b/plugins/sh/call.c
60545c
index 2d99a120..ae0cc0ac 100644
60545c
--- a/plugins/sh/call.c
60545c
+++ b/plugins/sh/call.c
60545c
@@ -44,10 +44,10 @@
60545c
 #include <poll.h>
60545c
 #include <sys/types.h>
60545c
 #include <sys/wait.h>
60545c
-#include <ctype.h>
60545c
 
60545c
 #include <nbdkit-plugin.h>
60545c
 
60545c
+#include "ascii-ctype.h"
60545c
 #include "cleanup.h"
60545c
 #include "utils.h"
60545c
 
60545c
@@ -392,7 +392,7 @@ handle_script_error (const char *argv0, char *ebuf, size_t len)
60545c
   }
60545c
 
60545c
   if (skip && ebuf[skip]) {
60545c
-    if (!isspace ((unsigned char) ebuf[skip])) {
60545c
+    if (!ascii_isspace ((unsigned char) ebuf[skip])) {
60545c
       /* Treat 'EINVALID' as EIO, not EINVAL */
60545c
       err = EIO;
60545c
       skip = 0;
60545c
@@ -400,7 +400,7 @@ handle_script_error (const char *argv0, char *ebuf, size_t len)
60545c
     else
60545c
       do
60545c
         skip++;
60545c
-      while (isspace ((unsigned char) ebuf[skip]));
60545c
+      while (ascii_isspace ((unsigned char) ebuf[skip]));
60545c
   }
60545c
 
60545c
   while (len > 0 && ebuf[len-1] == '\n')
60545c
diff --git a/server/backend.c b/server/backend.c
60545c
index 208c07b1..9add341f 100644
60545c
--- a/server/backend.c
60545c
+++ b/server/backend.c
60545c
@@ -37,13 +37,14 @@
60545c
 #include <inttypes.h>
60545c
 #include <string.h>
60545c
 #include <assert.h>
60545c
-#include <ctype.h>
60545c
 
60545c
 #include <dlfcn.h>
60545c
 
60545c
-#include "internal.h"
60545c
+#include "ascii-ctype.h"
60545c
 #include "minmax.h"
60545c
 
60545c
+#include "internal.h"
60545c
+
60545c
 /* Helpers for registering a new backend. */
60545c
 
60545c
 /* Use:
60545c
@@ -100,7 +101,7 @@ backend_load (struct backend *b, const char *name, void (*load) (void))
60545c
   for (i = 0; i < len; ++i) {
60545c
     unsigned char c = name[i];
60545c
 
60545c
-    if (!(isascii (c) && isalnum (c))) {
60545c
+    if (! ascii_isalnum (c)) {
60545c
       fprintf (stderr,
60545c
                "%s: %s: %s.name ('%s') field "
60545c
                "must contain only ASCII alphanumeric characters\n",
60545c
diff --git a/server/public.c b/server/public.c
60545c
index 418945fe..98b78482 100644
60545c
--- a/server/public.c
60545c
+++ b/server/public.c
60545c
@@ -45,13 +45,13 @@
60545c
 #include <string.h>
60545c
 #include <unistd.h>
60545c
 #include <limits.h>
60545c
-#include <ctype.h>
60545c
 #include <termios.h>
60545c
 #include <errno.h>
60545c
 #include <poll.h>
60545c
 #include <signal.h>
60545c
 #include <sys/socket.h>
60545c
 
60545c
+#include "ascii-ctype.h"
60545c
 #include "get-current-dir-name.h"
60545c
 
60545c
 #include "internal.h"
60545c
@@ -210,7 +210,7 @@ nbdkit_parse_int64_t (const char *what, const char *str, int64_t *rp)
60545c
  */
60545c
 #define PARSE_ERROR_IF_NEGATIVE                                         \
60545c
   do {                                                                  \
60545c
-    while (isspace (*str))                                              \
60545c
+    while (ascii_isspace (*str))                                        \
60545c
       str++;                                                            \
60545c
     if (*str == '-') {                                                  \
60545c
       nbdkit_error ("%s: negative numbers are not allowed", what);      \
60545c
-- 
60545c
2.18.2
60545c