summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Johnson <ajohnson@redneon.com>2022-04-09 21:54:38 +0930
committerAdrian Johnson <ajohnson@redneon.com>2022-04-09 22:08:57 +0930
commit5b18aeffbbd1641962f2ae374a3cb773fe7a6d6e (patch)
tree372b56c4369e8d49856ac74e98c17a716cb6efc3
parent20a54465c597b4f90ab35d73cab25b6e89dfa9bb (diff)
downloadcairo-5b18aeffbbd1641962f2ae374a3cb773fe7a6d6e.tar.gz
Replace use of ctype functions with internal version where only ASCII chars are used
In !309 Taylor R Campbell found a number of instances of ctype incorrectly passed a signed char. In many cases, where only ASCII characters are used, the code should have been using the cairo version of the ctype function to avoid locale issues.
-rw-r--r--boilerplate/cairo-boilerplate.c4
-rw-r--r--perf/cairo-perf-report.c3
-rw-r--r--src/cairo-ctype-inline.h86
-rw-r--r--src/cairo-script-surface.c2
-rw-r--r--src/cairoint.h28
-rw-r--r--util/cairo-trace/trace.c12
6 files changed, 99 insertions, 36 deletions
diff --git a/boilerplate/cairo-boilerplate.c b/boilerplate/cairo-boilerplate.c
index c5d3ad236..a3e710f08 100644
--- a/boilerplate/cairo-boilerplate.c
+++ b/boilerplate/cairo-boilerplate.c
@@ -30,6 +30,7 @@
#include <pixman.h>
+#include <cairo-ctype-inline.h>
#include <cairo-types-private.h>
#include <cairo-scaled-font-private.h>
@@ -39,7 +40,6 @@
#include <stddef.h>
#include <stdlib.h>
-#include <ctype.h>
#include <assert.h>
#include <errno.h>
@@ -561,7 +561,7 @@ _cairo_boilerplate_target_matches_name (const cairo_boilerplate_target_t *target
if (! (name_len == 1 && 0 == strncmp (tname, "?", 1))) { /* wildcard? */
if (0 != strncmp (target->name, tname, name_len)) /* exact match? */
return FALSE;
- if (isalnum (target->name[name_len]))
+ if (_cairo_isalnum (target->name[name_len]))
return FALSE;
}
diff --git a/perf/cairo-perf-report.c b/perf/cairo-perf-report.c
index 64a680242..163368fee 100644
--- a/perf/cairo-perf-report.c
+++ b/perf/cairo-perf-report.c
@@ -32,6 +32,7 @@
#include "cairo-perf.h"
#include "cairo-missing.h"
#include "cairo-stats.h"
+#include "cairo-ctype-inline.h"
#include <stdio.h>
#include <stdlib.h>
@@ -100,7 +101,7 @@ do { \
#define parse_string(result) \
do { \
for (end = s; *end; end++) \
- if (isspace (*end)) \
+ if (_cairo_isspace (*end)) \
break; \
(result) = strndup (s, end - s); \
if ((result) == NULL) { \
diff --git a/src/cairo-ctype-inline.h b/src/cairo-ctype-inline.h
new file mode 100644
index 000000000..6cc5d005b
--- /dev/null
+++ b/src/cairo-ctype-inline.h
@@ -0,0 +1,86 @@
+/* cairo - a vector graphics library with display and print output
+ *
+ * Copyright © 2009,2016,2021,2022 Adrian Johnson
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it either under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation
+ * (the "LGPL") or, at your option, under the terms of the Mozilla
+ * Public License Version 1.1 (the "MPL"). If you do not alter this
+ * notice, a recipient may use your version of this file under either
+ * the MPL or the LGPL.
+ *
+ * You should have received a copy of the LGPL along with this library
+ * in the file COPYING-LGPL-2.1; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
+ * You should have received a copy of the MPL along with this library
+ * in the file COPYING-MPL-1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
+ * OF ANY KIND, either express or implied. See the LGPL or the MPL for
+ * the specific language governing rights and limitations.
+ *
+ * The Original Code is the cairo graphics library.
+ *
+ * The Initial Developer of the Original Code is University of Southern
+ * California.
+ *
+ * Contributor(s):
+ * Adrian Johnson <ajohnson@redneon.com>
+ */
+
+#ifndef _CAIRO_CTYPE_INLINE_H_
+#define _CAIRO_CTYPE_INLINE_H_
+
+#include "cairo-error-private.h"
+
+CAIRO_BEGIN_DECLS
+
+/* ASCII only versions of some ctype.h character classification functions.
+ * The glibc versions are slow in UTF-8 locales.
+ */
+
+static inline int cairo_const
+_cairo_isspace (int c)
+{
+ return (c == 0x20 || (c >= 0x09 && c <= 0x0d));
+}
+
+static inline int cairo_const
+_cairo_isdigit (int c)
+{
+ return (c >= '0' && c <= '9');
+}
+
+static inline int cairo_const
+_cairo_isxdigit (int c)
+{
+ return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
+}
+
+static inline int cairo_const
+_cairo_isalpha (int c)
+{
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+}
+
+static inline int cairo_const
+_cairo_isprint (int c)
+{
+ return (c >= 0x20 && c <= 0x7e);
+}
+
+static inline int cairo_const
+_cairo_isalnum (int c)
+{
+ return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
+}
+
+CAIRO_END_DECLS
+
+#endif /* _CAIRO_CTYPE_INLINE_H_ */
diff --git a/src/cairo-script-surface.c b/src/cairo-script-surface.c
index ca9bafbb7..b96d7d182 100644
--- a/src/cairo-script-surface.c
+++ b/src/cairo-script-surface.c
@@ -3315,7 +3315,7 @@ ESCAPED_CHAR:
_cairo_output_stream_printf (ctx->stream, "\\%c", c);
break;
default:
- if (isprint (c) || isspace (c)) {
+ if (_cairo_isprint(c)) {
_cairo_output_stream_printf (ctx->stream, "%c", c);
} else {
char buf[4] = { '\\' };
diff --git a/src/cairoint.h b/src/cairoint.h
index 8d472b0bb..79b4ffaf0 100644
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -271,33 +271,7 @@ static inline void put_unaligned_be32 (uint32_t v, unsigned char *p)
p[3] = v & 0xff;
}
-/* The glibc versions of ispace() and isdigit() are slow in UTF-8 locales.
- */
-
-static inline int cairo_const
-_cairo_isspace (int c)
-{
- return (c == 0x20 || (c >= 0x09 && c <= 0x0d));
-}
-
-static inline int cairo_const
-_cairo_isdigit (int c)
-{
- return (c >= '0' && c <= '9');
-}
-
-static inline int cairo_const
-_cairo_isxdigit (int c)
-{
- return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
-}
-
-static inline int cairo_const
-_cairo_isalpha (int c)
-{
- return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
-}
-
+#include "cairo-ctype-inline.h"
#include "cairo-types-private.h"
#include "cairo-cache-private.h"
#include "cairo-reference-count-private.h"
diff --git a/util/cairo-trace/trace.c b/util/cairo-trace/trace.c
index fd9a4a38a..61e57f4d8 100644
--- a/util/cairo-trace/trace.c
+++ b/util/cairo-trace/trace.c
@@ -47,6 +47,8 @@
# include <cairo-ft.h>
#endif
+#include "cairo-ctype-inline.h"
+
#ifndef TRUE
#define TRUE 1
#define FALSE 0
@@ -565,7 +567,7 @@ _trace_dtostr (char *buffer, size_t size, double d)
if (*p == '+' || *p == '-')
p++;
- while (isdigit (*p))
+ while (_cairo_isdigit (*p))
p++;
if (strncmp (p, decimal_point, decimal_point_len) == 0)
@@ -585,7 +587,7 @@ _trace_dtostr (char *buffer, size_t size, double d)
if (*p == '+' || *p == '-')
p++;
- while (isdigit (*p))
+ while (_cairo_isdigit (*p))
p++;
if (strncmp (p, decimal_point, decimal_point_len) == 0) {
@@ -651,7 +653,7 @@ _trace_vprintf (const char *fmt, va_list ap)
f++;
}
- while (isdigit (*f))
+ while (_cairo_isdigit (*f))
f++;
length_modifier = 0;
@@ -1850,7 +1852,7 @@ _encode_string_literal (char *out, int max,
max -= 2;
break;
default:
- if (isprint (c) || isspace (c)) {
+ if (_cairo_isprint (c)) {
*out++ = c;
} else {
int octal = 0;
@@ -1920,7 +1922,7 @@ ESCAPED_CHAR:
_trace_printf ("\\%c", c);
break;
default:
- if (isprint (c) || isspace (c)) {
+ if (_cairo_isprint (c)) {
_trace_printf ("%c", c);
} else {
char buf[4] = { '\\' };