Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Kea
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
445
Issues
445
List
Boards
Labels
Service Desk
Milestones
Merge Requests
71
Merge Requests
71
Operations
Operations
Incidents
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
ISC Open Source Projects
Kea
Commits
ba9b18cf
Commit
ba9b18cf
authored
Jun 18, 2018
by
Francis Dupont
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[5582] Fixed handling of unicode escapes
parent
4d388fbf
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
3 deletions
+74
-3
src/lib/cc/data.cc
src/lib/cc/data.cc
+44
-1
src/lib/cc/tests/data_unittests.cc
src/lib/cc/tests/data_unittests.cc
+30
-2
No files found.
src/lib/cc/data.cc
View file @
ba9b18cf
...
...
@@ -357,6 +357,7 @@ strFromStringstream(std::istream& in, const std::string& file,
while
(
c
!=
EOF
&&
c
!=
'"'
)
{
if
(
c
==
'\\'
)
{
// see the spec for allowed escape characters
int
d
;
switch
(
in
.
peek
())
{
case
'"'
:
c
=
'"'
;
...
...
@@ -382,6 +383,48 @@ strFromStringstream(std::istream& in, const std::string& file,
case
't'
:
c
=
'\t'
;
break
;
case
'u'
:
// skip first 0
in
.
ignore
();
++
pos
;
c
=
in
.
peek
();
if
(
c
!=
'0'
)
{
throwJSONError
(
"Unsupported unicode escape"
,
file
,
line
,
pos
);
}
// skip second 0
in
.
ignore
();
++
pos
;
c
=
in
.
peek
();
if
(
c
!=
'0'
)
{
throwJSONError
(
"Unsupported unicode escape"
,
file
,
line
,
pos
-
2
);
}
// get first digit
in
.
ignore
();
++
pos
;
d
=
in
.
peek
();
if
((
d
>=
'0'
)
&&
(
d
<=
'9'
))
{
c
=
(
d
-
'0'
)
<<
4
;
}
else
if
((
d
>=
'A'
)
&&
(
d
<=
'F'
))
{
c
=
(
d
-
'A'
+
10
)
<<
4
;
}
else
if
((
d
>=
'a'
)
&&
(
d
<=
'f'
))
{
c
=
(
d
-
'a'
+
10
)
<<
4
;
}
else
{
throwJSONError
(
"Not hexadecimal in unicode escape"
,
file
,
line
,
pos
-
3
);
}
// get second digit
in
.
ignore
();
++
pos
;
d
=
in
.
peek
();
if
((
d
>=
'0'
)
&&
(
d
<=
'9'
))
{
c
|=
d
-
'0'
;
}
else
if
((
d
>=
'A'
)
&&
(
d
<=
'F'
))
{
c
|=
d
-
'A'
+
10
;
}
else
if
((
d
>=
'a'
)
&&
(
d
<=
'f'
))
{
c
|=
d
-
'a'
+
10
;
}
else
{
throwJSONError
(
"Not hexadecimal in unicode escape"
,
file
,
line
,
pos
-
4
);
}
break
;
default:
throwJSONError
(
"Bad escape"
,
file
,
line
,
pos
);
}
...
...
@@ -797,7 +840,7 @@ StringElement::toJSON(std::ostream& ss) const {
ss
<<
'\\'
<<
't'
;
break
;
default:
if
((
c
>=
0
)
&&
(
c
<
0x20
))
{
if
((
(
c
>=
0
)
&&
(
c
<
0x20
))
||
(
c
<
0
)
||
(
c
>=
0x7f
))
{
std
::
ostringstream
esc
;
esc
<<
"
\\
u"
<<
hex
...
...
src/lib/cc/tests/data_unittests.cc
View file @
ba9b18cf
...
...
@@ -100,7 +100,7 @@ TEST(Element, from_and_to_json) {
// We should confirm that our string handling is 8-bit clean.
// At one point we were using char-length data and comparing to EOF,
// which means that character '\xFF' would not parse properly.
sv
.
push_back
(
"
\"\
xFF
\"
"
);
sv
.
push_back
(
"
\"\
\
u00ff
\"
"
);
BOOST_FOREACH
(
const
std
::
string
&
s
,
sv
)
{
// Test two types of fromJSON(): with string and istream.
...
...
@@ -150,7 +150,12 @@ TEST(Element, from_and_to_json) {
// String not delimited correctly
sv
.
push_back
(
"
\"
hello"
);
sv
.
push_back
(
"hello
\"
"
);
// Bad unicode
sv
.
push_back
(
"
\"\\
u123
\"
"
);
sv
.
push_back
(
"
\"\\
u1234
\"
"
);
sv
.
push_back
(
"
\"\\
u0123
\"
"
);
sv
.
push_back
(
"
\"\\
u00ag
\"
"
);
sv
.
push_back
(
"
\"\\
u00BH
\"
"
);
BOOST_FOREACH
(
std
::
string
s
,
sv
)
{
EXPECT_THROW
(
el
=
Element
::
fromJSON
(
s
),
isc
::
data
::
JSONError
);
...
...
@@ -550,11 +555,21 @@ TEST(Element, escape) {
escapeHelper
(
"foo
\n
bar"
,
"
\"
foo
\\
nbar
\"
"
);
escapeHelper
(
"foo
\r
bar"
,
"
\"
foo
\\
rbar
\"
"
);
escapeHelper
(
"foo
\t
bar"
,
"
\"
foo
\\
tbar
\"
"
);
escapeHelper
(
"foo\u001fbar"
,
"
\"
foo
\\
u001fbar
\"
"
);
// Bad escapes
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
a"
),
JSONError
);
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
"
),
JSONError
);
// Can't have escaped quotes outside strings
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\\"\\\"
"
),
JSONError
);
// Unicode use lower u and 4 hexa, only 00 prefix is supported
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
U0020"
),
JSONError
);
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
u002"
),
JSONError
);
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
u0123"
),
JSONError
);
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
u1023"
),
JSONError
);
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
u00ag"
),
JSONError
);
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
u00ga"
),
JSONError
);
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
u00BH"
),
JSONError
);
EXPECT_THROW
(
Element
::
fromJSON
(
"
\\
u00HB"
),
JSONError
);
// Inside strings is OK
EXPECT_NO_THROW
(
Element
::
fromJSON
(
"
\"\\\"\\\"\"
"
));
// A whitespace test
...
...
@@ -565,6 +580,19 @@ TEST(Element, escape) {
// Control characters
StringElement
bell
(
"foo
\a
bar"
);
EXPECT_EQ
(
"
\"
foo
\\
u0007bar
\"
"
,
bell
.
str
());
// 8 bit escape
StringElement
ab
(
"foo
\253
bar"
);
EXPECT_EQ
(
"
\"
foo
\\
u00abbar
\"
"
,
ab
.
str
());
ASSERT_NO_THROW
(
Element
::
fromJSON
(
"
\"
foo
\\
u00abbar
\"
"
));
EXPECT_TRUE
(
ab
.
equals
(
*
Element
::
fromJSON
(
"
\"
foo
\\
u00abbar
\"
"
)));
ASSERT_NO_THROW
(
Element
::
fromJSON
(
"
\"
foo
\\
u00ABbar
\"
"
));
EXPECT_TRUE
(
ab
.
equals
(
*
Element
::
fromJSON
(
"
\"
foo
\\
u00ABbar
\"
"
)));
StringElement
f1
(
"foo
\361
bar"
);
EXPECT_EQ
(
"
\"
foo
\\
u00f1bar
\"
"
,
f1
.
str
());
ASSERT_NO_THROW
(
Element
::
fromJSON
(
"
\"
foo
\\
u00f1bar
\"
"
));
EXPECT_TRUE
(
f1
.
equals
(
*
Element
::
fromJSON
(
"
\"
foo
\\
u00f1bar
\"
"
)));
ASSERT_NO_THROW
(
Element
::
fromJSON
(
"
\"
foo
\\
u00F1bar
\"
"
));
EXPECT_TRUE
(
f1
.
equals
(
*
Element
::
fromJSON
(
"
\"
foo
\\
u00F1bar
\"
"
)));
}
// This test verifies that strings are copied.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment