Skip to content
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

docs: add stdlib env::var(_os) panic #63461

Merged
merged 1 commit into from
Aug 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/libstd/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ impl fmt::Debug for VarsOs {
/// * Environment variable is not present
/// * Environment variable is not valid unicode
///
/// # Panics
///
/// This function may panic if `key` is empty, contains an ASCII equals sign

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it "may panic" or "panics" (definitive)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function relies on std::sys::os, so the behaviour differs by operating system. We could update the docs to make this clearer.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure what is the practice in this case. Maybe "may" is fine.

BTW, I do wonder why this behavior was chosen, that if key contains a nul then it panics. It makes sense for setting a var, but when getting, it makes more sense to me that it just return None. The code comment in the unix implementation seems to agree, I think..

// environment variables with a nul byte can't be set, so their value is
// always None as well
let k = CString::new(k.as_bytes())?;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another question: the function documentation says that when key is not present, an error is returned, not a panic. So is the "is empty" here correct?

/// `'='` or the NUL character `'\0'`, or when the value contains the NUL
/// character.
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -210,6 +216,12 @@ fn _var(key: &OsStr) -> Result<String, VarError> {
///
/// [`None`]: ../option/enum.Option.html#variant.None
///
/// # Panics
///
/// This function may panic if `key` is empty, contains an ASCII equals sign
/// `'='` or the NUL character `'\0'`, or when the value contains the NUL
/// character.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NUL character is repeated twice in both of these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copied verbatim from the underlying functions called, which state they may crash when either the key or the value contain a null byte.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed the key/value distinction.

///
/// # Examples
///
/// ```
Expand Down