-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mpf fixups #24
base: master
Are you sure you want to change the base?
Mpf fixups #24
Conversation
bryant
commented
Jun 5, 2017
- self doesn't need to be mut when get_str
- impl Display and Debug for Mpf
Don’t you think the |
src/mpf.rs
Outdated
/// Due to the way `Mpf::get_str` works, the output contains an implicit | ||
/// radix point to the left of the first digit. `3.14`, for instance, will | ||
/// print as `314e1` which means `0.314e1`. | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to duplicate code; you can just delegate to fmt::Display::fmt(&self, f)
.
src/mpf.rs
Outdated
let c_str = CString::new("").unwrap(); | ||
let out; | ||
unsafe{ | ||
out = CString::from_raw(__gmpf_get_str(c_str.into_raw(), exp, base, n_digits, &mut self.mpf)); | ||
out = CString::from_raw(__gmpf_get_str(c_str.into_raw(), exp, base, n_digits, &self.mpf)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your tests have exposed the (pre-existing) bug that this is a blatant buffer overflow of c_str
. The mpf_get_str
documentation says “The area at str
has to have space for the largest possible number represented by a s1n
long limb array, plus one extra character.”
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not seeing a quick and easy entry point to calculate the number of digits. the easiest way would be to let gmp_allocate_func do the work.
from https://gmplib.org/manual/Converting-Floats.html : `Function: char * mpf_get_str (char *str, mp_exp_t *expptr, int base, size_t n_digits, const mpf_t op)`
will rebase after #25 is merged. |
src/mpf.rs
Outdated
impl fmt::Display for Mpf { | ||
/// Due to the way `Mpf::get_str` works, the output contains an implicit | ||
/// radix point to the left of the first digit. `3.14`, for instance, will | ||
/// print as `314e1` which means `0.314e1`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment needs to be updated from below.
this incorrectly created an uninitialized Vec, and using uninitialized isn't worth it anyway.
4e3c307
to
f248285
Compare