1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
From e89cfe51d2001af08fc2a13e5133ba8157f90beb Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
Date: Tue, 1 Jul 2014 18:27:19 -0400
Subject: [PATCH] fix aliasing violations in mbtowc and mbrtowc
these functions were setting wc to point to wchar_t aliasing itself as
a "cheap" way to support null wc arguments. doing so was anything but
cheap, since even without the aliasing violation, it would limit the
compiler's ability to optimize.
making wc point to a dummy object is equally easy and does not suffer
from the above problems.
---
src/multibyte/mbrtowc.c | 3 ++-
src/multibyte/mbtowc.c | 3 ++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/multibyte/mbrtowc.c b/src/multibyte/mbrtowc.c
index 35e834e..e7b3654 100644
--- a/src/multibyte/mbrtowc.c
+++ b/src/multibyte/mbrtowc.c
@@ -14,6 +14,7 @@ size_t mbrtowc(wchar_t *restrict wc, const char *restrict src, size_t n, mbstate
unsigned c;
const unsigned char *s = (const void *)src;
const unsigned N = n;
+ wchar_t dummy;
if (!st) st = (void *)&internal_state;
c = *(unsigned *)st;
@@ -21,7 +22,7 @@ size_t mbrtowc(wchar_t *restrict wc, const char *restrict src, size_t n, mbstate
if (!s) {
if (c) goto ilseq;
return 0;
- } else if (!wc) wc = (void *)&wc;
+ } else if (!wc) wc = &dummy;
if (!n) return -2;
if (!c) {
diff --git a/src/multibyte/mbtowc.c b/src/multibyte/mbtowc.c
index 6710637..803d221 100644
--- a/src/multibyte/mbtowc.c
+++ b/src/multibyte/mbtowc.c
@@ -12,10 +12,11 @@ int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n)
{
unsigned c;
const unsigned char *s = (const void *)src;
+ wchar_t dummy;
if (!s) return 0;
if (!n) goto ilseq;
- if (!wc) wc = (void *)&wc;
+ if (!wc) wc = &dummy;
if (*s < 0x80) return !!(*wc = *s);
if (*s-SA > SB-SA) goto ilseq;
--
2.0.1
|