Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
778a0c14
T
Third Party CJSON
项目概览
OpenHarmony
/
Third Party CJSON
大约 1 年 前同步成功
通知
6
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party CJSON
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
778a0c14
编写于
3月 12, 2017
作者:
M
Max Bruckner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
parse_string: Use parsebuffer
上级
f69b109c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
36 addition
and
26 deletion
+36
-26
cJSON.c
cJSON.c
+20
-22
tests/parse_string.c
tests/parse_string.c
+16
-4
未找到文件。
cJSON.c
浏览文件 @
778a0c14
...
...
@@ -662,17 +662,17 @@ fail:
}
/* Parse the input text into an unescaped cinput, and populate item. */
static
const
unsigned
char
*
parse_string
(
cJSON
*
const
item
,
const
unsigned
char
*
const
input
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
static
const
unsigned
char
*
parse_string
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
{
const
unsigned
char
*
input_pointer
=
input
+
1
;
const
unsigned
char
*
input_end
=
input
+
1
;
const
unsigned
char
*
input_pointer
=
buffer_at_offset
(
input_buffer
)
+
1
;
const
unsigned
char
*
input_end
=
buffer_at_offset
(
input_buffer
)
+
1
;
unsigned
char
*
output_pointer
=
NULL
;
unsigned
char
*
output
=
NULL
;
/* not a string */
if
(
*
input
!=
'\"'
)
if
(
buffer_at_offset
(
input_buffer
)[
0
]
!=
'\"'
)
{
*
error_pointer
=
input
;
*
error_pointer
=
buffer_at_offset
(
input_buffer
)
;
goto
fail
;
}
...
...
@@ -680,12 +680,12 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
/* calculate approximate size of the output (overestimate) */
size_t
allocation_length
=
0
;
size_t
skipped_bytes
=
0
;
while
((
*
input_end
!=
'\"'
)
&&
(
*
input_end
!=
'\0'
))
while
((
*
input_end
!=
'\"'
)
&&
(
(
size_t
)(
input_end
-
input_buffer
->
content
)
<
input_buffer
->
length
))
{
/* is escape sequence */
if
(
input_end
[
0
]
==
'\\'
)
{
if
(
input_end
[
1
]
==
'\0'
)
if
(
(
size_t
)(
input_end
+
1
-
input_buffer
->
content
)
>=
input_buffer
->
length
)
{
/* prevent buffer overflow when last input character is a backslash */
goto
fail
;
...
...
@@ -695,13 +695,13 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
}
input_end
++
;
}
if
(
*
input_end
==
'\0
'
)
if
(
*
input_end
!=
'\"
'
)
{
goto
fail
;
/* string ended unexpectedly */
}
/* This is at most how much we need for the output */
allocation_length
=
(
size_t
)
(
input_end
-
input
)
-
skipped_bytes
;
allocation_length
=
(
size_t
)
(
input_end
-
buffer_at_offset
(
input_buffer
)
)
-
skipped_bytes
;
output
=
(
unsigned
char
*
)
hooks
->
allocate
(
allocation_length
+
sizeof
(
""
));
if
(
output
==
NULL
)
{
...
...
@@ -721,6 +721,11 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
else
{
unsigned
char
sequence_length
=
2
;
if
((
input_end
-
input_pointer
)
<
1
)
{
goto
fail
;
}
switch
(
input_pointer
[
1
])
{
case
'b'
:
...
...
@@ -768,7 +773,10 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
item
->
type
=
cJSON_String
;
item
->
valuestring
=
(
char
*
)
output
;
return
input_end
+
1
;
input_buffer
->
offset
=
(
size_t
)
(
input_end
-
input_buffer
->
content
);
input_buffer
->
offset
++
;
return
buffer_at_offset
(
input_buffer
);
fail:
if
(
output
!=
NULL
)
...
...
@@ -1141,14 +1149,7 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
/* string */
if
(
can_access_at_index
(
input_buffer
,
0
)
&&
(
buffer_at_offset
(
input_buffer
)[
0
]
==
'\"'
))
{
content_pointer
=
parse_string
(
item
,
buffer_at_offset
(
input_buffer
),
error_pointer
,
hooks
);
if
(
content_pointer
==
NULL
)
{
return
NULL
;
}
input_buffer
->
offset
=
(
size_t
)(
content_pointer
-
input_buffer
->
content
);
return
buffer_at_offset
(
input_buffer
);
return
content_pointer
=
parse_string
(
item
,
input_buffer
,
error_pointer
,
hooks
);
}
/* number */
if
(
can_access_at_index
(
input_buffer
,
0
)
&&
((
buffer_at_offset
(
input_buffer
)[
0
]
==
'-'
)
||
((
buffer_at_offset
(
input_buffer
)[
0
]
>=
'0'
)
&&
(
buffer_at_offset
(
input_buffer
)[
0
]
<=
'9'
))))
...
...
@@ -1422,7 +1423,6 @@ static const unsigned char *parse_object(cJSON * const item, parse_buffer * cons
{
cJSON
*
head
=
NULL
;
/* linked list head */
cJSON
*
current_item
=
NULL
;
const
unsigned
char
*
content_pointer
=
NULL
;
if
(
cannot_access_at_index
(
input_buffer
,
0
)
||
(
buffer_at_offset
(
input_buffer
)[
0
]
!=
'{'
))
{
...
...
@@ -1474,12 +1474,10 @@ static const unsigned char *parse_object(cJSON * const item, parse_buffer * cons
/* parse the name of the child */
input_buffer
->
offset
++
;
buffer_skip_whitespace
(
input_buffer
);
content_pointer
=
parse_string
(
current_item
,
buffer_at_offset
(
input_buffer
),
error_pointer
,
hooks
);
if
(
content_pointer
==
NULL
)
if
(
parse_string
(
current_item
,
input_buffer
,
error_pointer
,
hooks
)
==
NULL
)
{
goto
fail
;
/* faile to parse name */
}
input_buffer
->
offset
=
(
size_t
)(
content_pointer
-
input_buffer
->
content
);
buffer_skip_whitespace
(
input_buffer
);
/* swap valuestring and string, because we parsed the name */
...
...
tests/parse_string.c
浏览文件 @
778a0c14
...
...
@@ -47,16 +47,28 @@ static void assert_is_string(cJSON *string_item)
static
void
assert_parse_string
(
const
char
*
string
,
const
char
*
expected
)
{
TEST_ASSERT_NOT_NULL_MESSAGE
(
parse_string
(
item
,
(
const
unsigned
char
*
)
string
,
&
error_pointer
,
&
global_hooks
),
"Couldn't parse string."
);
parse_buffer
buffer
;
buffer
.
content
=
(
const
unsigned
char
*
)
string
;
buffer
.
length
=
strlen
(
string
)
+
sizeof
(
""
);
buffer
.
offset
=
0
;
TEST_ASSERT_NOT_NULL_MESSAGE
(
parse_string
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
),
"Couldn't parse string."
);
assert_is_string
(
item
);
TEST_ASSERT_EQUAL_STRING_MESSAGE
(
expected
,
item
->
valuestring
,
"The parsed result isn't as expected."
);
global_hooks
.
deallocate
(
item
->
valuestring
);
item
->
valuestring
=
NULL
;
}
#define assert_not_parse_string(string) \
TEST_ASSERT_NULL_MESSAGE(parse_string(item, (const unsigned char*)string, &error_pointer, &global_hooks), "Malformed string should not be accepted");\
assert_is_invalid(item)
static
void
assert_not_parse_string
(
const
char
*
const
string
)
{
parse_buffer
buffer
;
buffer
.
content
=
(
const
unsigned
char
*
)
string
;
buffer
.
length
=
strlen
(
string
)
+
sizeof
(
""
);
buffer
.
offset
=
0
;
TEST_ASSERT_NULL_MESSAGE
(
parse_string
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
),
"Malformed string should not be accepted."
);
assert_is_invalid
(
item
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录