Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
cea6cf8a
Commit
cea6cf8a
authored
Mar 15, 2016
by
Damien George
Browse files
py/formatfloat: Fix buffer overflow when formatting tiny numbers.
parent
0d1f8868
Changes
2
Hide whitespace changes
Inline
Side-by-side
py/formatfloat.c
View file @
cea6cf8a
...
...
@@ -27,6 +27,7 @@
#include
"py/mpconfig.h"
#if MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_NONE
#include
<assert.h>
#include
<stdlib.h>
#include
<stdint.h>
#include
"py/formatfloat.h"
...
...
@@ -210,13 +211,15 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
dec
=
-
1
;
*
s
++
=
first_dig
;
if
(
prec
+
e
+
1
>
buf_remaining
)
{
prec
=
buf_remaining
-
e
-
1
;
}
if
(
org_fmt
==
'g'
)
{
prec
+=
(
e
-
1
);
}
// truncate precision to prevent buffer overflow
if
(
prec
+
2
>
buf_remaining
)
{
prec
=
buf_remaining
-
2
;
}
num_digits
=
prec
;
if
(
num_digits
)
{
*
s
++
=
'.'
;
...
...
@@ -390,6 +393,9 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
}
*
s
=
'\0'
;
// verify that we did not overrun the input buffer
assert
((
size_t
)(
s
+
1
-
buf
)
<=
buf_size
);
return
s
-
buf
;
}
...
...
tests/float/string_format_modulo.py
View file @
cea6cf8a
...
...
@@ -26,3 +26,19 @@ print("%02.3d" % 123) # prec > width
print
(
"%+f %+f"
%
(
1.23
,
-
1.23
))
# float sign
print
(
"% f % f"
%
(
1.23
,
-
1.23
))
# float space sign
print
(
"%0f"
%
-
1.23
)
# negative number with 0 padding
# numbers with large negative exponents
print
(
'%f'
%
1e-10
)
print
(
'%f'
%
1e-20
)
print
(
'%f'
%
1e-50
)
print
(
'%f'
%
1e-100
)
print
(
'%f'
%
1e-300
)
# large decimal precision should be truncated and not overflow buffer
# the output depends on the FP calculation so only first 2 digits are printed
# (the 'g' with small e are printed using 'f' style, so need to be checked)
print
((
'%.40f'
%
1e-300
)[:
2
])
print
((
'%.40g'
%
1e-1
)[:
2
])
print
((
'%.40g'
%
1e-2
)[:
2
])
print
((
'%.40g'
%
1e-3
)[:
2
])
print
((
'%.40g'
%
1e-4
)[:
2
])
Write
Preview
Supports
Markdown
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