Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
631475ac
Commit
631475ac
authored
Apr 20, 2020
by
Sam Sneddon
Committed by
Stefan Behnel
Apr 20, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid integer overflow when decoding bytes/charptr (GH-3535)
Fixes GH-3534.
parent
43517bfb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
4 deletions
+38
-4
Cython/Utility/StringTools.c
Cython/Utility/StringTools.c
+4
-4
tests/run/bytesmethods.pyx
tests/run/bytesmethods.pyx
+16
-0
tests/run/charptr_decode.pyx
tests/run/charptr_decode.pyx
+18
-0
No files found.
Cython/Utility/StringTools.c
View file @
631475ac
...
@@ -466,9 +466,9 @@ static CYTHON_INLINE PyObject* __Pyx_decode_c_string(
...
@@ -466,9 +466,9 @@ static CYTHON_INLINE PyObject* __Pyx_decode_c_string(
if
(
stop
<
0
)
if
(
stop
<
0
)
stop
+=
length
;
stop
+=
length
;
}
}
length
=
stop
-
start
;
if
(
unlikely
(
stop
<=
start
))
if
(
unlikely
(
length
<=
0
))
return
PyUnicode_FromUnicode
(
NULL
,
0
);
return
PyUnicode_FromUnicode
(
NULL
,
0
);
length
=
stop
-
start
;
cstring
+=
start
;
cstring
+=
start
;
if
(
decode_func
)
{
if
(
decode_func
)
{
return
decode_func
(
cstring
,
length
,
errors
);
return
decode_func
(
cstring
,
length
,
errors
);
...
@@ -502,9 +502,9 @@ static CYTHON_INLINE PyObject* __Pyx_decode_c_bytes(
...
@@ -502,9 +502,9 @@ static CYTHON_INLINE PyObject* __Pyx_decode_c_bytes(
}
}
if
(
stop
>
length
)
if
(
stop
>
length
)
stop
=
length
;
stop
=
length
;
length
=
stop
-
start
;
if
(
unlikely
(
stop
<=
start
))
if
(
unlikely
(
length
<=
0
))
return
PyUnicode_FromUnicode
(
NULL
,
0
);
return
PyUnicode_FromUnicode
(
NULL
,
0
);
length
=
stop
-
start
;
cstring
+=
start
;
cstring
+=
start
;
if
(
decode_func
)
{
if
(
decode_func
)
{
return
decode_func
(
cstring
,
length
,
errors
);
return
decode_func
(
cstring
,
length
,
errors
);
...
...
tests/run/bytesmethods.pyx
View file @
631475ac
cimport
cython
cimport
cython
cdef
extern
from
*
:
cdef
Py_ssize_t
PY_SSIZE_T_MIN
cdef
Py_ssize_t
PY_SSIZE_T_MAX
SSIZE_T_MAX
=
PY_SSIZE_T_MAX
SSIZE_T_MIN
=
PY_SSIZE_T_MIN
b_a
=
b'a'
b_a
=
b'a'
b_b
=
b'b'
b_b
=
b'b'
...
@@ -114,6 +122,14 @@ def bytes_decode(bytes s, start=None, stop=None):
...
@@ -114,6 +122,14 @@ def bytes_decode(bytes s, start=None, stop=None):
<BLANKLINE>
<BLANKLINE>
>>> print(bytes_decode(s, -300, -500))
>>> print(bytes_decode(s, -300, -500))
<BLANKLINE>
<BLANKLINE>
>>> print(bytes_decode(s, SSIZE_T_MIN, SSIZE_T_MIN))
<BLANKLINE>
>>> print(bytes_decode(s, SSIZE_T_MIN, SSIZE_T_MAX))
abaab
>>> print(bytes_decode(s, SSIZE_T_MAX, SSIZE_T_MIN))
<BLANKLINE>
>>> print(bytes_decode(s, SSIZE_T_MAX, SSIZE_T_MAX))
<BLANKLINE>
>>> s[:'test'] # doctest: +ELLIPSIS
>>> s[:'test'] # doctest: +ELLIPSIS
Traceback (most recent call last):
Traceback (most recent call last):
...
...
tests/run/charptr_decode.pyx
View file @
631475ac
cimport
cython
cimport
cython
cdef
extern
from
*
:
cdef
Py_ssize_t
PY_SSIZE_T_MIN
cdef
Py_ssize_t
PY_SSIZE_T_MAX
############################################################
############################################################
# tests for char* slicing
# tests for char* slicing
...
@@ -118,6 +123,19 @@ def slice_charptr_dynamic_bounds_non_name():
...
@@ -118,6 +123,19 @@ def slice_charptr_dynamic_bounds_non_name():
(
cstring
+
1
)[:].
decode
(
'UTF-8'
),
(
cstring
+
1
)[:].
decode
(
'UTF-8'
),
(
cstring
+
1
)[
return1
():
return5
()].
decode
(
'UTF-8'
))
(
cstring
+
1
)[
return1
():
return5
()].
decode
(
'UTF-8'
))
@
cython
.
test_assert_path_exists
(
"//PythonCapiCallNode"
)
@
cython
.
test_fail_if_path_exists
(
"//AttributeNode"
)
def
slice_charptr_decode_large_bounds
():
"""
>>> print(str(slice_charptr_decode_large_bounds()).replace("u'", "'"))
('abcABCqtp', '', '', '')
"""
return
(
cstring
[
PY_SSIZE_T_MIN
:
9
].
decode
(
'UTF-8'
),
cstring
[
PY_SSIZE_T_MAX
:
PY_SSIZE_T_MIN
].
decode
(
'UTF-8'
),
cstring
[
PY_SSIZE_T_MIN
:
PY_SSIZE_T_MIN
].
decode
(
'UTF-8'
),
cstring
[
PY_SSIZE_T_MAX
:
PY_SSIZE_T_MAX
].
decode
(
'UTF-8'
))
cdef
return1
():
return
1
cdef
return1
():
return
1
cdef
return3
():
return
3
cdef
return3
():
return
3
cdef
return4
():
return
4
cdef
return4
():
return
4
...
...
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