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

Limit length of metadata decode debug output #19043

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 19 additions & 7 deletions src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ use middle::ty;
use std::rc::Rc;
use std::str;
use std::string::String;
use std::cmp;
use syntax::abi;
use syntax::ast;
use syntax::ast::*;
use syntax::parse::token;

static DATA_TRUNCATE : uint = 32;

// Compact string representation for ty::t values. API ty_str &
// parse_from_str. Extra parameters are for converting to/from def_ids in the
// data buffer. Whatever format you choose should not contain pipe characters.
Expand Down Expand Up @@ -123,17 +126,26 @@ pub fn parse_state_from_data<'a, 'tcx>(data: &'a [u8], crate_num: ast::CrateNum,
}
}

fn data_log_string(data: &[u8], pos: uint) -> String {
fn data_log_string(data: &[u8], pos: uint, max: uint) -> String {
let mut buf = String::new();
let end = match max {
Copy link
Member

Choose a reason for hiding this comment

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

  1. You haven't documented anywhere this special handing of max == 0; (I personally would rather not treat 0 as a special case, and instead pass in max: Option<uint>, but reasonable people can differ on this sort of detail.) It might be a good idea to note the special case in a note above DATA_TRUNCATE.
  2. More important: since every call to data_log_string is passing in DATA_TRUNCATE, and data_log_string is private to this module, why make it a parameter at all? Why not just use DATA_TRUNCATE directly in data_log_string ?

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I changed my mind; I am fine with making the cap a parameter to data_log_string as you did.

0u => data.len(),
x => cmp::min( pos + x, data.len() )
};
buf.push_str("<<");
for i in range(pos, data.len()) {
for i in range(pos, end) {
let c = data[i];
if c > 0x20 && c <= 0x7F {
buf.push(c as char);
} else {
buf.push('.');
}
}
if end < data.len() {
buf.push_str(
format!( " ({} more bytes)", data.len() - end ).as_slice()
);
}
buf.push_str(">>");
buf
}
Expand All @@ -150,35 +162,35 @@ pub fn parse_ty_closure_data(data: &[u8],

pub fn parse_ty_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
conv: conv_did) -> ty::t {
debug!("parse_ty_data {}", data_log_string(data, pos));
debug!("parse_ty_data {}", data_log_string(data, pos, DATA_TRUNCATE));
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_ty(&mut st, conv)
}

pub fn parse_region_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
conv: conv_did) -> ty::Region {
debug!("parse_region_data {}", data_log_string(data, pos));
debug!("parse_region_data {}", data_log_string(data, pos, DATA_TRUNCATE));
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_region(&mut st, conv)
}

pub fn parse_bare_fn_ty_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
conv: conv_did) -> ty::BareFnTy {
debug!("parse_bare_fn_ty_data {}", data_log_string(data, pos));
debug!("parse_bare_fn_ty_data {}", data_log_string(data, pos, DATA_TRUNCATE));
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_bare_fn_ty(&mut st, conv)
}

pub fn parse_trait_ref_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
conv: conv_did) -> ty::TraitRef {
debug!("parse_trait_ref_data {}", data_log_string(data, pos));
debug!("parse_trait_ref_data {}", data_log_string(data, pos, DATA_TRUNCATE));
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_trait_ref(&mut st, conv)
}

pub fn parse_substs_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: &ty::ctxt,
conv: conv_did) -> subst::Substs {
debug!("parse_substs_data {}", data_log_string(data, pos));
debug!("parse_substs_data {}", data_log_string(data, pos, DATA_TRUNCATE));
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_substs(&mut st, conv)
}
Expand Down