Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a79f4c21
Commit
a79f4c21
authored
Apr 19, 2017
by
Serhiy Storchaka
Committed by
GitHub
Apr 19, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-30070: Fixed leaks and crashes in errors handling in the parser module. (#1131)
parent
d90045f3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
162 additions
and
52 deletions
+162
-52
Lib/test/test_parser.py
Lib/test/test_parser.py
+81
-0
Misc/NEWS
Misc/NEWS
+2
-0
Modules/parsermodule.c
Modules/parsermodule.c
+79
-52
No files found.
Lib/test/test_parser.py
View file @
a79f4c21
import
copy
import
parser
import
parser
import
pickle
import
unittest
import
unittest
import
operator
import
operator
import
struct
import
struct
...
@@ -424,6 +426,52 @@ class IllegalSyntaxTestCase(unittest.TestCase):
...
@@ -424,6 +426,52 @@ class IllegalSyntaxTestCase(unittest.TestCase):
# not even remotely valid:
# not even remotely valid:
self
.
check_bad_tree
((
1
,
2
,
3
),
"<junk>"
)
self
.
check_bad_tree
((
1
,
2
,
3
),
"<junk>"
)
def
test_illegal_terminal
(
self
):
tree
=
\
(
257
,
(
269
,
(
270
,
(
271
,
(
277
,
(
1
,))),
(
4
,
''
))),
(
4
,
''
),
(
0
,
''
))
self
.
check_bad_tree
(
tree
,
"too small items in terminal node"
)
tree
=
\
(
257
,
(
269
,
(
270
,
(
271
,
(
277
,
(
1
,
b'pass'
))),
(
4
,
''
))),
(
4
,
''
),
(
0
,
''
))
self
.
check_bad_tree
(
tree
,
"non-string second item in terminal node"
)
tree
=
\
(
257
,
(
269
,
(
270
,
(
271
,
(
277
,
(
1
,
'pass'
,
'0'
,
0
))),
(
4
,
''
))),
(
4
,
''
),
(
0
,
''
))
self
.
check_bad_tree
(
tree
,
"non-integer third item in terminal node"
)
tree
=
\
(
257
,
(
269
,
(
270
,
(
271
,
(
277
,
(
1
,
'pass'
,
0
,
0
))),
(
4
,
''
))),
(
4
,
''
),
(
0
,
''
))
self
.
check_bad_tree
(
tree
,
"too many items in terminal node"
)
def
test_illegal_yield_1
(
self
):
def
test_illegal_yield_1
(
self
):
# Illegal yield statement: def f(): return 1; yield 1
# Illegal yield statement: def f(): return 1; yield 1
tree
=
\
tree
=
\
...
@@ -628,6 +676,24 @@ class IllegalSyntaxTestCase(unittest.TestCase):
...
@@ -628,6 +676,24 @@ class IllegalSyntaxTestCase(unittest.TestCase):
(
4
,
''
),
(
0
,
''
))
(
4
,
''
),
(
0
,
''
))
self
.
check_bad_tree
(
tree
,
"from import fred"
)
self
.
check_bad_tree
(
tree
,
"from import fred"
)
def
test_illegal_encoding
(
self
):
# Illegal encoding declaration
tree
=
\
(
339
,
(
257
,
(
0
,
''
)))
self
.
check_bad_tree
(
tree
,
"missed encoding"
)
tree
=
\
(
339
,
(
257
,
(
0
,
''
)),
b'iso-8859-1'
)
self
.
check_bad_tree
(
tree
,
"non-string encoding"
)
tree
=
\
(
339
,
(
257
,
(
0
,
''
)),
'
\
udcff
'
)
with
self
.
assertRaises
(
UnicodeEncodeError
):
parser
.
sequence2st
(
tree
)
class
CompileTestCase
(
unittest
.
TestCase
):
class
CompileTestCase
(
unittest
.
TestCase
):
...
@@ -772,6 +838,21 @@ class STObjectTestCase(unittest.TestCase):
...
@@ -772,6 +838,21 @@ class STObjectTestCase(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
operator
.
lt
,
st1
,
1815
)
self
.
assertRaises
(
TypeError
,
operator
.
lt
,
st1
,
1815
)
self
.
assertRaises
(
TypeError
,
operator
.
gt
,
b'waterloo'
,
st2
)
self
.
assertRaises
(
TypeError
,
operator
.
gt
,
b'waterloo'
,
st2
)
def
test_copy_pickle
(
self
):
sts
=
[
parser
.
expr
(
'2 + 3'
),
parser
.
suite
(
'x = 2; y = x + 3'
),
parser
.
expr
(
'list(x**3 for x in range(20))'
)
]
for
st
in
sts
:
st_copy
=
copy
.
copy
(
st
)
self
.
assertEqual
(
st_copy
.
totuple
(),
st
.
totuple
())
st_copy
=
copy
.
deepcopy
(
st
)
self
.
assertEqual
(
st_copy
.
totuple
(),
st
.
totuple
())
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
st_copy
=
pickle
.
loads
(
pickle
.
dumps
(
st
,
proto
))
self
.
assertEqual
(
st_copy
.
totuple
(),
st
.
totuple
())
check_sizeof
=
support
.
check_sizeof
check_sizeof
=
support
.
check_sizeof
@
support
.
cpython_only
@
support
.
cpython_only
...
...
Misc/NEWS
View file @
a79f4c21
...
@@ -313,6 +313,8 @@ Extension Modules
...
@@ -313,6 +313,8 @@ Extension Modules
Library
Library
-------
-------
- bpo-30070: Fixed leaks and crashes in errors handling in the parser module.
- bpo-22352: Column widths in the output of dis.dis() are now adjusted for
- bpo-22352: Column widths in the output of dis.dis() are now adjusted for
large line numbers and instruction offsets.
large line numbers and instruction offsets.
...
...
Modules/parsermodule.c
View file @
a79f4c21
...
@@ -775,32 +775,35 @@ parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
...
@@ -775,32 +775,35 @@ parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
*/
*/
tree
=
build_node_tree
(
tuple
);
tree
=
build_node_tree
(
tuple
);
if
(
tree
!=
0
)
{
if
(
tree
!=
0
)
{
node
*
validation_root
=
tree
;
node
*
validation_root
=
NULL
;
int
tree_type
=
0
;
int
tree_type
=
0
;
switch
(
TYPE
(
tree
))
{
switch
(
TYPE
(
tree
))
{
case
eval_input
:
case
eval_input
:
/* Might be an eval form. */
/* Might be an eval form. */
tree_type
=
PyST_EXPR
;
tree_type
=
PyST_EXPR
;
validation_root
=
tree
;
break
;
break
;
case
encoding_decl
:
case
encoding_decl
:
/* This looks like an encoding_decl so far. */
/* This looks like an encoding_decl so far. */
if
(
NCH
(
tree
)
!=
1
)
if
(
NCH
(
tree
)
==
1
)
{
err_string
(
"Error Parsing encoding_decl"
)
;
tree_type
=
PyST_SUITE
;
validation_root
=
CHILD
(
tree
,
0
);
validation_root
=
CHILD
(
tree
,
0
);
/* Fall through */
}
else
{
err_string
(
"Error Parsing encoding_decl"
);
}
break
;
case
file_input
:
case
file_input
:
/* This looks like an exec form so far. */
/* This looks like an exec form so far. */
tree_type
=
PyST_SUITE
;
tree_type
=
PyST_SUITE
;
validation_root
=
tree
;
break
;
break
;
default:
default:
/* This is a fragment, at best. */
/* This is a fragment, at best. */
PyNode_Free
(
tree
);
err_string
(
"parse tree does not use a valid start symbol"
);
err_string
(
"parse tree does not use a valid start symbol"
);
return
(
0
);
}
}
if
(
validate_node
(
validation_root
))
if
(
validat
ion_root
!=
NULL
&&
validat
e_node
(
validation_root
))
st
=
parser_newstobject
(
tree
,
tree_type
);
st
=
parser_newstobject
(
tree
,
tree_type
);
else
else
PyNode_Free
(
tree
);
PyNode_Free
(
tree
);
...
@@ -830,6 +833,9 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
...
@@ -830,6 +833,9 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
Py_ssize_t
i
;
Py_ssize_t
i
;
int
err
;
int
err
;
if
(
len
<
0
)
{
return
NULL
;
}
for
(
i
=
1
;
i
<
len
;
++
i
)
{
for
(
i
=
1
;
i
<
len
;
++
i
)
{
/* elem must always be a sequence, however simple */
/* elem must always be a sequence, however simple */
PyObject
*
elem
=
PySequence_GetItem
(
tuple
,
i
);
PyObject
*
elem
=
PySequence_GetItem
(
tuple
,
i
);
...
@@ -850,7 +856,7 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
...
@@ -850,7 +856,7 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
if
(
type
==
-
1
&&
PyErr_Occurred
())
{
if
(
type
==
-
1
&&
PyErr_Occurred
())
{
Py_DECREF
(
temp
);
Py_DECREF
(
temp
);
Py_DECREF
(
elem
);
Py_DECREF
(
elem
);
return
0
;
return
NULL
;
}
}
}
}
Py_DECREF
(
temp
);
Py_DECREF
(
temp
);
...
@@ -862,7 +868,7 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
...
@@ -862,7 +868,7 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
PyErr_SetObject
(
parser_error
,
err
);
PyErr_SetObject
(
parser_error
,
err
);
Py_XDECREF
(
err
);
Py_XDECREF
(
err
);
Py_XDECREF
(
elem
);
Py_XDECREF
(
elem
);
return
(
0
)
;
return
NULL
;
}
}
if
(
ISTERMINAL
(
type
))
{
if
(
ISTERMINAL
(
type
))
{
Py_ssize_t
len
=
PyObject_Size
(
elem
);
Py_ssize_t
len
=
PyObject_Size
(
elem
);
...
@@ -871,11 +877,14 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
...
@@ -871,11 +877,14 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
if
((
len
!=
2
)
&&
(
len
!=
3
))
{
if
((
len
!=
2
)
&&
(
len
!=
3
))
{
err_string
(
"terminal nodes must have 2 or 3 entries"
);
err_string
(
"terminal nodes must have 2 or 3 entries"
);
return
0
;
Py_DECREF
(
elem
);
return
NULL
;
}
}
temp
=
PySequence_GetItem
(
elem
,
1
);
temp
=
PySequence_GetItem
(
elem
,
1
);
if
(
temp
==
NULL
)
if
(
temp
==
NULL
)
{
return
0
;
Py_DECREF
(
elem
);
return
NULL
;
}
if
(
!
PyUnicode_Check
(
temp
))
{
if
(
!
PyUnicode_Check
(
temp
))
{
PyErr_Format
(
parser_error
,
PyErr_Format
(
parser_error
,
"second item in terminal node must be a string,"
"second item in terminal node must be a string,"
...
@@ -883,18 +892,22 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
...
@@ -883,18 +892,22 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
Py_TYPE
(
temp
)
->
tp_name
);
Py_TYPE
(
temp
)
->
tp_name
);
Py_DECREF
(
temp
);
Py_DECREF
(
temp
);
Py_DECREF
(
elem
);
Py_DECREF
(
elem
);
return
0
;
return
NULL
;
}
}
if
(
len
==
3
)
{
if
(
len
==
3
)
{
PyObject
*
o
=
PySequence_GetItem
(
elem
,
2
);
PyObject
*
o
=
PySequence_GetItem
(
elem
,
2
);
if
(
o
!=
NULL
)
{
if
(
o
==
NULL
)
{
Py_DECREF
(
temp
);
Py_DECREF
(
elem
);
return
NULL
;
}
if
(
PyLong_Check
(
o
))
{
if
(
PyLong_Check
(
o
))
{
int
num
=
_PyLong_AsInt
(
o
);
int
num
=
_PyLong_AsInt
(
o
);
if
(
num
==
-
1
&&
PyErr_Occurred
())
{
if
(
num
==
-
1
&&
PyErr_Occurred
())
{
Py_DECREF
(
o
);
Py_DECREF
(
o
);
Py_DECREF
(
temp
);
Py_DECREF
(
temp
);
Py_DECREF
(
elem
);
Py_DECREF
(
elem
);
return
0
;
return
NULL
;
}
}
*
line_num
=
num
;
*
line_num
=
num
;
}
}
...
@@ -906,23 +919,22 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
...
@@ -906,23 +919,22 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
Py_DECREF
(
o
);
Py_DECREF
(
o
);
Py_DECREF
(
temp
);
Py_DECREF
(
temp
);
Py_DECREF
(
elem
);
Py_DECREF
(
elem
);
return
0
;
return
NULL
;
}
}
Py_DECREF
(
o
);
Py_DECREF
(
o
);
}
}
}
temp_str
=
PyUnicode_AsUTF8AndSize
(
temp
,
&
len
);
temp_str
=
PyUnicode_AsUTF8AndSize
(
temp
,
&
len
);
if
(
temp_str
==
NULL
)
{
if
(
temp_str
==
NULL
)
{
Py_DECREF
(
temp
);
Py_DECREF
(
temp
);
Py_
X
DECREF
(
elem
);
Py_DECREF
(
elem
);
return
0
;
return
NULL
;
}
}
strn
=
(
char
*
)
PyObject_MALLOC
(
len
+
1
);
strn
=
(
char
*
)
PyObject_MALLOC
(
len
+
1
);
if
(
strn
==
NULL
)
{
if
(
strn
==
NULL
)
{
Py_DECREF
(
temp
);
Py_DECREF
(
temp
);
Py_
X
DECREF
(
elem
);
Py_DECREF
(
elem
);
PyErr_NoMemory
();
PyErr_NoMemory
();
return
0
;
return
NULL
;
}
}
(
void
)
memcpy
(
strn
,
temp_str
,
len
+
1
);
(
void
)
memcpy
(
strn
,
temp_str
,
len
+
1
);
Py_DECREF
(
temp
);
Py_DECREF
(
temp
);
...
@@ -932,20 +944,21 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
...
@@ -932,20 +944,21 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
* It has to be one or the other; this is an error.
* It has to be one or the other; this is an error.
* Raise an exception.
* Raise an exception.
*/
*/
PyObject
*
err
=
Py_BuildValue
(
"
o
s"
,
elem
,
"unknown node type."
);
PyObject
*
err
=
Py_BuildValue
(
"
O
s"
,
elem
,
"unknown node type."
);
PyErr_SetObject
(
parser_error
,
err
);
PyErr_SetObject
(
parser_error
,
err
);
Py_XDECREF
(
err
);
Py_XDECREF
(
err
);
Py_
X
DECREF
(
elem
);
Py_DECREF
(
elem
);
return
(
0
)
;
return
NULL
;
}
}
err
=
PyNode_AddChild
(
root
,
type
,
strn
,
*
line_num
,
0
);
err
=
PyNode_AddChild
(
root
,
type
,
strn
,
*
line_num
,
0
);
if
(
err
==
E_NOMEM
)
{
if
(
err
==
E_NOMEM
)
{
Py_
X
DECREF
(
elem
);
Py_DECREF
(
elem
);
PyObject_FREE
(
strn
);
PyObject_FREE
(
strn
);
return
(
node
*
)
PyErr_NoMemory
();
PyErr_NoMemory
();
return
NULL
;
}
}
if
(
err
==
E_OVERFLOW
)
{
if
(
err
==
E_OVERFLOW
)
{
Py_
X
DECREF
(
elem
);
Py_DECREF
(
elem
);
PyObject_FREE
(
strn
);
PyObject_FREE
(
strn
);
PyErr_SetString
(
PyExc_ValueError
,
PyErr_SetString
(
PyExc_ValueError
,
"unsupported number of child nodes"
);
"unsupported number of child nodes"
);
...
@@ -956,14 +969,14 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
...
@@ -956,14 +969,14 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
node
*
new_child
=
CHILD
(
root
,
i
-
1
);
node
*
new_child
=
CHILD
(
root
,
i
-
1
);
if
(
new_child
!=
build_node_children
(
elem
,
new_child
,
line_num
))
{
if
(
new_child
!=
build_node_children
(
elem
,
new_child
,
line_num
))
{
Py_
X
DECREF
(
elem
);
Py_DECREF
(
elem
);
return
(
0
)
;
return
NULL
;
}
}
}
}
else
if
(
type
==
NEWLINE
)
{
/* It's true: we increment the */
else
if
(
type
==
NEWLINE
)
{
/* It's true: we increment the */
++
(
*
line_num
);
/* line number *after* the newline! */
++
(
*
line_num
);
/* line number *after* the newline! */
}
}
Py_
X
DECREF
(
elem
);
Py_DECREF
(
elem
);
}
}
return
root
;
return
root
;
}
}
...
@@ -998,11 +1011,24 @@ build_node_tree(PyObject *tuple)
...
@@ -998,11 +1011,24 @@ build_node_tree(PyObject *tuple)
if
(
num
==
encoding_decl
)
{
if
(
num
==
encoding_decl
)
{
encoding
=
PySequence_GetItem
(
tuple
,
2
);
encoding
=
PySequence_GetItem
(
tuple
,
2
);
if
(
encoding
==
NULL
)
{
PyErr_SetString
(
parser_error
,
"missed encoding"
);
return
NULL
;
}
if
(
!
PyUnicode_Check
(
encoding
))
{
PyErr_Format
(
parser_error
,
"encoding must be a string, found %.200s"
,
Py_TYPE
(
encoding
)
->
tp_name
);
Py_DECREF
(
encoding
);
return
NULL
;
}
/* tuple isn't borrowed anymore here, need to DECREF */
/* tuple isn't borrowed anymore here, need to DECREF */
tuple
=
PySequence_GetSlice
(
tuple
,
0
,
2
);
tuple
=
PySequence_GetSlice
(
tuple
,
0
,
2
);
if
(
tuple
==
NULL
)
if
(
tuple
==
NULL
)
{
Py_DECREF
(
encoding
);
return
NULL
;
return
NULL
;
}
}
}
res
=
PyNode_New
(
num
);
res
=
PyNode_New
(
num
);
if
(
res
!=
NULL
)
{
if
(
res
!=
NULL
)
{
if
(
res
!=
build_node_children
(
tuple
,
res
,
&
line_num
))
{
if
(
res
!=
build_node_children
(
tuple
,
res
,
&
line_num
))
{
...
@@ -1014,31 +1040,33 @@ build_node_tree(PyObject *tuple)
...
@@ -1014,31 +1040,33 @@ build_node_tree(PyObject *tuple)
const
char
*
temp
;
const
char
*
temp
;
temp
=
PyUnicode_AsUTF8AndSize
(
encoding
,
&
len
);
temp
=
PyUnicode_AsUTF8AndSize
(
encoding
,
&
len
);
if
(
temp
==
NULL
)
{
if
(
temp
==
NULL
)
{
Py
_DECREF
(
res
);
Py
Node_Free
(
res
);
Py_DECREF
(
encoding
);
Py_DECREF
(
encoding
);
Py_DECREF
(
tuple
);
Py_DECREF
(
tuple
);
return
NULL
;
return
NULL
;
}
}
res
->
n_str
=
(
char
*
)
PyObject_MALLOC
(
len
+
1
);
res
->
n_str
=
(
char
*
)
PyObject_MALLOC
(
len
+
1
);
if
(
res
->
n_str
==
NULL
)
{
if
(
res
->
n_str
==
NULL
)
{
Py
_DECREF
(
res
);
Py
Node_Free
(
res
);
Py_DECREF
(
encoding
);
Py_DECREF
(
encoding
);
Py_DECREF
(
tuple
);
Py_DECREF
(
tuple
);
PyErr_NoMemory
();
PyErr_NoMemory
();
return
NULL
;
return
NULL
;
}
}
(
void
)
memcpy
(
res
->
n_str
,
temp
,
len
+
1
);
(
void
)
memcpy
(
res
->
n_str
,
temp
,
len
+
1
);
}
}
if
(
encoding
!=
NULL
)
{
Py_DECREF
(
encoding
);
Py_DECREF
(
encoding
);
Py_DECREF
(
tuple
);
Py_DECREF
(
tuple
);
}
}
}
}
}
else
{
else
{
/* The tuple is illegal -- if the number is neither TERMINAL nor
/* The tuple is illegal -- if the number is neither TERMINAL nor
* NONTERMINAL, we can't use it. Not sure the implementation
* NONTERMINAL, we can't use it. Not sure the implementation
* allows this condition, but the API doesn't preclude it.
* allows this condition, but the API doesn't preclude it.
*/
*/
PyObject
*
err
=
Py_BuildValue
(
"
o
s"
,
tuple
,
PyObject
*
err
=
Py_BuildValue
(
"
O
s"
,
tuple
,
"Illegal component tuple."
);
"Illegal component tuple."
);
PyErr_SetObject
(
parser_error
,
err
);
PyErr_SetObject
(
parser_error
,
err
);
Py_XDECREF
(
err
);
Py_XDECREF
(
err
);
...
@@ -1073,7 +1101,6 @@ parser__pickler(PyObject *self, PyObject *args)
...
@@ -1073,7 +1101,6 @@ parser__pickler(PyObject *self, PyObject *args)
result
=
Py_BuildValue
(
"O(O)"
,
pickle_constructor
,
tuple
);
result
=
Py_BuildValue
(
"O(O)"
,
pickle_constructor
,
tuple
);
Py_DECREF
(
tuple
);
Py_DECREF
(
tuple
);
}
}
Py_DECREF
(
empty_dict
);
Py_DECREF
(
newargs
);
Py_DECREF
(
newargs
);
}
}
finally:
finally:
...
...
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