Skip to content

Commit e9915f3

Browse files
committed
Use less raw pointers in aux
1 parent 9aa6444 commit e9915f3

3 files changed

Lines changed: 41 additions & 31 deletions

File tree

crates/engine_bibtex/src/auxi.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ use crate::{
1212
log_pr_bst_name, print_a_pool_str, print_a_token, print_aux_name, print_bib_name,
1313
print_confusion, print_overflow, write_log_file, write_logs, AuxTy,
1414
},
15-
peekable::{peekable_close, peekable_open, PeekableInput},
15+
peekable::{peekable_open, PeekableInput},
1616
pool::StringPool,
1717
scan::Scan,
1818
AuxNumber, Bibtex, BibtexError, GlobalItems, StrIlk, StrNumber,
1919
};
20-
use std::{cell::RefCell, ffi::CString, ptr, ptr::NonNull};
20+
use std::{cell::RefCell, ffi::CString, ptr::NonNull};
2121
use tectonic_bridge_core::FileFormat;
2222

2323
const AUX_STACK_SIZE: usize = 20;
2424

2525
pub(crate) struct AuxData {
2626
aux_list: [StrNumber; AUX_STACK_SIZE + 1],
27-
aux_file: [*mut PeekableInput; AUX_STACK_SIZE + 1],
27+
aux_file: [Option<Box<PeekableInput>>; AUX_STACK_SIZE + 1],
2828
aux_ln_stack: [i32; AUX_STACK_SIZE + 1],
2929
aux_ptr: AuxNumber,
3030
}
@@ -33,7 +33,7 @@ impl AuxData {
3333
fn new() -> AuxData {
3434
AuxData {
3535
aux_list: [0; AUX_STACK_SIZE + 1],
36-
aux_file: [ptr::null_mut(); AUX_STACK_SIZE + 1],
36+
aux_file: [(); AUX_STACK_SIZE + 1].map(|_| None),
3737
aux_ln_stack: [0; AUX_STACK_SIZE + 1],
3838
aux_ptr: 0,
3939
}
@@ -55,12 +55,19 @@ impl AuxData {
5555
self.aux_list[self.aux_ptr] = num;
5656
}
5757

58-
pub fn file_at_ptr(&self) -> *mut PeekableInput {
59-
self.aux_file[self.aux_ptr]
58+
pub fn file_at_ptr(&mut self) -> &mut PeekableInput {
59+
self.aux_file[self.aux_ptr].as_mut().unwrap()
6060
}
6161

62-
pub fn set_file_at_ptr(&mut self, file: *mut PeekableInput) {
63-
self.aux_file[self.aux_ptr] = file;
62+
pub fn set_file_at_ptr(&mut self, file: Box<PeekableInput>) {
63+
self.aux_file[self.aux_ptr] = Some(file);
64+
}
65+
66+
pub fn pop_file(&mut self) -> (Box<PeekableInput>, bool) {
67+
let out = self.aux_file[self.aux_ptr].take().unwrap();
68+
let last = self.aux_ptr == 0;
69+
self.aux_ptr = self.aux_ptr.saturating_sub(1);
70+
(out, last)
6471
}
6572

6673
pub fn ln_at_ptr(&self) -> i32 {
@@ -398,15 +405,19 @@ fn aux_input_command(
398405

399406
let name = pool.get_str(aux.at_ptr());
400407
let fname = CString::new(name).unwrap();
401-
let ptr = peekable_open(ctx, &fname, FileFormat::Tex);
402-
if ptr.is_null() {
403-
write_logs("I couldn't open auxiliary file ");
404-
print_aux_name(aux, pool)?;
405-
aux.set_ptr(aux.ptr() - 1);
406-
aux_err_print(buffers, aux, pool)?;
407-
return Ok(());
408+
let file = PeekableInput::open(ctx, &fname, FileFormat::Tex);
409+
match file {
410+
Err(_) => {
411+
write_logs("I couldn't open auxiliary file ");
412+
print_aux_name(aux, pool)?;
413+
aux.set_ptr(aux.ptr() - 1);
414+
aux_err_print(buffers, aux, pool)?;
415+
return Ok(());
416+
}
417+
Ok(file) => {
418+
aux.set_file_at_ptr(file);
419+
}
408420
}
409-
aux.set_file_at_ptr(ptr);
410421

411422
write_logs(&format!("A level-{} auxiliary file: ", aux.ptr()));
412423
log_pr_aux_name(aux, pool)?;
@@ -474,15 +485,9 @@ pub(crate) fn get_aux_command_and_process(
474485
}
475486

476487
pub(crate) fn pop_the_aux_stack(ctx: &mut Bibtex<'_, '_>, aux: &mut AuxData) -> bool {
477-
// SAFETY: Aux file at pointer guaranteed valid at this point
478-
unsafe { peekable_close(ctx, NonNull::new(aux.file_at_ptr())) };
479-
aux.set_file_at_ptr(ptr::null_mut());
480-
if aux.ptr() == 0 {
481-
true
482-
} else {
483-
aux.set_ptr(aux.ptr() - 1);
484-
false
485-
}
488+
let (file, last) = aux.pop_file();
489+
file.close(ctx).unwrap();
490+
last
486491
}
487492

488493
pub(crate) fn last_check_for_aux_errors(

crates/engine_bibtex/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,7 @@ pub(crate) fn inner_bibtex_main(
439439
loop {
440440
globals.aux.set_ln_at_ptr(globals.aux.ln_at_ptr() + 1);
441441

442-
if !input_ln(
443-
// SAFETY: top of aux stack guaranteed valid
444-
unsafe { globals.aux.file_at_ptr().as_mut() },
445-
globals.buffers,
446-
) {
442+
if !input_ln(Some(globals.aux.file_at_ptr()), globals.buffers) {
447443
if pop_the_aux_stack(ctx, globals.aux) {
448444
break;
449445
}
@@ -504,7 +500,7 @@ pub(crate) fn get_the_top_level_aux_file_name(
504500
return Ok(1);
505501
}
506502
};
507-
aux.set_file_at_ptr(Box::into_raw(aux_file));
503+
aux.set_file_at_ptr(aux_file);
508504

509505
set_extension(&mut path, b".blg");
510506
let log_file = CStr::from_bytes_with_nul(&path).unwrap();

crates/engine_bibtex/src/peekable.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ impl PeekableInput {
3737
}
3838
}
3939

40+
pub(crate) fn close(self, ctx: &mut Bibtex<'_, '_>) -> Result<(), BibtexError> {
41+
let err = unsafe { ttbc_input_close(ctx.engine, self.handle.as_ptr()) };
42+
if err == 0 {
43+
Ok(())
44+
} else {
45+
Err(BibtexError::Fatal)
46+
}
47+
}
48+
4049
fn getc(&mut self) -> libc::c_int {
4150
if self.peek_char != EOF {
4251
let rv = self.peek_char;

0 commit comments

Comments
 (0)