Commit bdb64b5
authored
Rewrite encoding detection as inverted UTF-8 validator (#7213)
* Fix EncodingDetectingInputStream missing bytes 0xF8-0xFF as non-UTF-8
Bytes in the range 0xF8-0xFF are never valid in any position of a UTF-8
sequence, but the detection logic had no branch for them — they fell
through without setting charset to Windows-1252. This caused ISO-8859-1
files containing characters like ü (0xFC) to be incorrectly detected as
UTF-8, leading to `mod git apply` failures when patch bytes didn't match
file bytes.
Also reject 0xC0 and 0xC1 as UTF-8 lead bytes — these would encode
code points below U+0080 (overlong encodings forbidden by RFC 3629).
* Rewrite guessCharset as inverted UTF-8 validator
Replace the enumeration-of-bad-bytes approach with a single
remainingContinuationBytes counter. Any byte not explicitly valid
in its position is rejected as non-UTF-8, which eliminates an
entire class of detection gaps by default.
Also tightens the 4-byte lead range to 0xF0-0xF4 (0xF5-0xF7
would encode code points above U+10FFFF).
* Simplify encoding detection tests with @ParameterizedTest
Use CsvSource-based parameterized tests for byte-level validation,
hex-encoded byte arrays for precise control over test inputs, and
consolidate duplicated test patterns.1 parent 77eea15 commit bdb64b5
2 files changed
Lines changed: 86 additions & 83 deletions
File tree
- rewrite-core/src
- main/java/org/openrewrite/internal
- test/java/org/openrewrite/internal
Lines changed: 18 additions & 58 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
| 41 | + | |
41 | 42 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
| 43 | + | |
48 | 44 | | |
49 | 45 | | |
50 | 46 | | |
| |||
84 | 80 | | |
85 | 81 | | |
86 | 82 | | |
87 | | - | |
| 83 | + | |
88 | 84 | | |
89 | 85 | | |
90 | 86 | | |
| |||
116 | 112 | | |
117 | 113 | | |
118 | 114 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
128 | 118 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | 119 | | |
135 | 120 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
151 | 132 | | |
152 | 133 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | 134 | | |
157 | 135 | | |
158 | 136 | | |
| |||
197 | 175 | | |
198 | 176 | | |
199 | 177 | | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | 178 | | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | 179 | | |
220 | 180 | | |
221 | 181 | | |
| |||
Lines changed: 68 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
22 | | - | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
84 | 93 | | |
85 | 94 | | |
86 | 95 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
95 | 108 | | |
96 | 109 | | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
105 | 131 | | |
106 | 132 | | |
107 | 133 | | |
| |||
128 | 154 | | |
129 | 155 | | |
130 | 156 | | |
131 | | - | |
132 | 157 | | |
133 | 158 | | |
134 | 159 | | |
| |||
138 | 163 | | |
139 | 164 | | |
140 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
141 | 184 | | |
142 | 185 | | |
143 | 186 | | |
| |||
0 commit comments