-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Implement Show
for CString
and fix warning compiling tests for libcollections
#15859
Conversation
When moving The |
It makes sense that I guess the dependency of libcollections on |
Sadly that's why I just settled to put this in librustrt for now. |
I guess I will just remove the first commit and go with the last two |
c_str
to collections
and implement Show
for CString
Show
for CString
and fix warning compiling tests for libcollections
Done. Thanks for reviewing! |
Maybe this is bikeshedding, but... Have you considered moving |
I considered that, but currently We don't currently have much precedent for I am personally in favor of implement |
@@ -344,6 +345,12 @@ impl Collection for CString { | |||
} | |||
} | |||
|
|||
impl fmt::Show for CString { | |||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |||
write!(f, "{}", String::from_utf8_lossy(self.as_bytes_no_nul())) |
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 should be String::from_utf8_lossy(self.as_bytes_no_nul()).fmt(f)
to preserve formatting specifiers and such.
We use use `from_utf8_lossy` to convert it to a MaybeOwned string, to avoid failing in case the CString contains invalid UTF-8
Just for clarity, Additionaly, we have of course undefined behavior in case the buffer points to invalid memory and such. However, that should cause problems all over the place so I don't think it is something we have to deal with. |
I think I'm OK with a failing That said, I think we should probably revisit the @alexcrichton Do you know of a rationale for the current design? |
Currently, there is one place in the codebase where a new |
Sadly I don't know too, too much about the current design. All of your suggestions sound like a good direction to go in, however. I was more concerned about the non-utf8 contents of I suppose I am proposing this convention:
|
@alexcrichton I think that's a useful convention; we should also then emphasize that |
@aturon I have added a check for null at I guess we can also deprecate the |
@aochagavia Can you also update the doc comments to reflect this change? The |
The commits will also need a |
I have included your suggestions. Should I also include I still have a question: why does |
@aochagavia It doesn't matter whether As to |
Thanks for the link to stackoverflow! The Travis build timed out, but everything should be ok. |
The last commit seems to have deleted the |
That's pretty weird... I have fixed it now. Thanks! |
This also removes checks in other methods of `CString` Breaking changes: * `CString::new` now fails if `buf` is null. To avoid this add a check before creatng a new `CString` . * The `is_null` and `is_not_null` methods are deprecated, because a `CString` cannot be null. * Other methods which used to fail if the `CString` was null do not fail anymore [breaking-change]
This should be ok now |
let ch = if c_host.is_null() { null() } else { c_host.as_ptr() }; | ||
let cs = if c_serv.is_null() { null() } else { c_serv.as_ptr() }; | ||
let ch = if c_host.is_none() { null() } else { c_host.unwrap().as_ptr() }; | ||
let cs = if c_serv.is_none() { null() } else { c_serv.unwrap().as_ptr() }; |
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 is a use-after-free because you're unwrapping c_host
and c_serv
, destroying their contents, and then referencing them right afterwards.
No description provided.