@@ -155,80 +155,53 @@ int UnescapeStringLiteral(const char *p_string, long *r_unescaped_string)
155155 t_value [t_length ++ ] = '\t' ;
156156 else if (* t_ptr == '\\' )
157157 t_value [t_length ++ ] = '\\' ;
158- else if (* t_ptr == 'x' )
159- {
160- t_ptr += 1 ;
161-
162- if (t_ptr + 1 < t_limit )
163- {
164- int t_nibble_1 , t_nibble_2 ;
165- int t_char ;
166-
167- if (!char_to_nibble (* t_ptr , & t_nibble_1 ))
168- goto error_exit ;
169- t_ptr += 1 ;
170- if (!char_to_nibble (* t_ptr , & t_nibble_2 ))
171- goto error_exit ;
172-
173- t_char = (t_nibble_1 << 4 ) | t_nibble_2 ;
174- append_utf8_char (t_value , & t_length , t_char );
175- }
176- }
177158 else if (* t_ptr == 'u' )
178159 {
160+ // We expect the form:
161+ // \u{ABCDEF}
162+ // With BCDEF all optional.
179163 t_ptr += 1 ;
180-
181- if (t_ptr + 3 < t_limit )
164+ if (t_ptr < t_limit && * t_ptr == '{' )
182165 {
183- int t_nibble_1 , t_nibble_2 , t_nibble_3 , t_nibble_4 ;
184166 int t_char ;
167+ t_char = 0 ;
168+ for (int i = 0 ; i < 6 ; i ++ )
169+ {
170+ // Advance the input ptr - if we are at the end here
171+ // it is an error.
172+ t_ptr += 1 ;
173+ if (t_ptr >= t_limit )
174+ goto error_exit ;
175+
176+ // If we get a } we are done, unless we haven't seen
177+ // a nibble yet, in which case it is an error.
178+ if (* t_ptr == '}' )
179+ {
180+ if (i == 0 )
181+ goto error_exit ;
182+ break ;
183+ }
184+
185+ // Parse the next nibble, shift and add it.
186+ int t_nibble ;
187+ if (!char_to_nibble (* t_ptr , & t_nibble ))
188+ goto error_exit ;
189+
190+ t_char = t_char << 4 ;
191+ t_char |= t_nibble ;
192+ }
185193
186- if (!char_to_nibble (* t_ptr , & t_nibble_1 ))
187- goto error_exit ;
188- t_ptr += 1 ;
189- if (!char_to_nibble (* t_ptr , & t_nibble_2 ))
190- goto error_exit ;
191- t_ptr += 1 ;
192- if (!char_to_nibble (* t_ptr , & t_nibble_3 ))
193- goto error_exit ;
194- t_ptr += 1 ;
195- if (!char_to_nibble (* t_ptr , & t_nibble_4 ))
194+ // If we get here and we are not looking at } then it
195+ // is an error.
196+ if (* t_ptr != '}' )
196197 goto error_exit ;
197198
198- t_char = (t_nibble_1 << 12 ) | (t_nibble_2 << 8 ) | (t_nibble_3 << 4 ) | t_nibble_4 ;
199- append_utf8_char (t_value , & t_length , t_char );
200- }
201- }
202- else if (* t_ptr == 'U' )
203- {
204- t_ptr += 1 ;
205-
206- if (t_ptr + 5 < t_limit )
207- {
208- int t_nibble_1 , t_nibble_2 , t_nibble_3 , t_nibble_4 , t_nibble_5 , t_nibble_6 ;
209- int t_char ;
210-
211- if (!char_to_nibble (* t_ptr , & t_nibble_1 ))
212- goto error_exit ;
213199 t_ptr += 1 ;
214- if (!char_to_nibble (* t_ptr , & t_nibble_2 ))
215- goto error_exit ;
216- t_ptr += 1 ;
217- if (!char_to_nibble (* t_ptr , & t_nibble_3 ))
218- goto error_exit ;
219- t_ptr += 1 ;
220- if (!char_to_nibble (* t_ptr , & t_nibble_4 ))
221- goto error_exit ;
222- t_ptr += 1 ;
223- if (!char_to_nibble (* t_ptr , & t_nibble_5 ))
224- goto error_exit ;
225- t_ptr += 1 ;
226- if (!char_to_nibble (* t_ptr , & t_nibble_6 ))
227- goto error_exit ;
228200
229- t_char = (t_nibble_1 << 20 ) | (t_nibble_2 << 16 ) | (t_nibble_3 << 12 ) | (t_nibble_4 << 8 ) | (t_nibble_5 << 4 ) | t_nibble_6 ;
230201 append_utf8_char (t_value , & t_length , t_char );
231202 }
203+ else
204+ goto error_exit ;
232205 }
233206 else
234207 goto error_exit ;
0 commit comments