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

Various safety improvements outside of the main library #38

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
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
34 changes: 14 additions & 20 deletions bzip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use libc::{
_exit, close, exit, fclose, fdopen, ferror, fflush, fgetc, fileno, fopen, fread, fwrite, open,
perror, remove, rewind, signal, stat, strcat, strcmp, strlen, strncpy, ungetc, utimbuf, write,
FILE,
FILE, SIGBUS, SIGHUP, SIGINT, SIGSEGV, SIGTERM,
};
extern "C" {
static mut stdin: *mut FILE;
Expand Down Expand Up @@ -469,7 +469,7 @@
if noisy {
eprintln!(
"\n{}: {}: trailing garbage after EOF ignored",
CStr::from_ptr(progName).to_string_lossy(),
get_program_name().display(),
CStr::from_ptr(inName.as_ptr()).to_string_lossy(),
);
}
Expand Down Expand Up @@ -695,10 +695,6 @@
exit(exitValue);
}

unsafe fn panic(s: *const c_char) -> ! {
panic_str(&CStr::from_ptr(s).to_string_lossy())
}

unsafe fn panic_str(s: &str) -> ! {
eprint!(
concat!(
Expand All @@ -708,7 +704,7 @@
"\tThis is a BUG. Please report it at:\n",
"\thttps://github.com/trifectatechfoundation/libbzip2-rs/issues\n"
),
CStr::from_ptr(progName).to_string_lossy(),
get_program_name().display(),

Check warning on line 707 in bzip2.rs

View check run for this annotation

Codecov / codecov/patch

bzip2.rs#L707

Added line #L707 was not covered by tests
s,
);
showFileNames();
Expand All @@ -718,7 +714,7 @@
unsafe fn crcError() -> ! {
eprintln!(
"\n{}: Data integrity error when decompressing.",
CStr::from_ptr(progName).to_string_lossy(),
get_program_name().display(),
);
showFileNames();
cadvise();
Expand All @@ -733,7 +729,7 @@
"{}: Compressed file ends unexpectedly;\n",
"\tperhaps it is corrupted? *Possible* reason follows.\n"
),
CStr::from_ptr(progName).to_string_lossy(),
get_program_name().display(),
);
perror(progName);
showFileNames();
Expand All @@ -744,7 +740,7 @@
unsafe fn ioError() -> ! {
eprintln!(
"\n{}: I/O or other error, bailing out. Possible reason follows.",
CStr::from_ptr(progName).to_string_lossy(),
get_program_name().display(),
);
perror(progName);
showFileNames();
Expand Down Expand Up @@ -850,7 +846,7 @@
unsafe fn outOfMemory() -> ! {
eprintln!(
"\n{}: couldn't allocate enough memory",
CStr::from_ptr(progName).to_string_lossy(),
get_program_name().display(),

Check warning on line 849 in bzip2.rs

View check run for this annotation

Codecov / codecov/patch

bzip2.rs#L849

Added line #L849 was not covered by tests
);
showFileNames();
cleanUpAndFail(1 as libc::c_int);
Expand Down Expand Up @@ -1083,7 +1079,7 @@
let mut statBuf: stat = zeroed();
delete_output_on_interrupt = false;
if name.is_null() && srcMode != SourceMode::I2O {
panic(b"compress: bad modes\n\0" as *const u8 as *const libc::c_char);
panic_str("compress: bad modes\n");

Check warning on line 1082 in bzip2.rs

View check run for this annotation

Codecov / codecov/patch

bzip2.rs#L1082

Added line #L1082 was not covered by tests
}
match srcMode {
SourceMode::I2O => {
Expand Down Expand Up @@ -1375,9 +1371,7 @@
let name_cstr = CString::new(name).unwrap();
copyFileName(outName.as_mut_ptr(), name_cstr.as_ptr());
}
(None, SourceMode::F2O | SourceMode::F2F) => {
panic(b"uncompress: bad modes\n\0" as *const u8 as *const libc::c_char)
}
(None, SourceMode::F2O | SourceMode::F2F) => panic_str("uncompress: bad modes\n"),

Check warning on line 1374 in bzip2.rs

View check run for this annotation

Codecov / codecov/patch

bzip2.rs#L1374

Added line #L1374 was not covered by tests
}

if srcMode != SourceMode::I2O && contains_dubious_chars_safe(&in_name) {
Expand Down Expand Up @@ -1812,11 +1806,11 @@
exitValue = 0;

signal(
11,
SIGSEGV,
mySIGSEGVorSIGBUScatcher as unsafe extern "C" fn(libc::c_int) as usize,
);
signal(
7,
SIGBUS,
mySIGSEGVorSIGBUScatcher as unsafe extern "C" fn(libc::c_int) as usize,
);

Expand Down Expand Up @@ -1985,15 +1979,15 @@
}
if srcMode == SourceMode::F2F {
signal(
2,
SIGINT,
mySignalCatcher as unsafe extern "C" fn(IntNative) as usize,
);
signal(
15,
SIGTERM,
mySignalCatcher as unsafe extern "C" fn(IntNative) as usize,
);
signal(
1,
SIGHUP,
mySignalCatcher as unsafe extern "C" fn(IntNative) as usize,
);
}
Expand Down
Loading
Loading