Skip to content

Commit

Permalink
Rollup merge of #78897 - hyd-dev:alloc-error-hook-newline, r=m-ou-se
Browse files Browse the repository at this point in the history
Add missing newline to error message of the default OOM hook

Currently the default OOM hook in libstd does not end the error message with a newline:
```
memory allocation of 4 bytes failedtimeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 11:     7 Aborted                 timeout --signal=KILL ${timeout} "$`@"`
```
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=030d8223eb57dfe47ef157709aa26542

This is because the `fmt::Arguments` passed to `dumb_print()` does not end with a newline. All other calls to `dumb_print()` in libstd pass a `\n`-ended `fmt::Arguments` to `dumb_print()`. For example:
https://github.com/rust-lang/rust/blob/25f6938da459a57b43bdf16ed6bdad3225b2a3ce/library/std/src/sys_common/util.rs#L18
I think the `\n` was forgotten in #51264.

This PR appends `\n` to the error string.

~~Note that I didn't add a test, because I didn't find tests for functions in ` library/std/src/alloc.rs` or a test that is similar to the test of this change would be.~~ *Edit: CI told me there is an existing test. Sorry.*
  • Loading branch information
jonas-schievink authored Nov 10, 2020
2 parents 87ecb0a + 70e175b commit 354098c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion library/std/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub fn take_alloc_error_hook() -> fn(Layout) {
}

fn default_alloc_error_hook(layout: Layout) {
dumb_print(format_args!("memory allocation of {} bytes failed", layout.size()));
dumb_print(format_args!("memory allocation of {} bytes failed\n", layout.size()));
}

#[cfg(not(test))]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/default-alloc-error-hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ fn main() {
let me = env::current_exe().unwrap();
let output = Command::new(&me).arg("next").output().unwrap();
assert!(!output.status.success(), "{:?} is a success", output.status);
assert_eq!(str::from_utf8(&output.stderr).unwrap(), "memory allocation of 42 bytes failed");
assert_eq!(str::from_utf8(&output.stderr).unwrap(), "memory allocation of 42 bytes failed\n");
}

0 comments on commit 354098c

Please sign in to comment.