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
76038810
Commit
76038810
authored
Jun 23, 2013
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #18137: Detect integer overflow on precision in float.__format__()
and complex.__format__().
parent
223a22b6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
2 deletions
+34
-2
Lib/test/test_format.py
Lib/test/test_format.py
+17
-0
Misc/NEWS
Misc/NEWS
+3
-0
Objects/stringlib/formatter.h
Objects/stringlib/formatter.h
+14
-2
No files found.
Lib/test/test_format.py
View file @
76038810
...
...
@@ -302,6 +302,23 @@ class FormatTest(unittest.TestCase):
def
test_main
():
test_support
.
run_unittest
(
FormatTest
)
def
test_precision
(
self
):
INT_MAX
=
2147483647
f
=
1.2
self
.
assertEqual
(
format
(
f
,
".0f"
),
"1"
)
self
.
assertEqual
(
format
(
f
,
".3f"
),
"1.200"
)
with
self
.
assertRaises
(
ValueError
)
as
cm
:
format
(
f
,
".%sf"
%
(
INT_MAX
+
1
))
self
.
assertEqual
(
str
(
cm
.
exception
),
"precision too big"
)
c
=
complex
(
f
)
self
.
assertEqual
(
format
(
f
,
".0f"
),
"1"
)
self
.
assertEqual
(
format
(
f
,
".3f"
),
"1.200"
)
with
self
.
assertRaises
(
ValueError
)
as
cm
:
format
(
f
,
".%sf"
%
(
INT_MAX
+
1
))
self
.
assertEqual
(
str
(
cm
.
exception
),
"precision too big"
)
if
__name__
==
"__main__"
:
unittest
.
main
()
Misc/NEWS
View file @
76038810
...
...
@@ -9,6 +9,9 @@ What's New in Python 2.7.6?
Core and Builtins
-----------------
- Issue #18137: Detect integer overflow on precision in float.__format__()
and complex.__format__().
- Issue #18038: SyntaxError raised during compilation sources with illegal
encoding now always contains an encoding name.
...
...
Objects/stringlib/formatter.h
View file @
76038810
...
...
@@ -928,7 +928,7 @@ format_float_internal(PyObject *value,
Py_ssize_t
n_total
;
int
has_decimal
;
double
val
;
Py_ssize_t
precision
=
format
->
precision
;
Py_ssize_t
precision
;
Py_ssize_t
default_precision
=
6
;
STRINGLIB_CHAR
type
=
format
->
type
;
int
add_pct
=
0
;
...
...
@@ -947,6 +947,12 @@ format_float_internal(PyObject *value,
from a hard-code pseudo-locale */
LocaleInfo
locale
;
if
(
format
->
precision
>
INT_MAX
)
{
PyErr_SetString
(
PyExc_ValueError
,
"precision too big"
);
goto
done
;
}
precision
=
(
int
)
format
->
precision
;
/* Alternate is not allowed on floats. */
if
(
format
->
alternate
)
{
PyErr_SetString
(
PyExc_ValueError
,
...
...
@@ -1078,7 +1084,7 @@ format_complex_internal(PyObject *value,
Py_ssize_t
n_im_total
;
int
re_has_decimal
;
int
im_has_decimal
;
Py_ssize_t
precision
=
format
->
precision
;
Py_ssize_t
precision
;
Py_ssize_t
default_precision
=
6
;
STRINGLIB_CHAR
type
=
format
->
type
;
STRINGLIB_CHAR
*
p_re
;
...
...
@@ -1107,6 +1113,12 @@ format_complex_internal(PyObject *value,
from a hard-code pseudo-locale */
LocaleInfo
locale
;
if
(
format
->
precision
>
INT_MAX
)
{
PyErr_SetString
(
PyExc_ValueError
,
"precision too big"
);
goto
done
;
}
precision
=
(
int
)
format
->
precision
;
/* Alternate is not allowed on complex. */
if
(
format
->
alternate
)
{
PyErr_SetString
(
PyExc_ValueError
,
...
...
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