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
f9b2aa2e
Commit
f9b2aa2e
authored
Nov 15, 2014
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #22193: Fixed integer overflow error in sys.getsizeof().
Fixed an error in _PySys_GetSizeOf declaration.
parent
af627902
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
5 deletions
+35
-5
Include/sysmodule.h
Include/sysmodule.h
+1
-1
Lib/test/test_sys.py
Lib/test/test_sys.py
+25
-0
Python/sysmodule.c
Python/sysmodule.c
+9
-4
No files found.
Include/sysmodule.h
View file @
f9b2aa2e
...
...
@@ -23,7 +23,7 @@ PyAPI_FUNC(void) PySys_ResetWarnOptions(void);
PyAPI_FUNC
(
void
)
PySys_AddWarnOption
(
char
*
);
PyAPI_FUNC
(
int
)
PySys_HasWarnOptions
(
void
);
PyAPI_
DATA
(
size_t
)
_PySys_GetSizeOf
(
PyObject
*
);
PyAPI_
FUNC
(
size_t
)
_PySys_GetSizeOf
(
PyObject
*
);
#ifdef __cplusplus
}
...
...
Lib/test/test_sys.py
View file @
f9b2aa2e
...
...
@@ -494,6 +494,31 @@ class SizeofTest(unittest.TestCase):
# but lists are
self
.
assertEqual
(
sys
.
getsizeof
([]),
size
(
'P PP'
)
+
gc_header_size
)
def
test_errors
(
self
):
class
BadSizeof
(
object
):
def
__sizeof__
(
self
):
raise
ValueError
self
.
assertRaises
(
ValueError
,
sys
.
getsizeof
,
BadSizeof
())
class
InvalidSizeof
(
object
):
def
__sizeof__
(
self
):
return
None
self
.
assertRaises
(
TypeError
,
sys
.
getsizeof
,
InvalidSizeof
())
sentinel
=
[
"sentinel"
]
self
.
assertIs
(
sys
.
getsizeof
(
InvalidSizeof
(),
sentinel
),
sentinel
)
class
OverflowSizeof
(
int
):
def
__sizeof__
(
self
):
return
int
(
self
)
self
.
assertEqual
(
sys
.
getsizeof
(
OverflowSizeof
(
sys
.
maxsize
)),
sys
.
maxsize
+
self
.
gc_headsize
)
with
self
.
assertRaises
(
OverflowError
):
sys
.
getsizeof
(
OverflowSizeof
(
sys
.
maxsize
+
1
))
with
self
.
assertRaises
(
ValueError
):
sys
.
getsizeof
(
OverflowSizeof
(
-
1
))
with
self
.
assertRaises
((
ValueError
,
OverflowError
)):
sys
.
getsizeof
(
OverflowSizeof
(
-
sys
.
maxsize
-
1
))
def
test_default
(
self
):
size
=
test
.
test_support
.
calcobjsize
self
.
assertEqual
(
sys
.
getsizeof
(
True
,
-
1
),
size
(
'l'
))
...
...
Python/sysmodule.c
View file @
f9b2aa2e
...
...
@@ -689,7 +689,7 @@ _PySys_GetSizeOf(PyObject *o)
{
static
PyObject
*
str__sizeof__
=
NULL
;
PyObject
*
res
=
NULL
;
size_t
size
;
Py_s
size_t
size
;
/* Make sure the type is initialized. float gets initialized late */
if
(
PyType_Ready
(
Py_TYPE
(
o
))
<
0
)
...
...
@@ -718,14 +718,19 @@ _PySys_GetSizeOf(PyObject *o)
size
=
(
size_t
)
PyInt_AsSsize_t
(
res
);
Py_DECREF
(
res
);
if
(
size
==
(
size_t
)
-
1
&&
PyErr_Occurred
())
if
(
size
==
-
1
&&
PyErr_Occurred
())
return
(
size_t
)
-
1
;
}
if
(
size
<
0
)
{
PyErr_SetString
(
PyExc_ValueError
,
"__sizeof__() should return >= 0"
);
return
(
size_t
)
-
1
;
}
/* add gc_head size */
if
(
PyObject_IS_GC
(
o
))
size
+=
sizeof
(
PyGC_Head
);
return
size
;
return
((
size_t
)
size
)
+
sizeof
(
PyGC_Head
);
return
(
size_t
)
size
;
}
static
PyObject
*
...
...
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