Skip to content

Commit b3aecb0

Browse files
committed
Fix c2rust-analyze panic for variadic functions
Add support for variadic functions in the "Infer pointee types" phase of c2rust-analyze.
1 parent 7bf22e0 commit b3aecb0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

c2rust-analyze/src/analyze.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,12 @@ pub(super) fn gather_foreign_sigs<'tcx>(gacx: &mut GlobalAnalysisCtxt<'tcx>, tcx
342342

343343
let inputs = gacx.lcx.mk_slice(&inputs);
344344
let output = gacx.assign_pointer_ids_with_info(sig.output(), PointerInfo::ANNOTATED);
345-
let lsig = LFnSig { inputs, output };
345+
let c_variadic = sig.c_variadic;
346+
let lsig = LFnSig {
347+
inputs,
348+
output,
349+
c_variadic,
350+
};
346351
gacx.fn_sigs.insert(did, lsig);
347352
}
348353
}
@@ -607,8 +612,13 @@ fn run(tcx: TyCtxt) {
607612
.collect::<Vec<_>>();
608613
let inputs = gacx.lcx.mk_slice(&inputs);
609614
let output = gacx.assign_pointer_ids_with_info(sig.output(), PointerInfo::ANNOTATED);
615+
let c_variadic = sig.c_variadic;
610616

611-
let lsig = LFnSig { inputs, output };
617+
let lsig = LFnSig {
618+
inputs,
619+
output,
620+
c_variadic,
621+
};
612622
gacx.fn_sigs.insert(ldid.to_def_id(), lsig);
613623
}
614624

@@ -666,6 +676,14 @@ fn run(tcx: TyCtxt) {
666676
// TODO: set PointerInfo::ANNOTATED for the parts of the type with user annotations
667677
let lty = match mir.local_kind(local) {
668678
LocalKind::Var | LocalKind::Temp => acx.assign_pointer_ids(decl.ty),
679+
LocalKind::Arg
680+
if lsig.c_variadic && local.as_usize() - 1 == lsig.inputs.len() =>
681+
{
682+
// This is the hidden VaList<'a> argument at the end
683+
// of the argument list of a variadic function. It does not
684+
// appear in lsig.inputs, so we handle it separately here.
685+
acx.assign_pointer_ids(decl.ty)
686+
}
669687
LocalKind::Arg => {
670688
debug_assert!(local.as_usize() >= 1 && local.as_usize() <= mir.arg_count);
671689
lsig.inputs[local.as_usize() - 1]

c2rust-analyze/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ pub type LTyCtxt<'tcx> = LabeledTyCtxt<'tcx, PointerId>;
266266
pub struct LFnSig<'tcx> {
267267
pub inputs: &'tcx [LTy<'tcx>],
268268
pub output: LTy<'tcx>,
269+
pub c_variadic: bool,
269270
}
270271

271272
impl<'tcx> LFnSig<'tcx> {

0 commit comments

Comments
 (0)