Skip to content

Commit

Permalink
Int parsing bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Codetector1374 committed Aug 5, 2024
1 parent 1de7242 commit 60f3b90
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/bin/page_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ impl PageExplorer {
BufReader::new(File::open(&self.arguments.file).expect("Can't open page file"));
let mut buffer = Box::<[u8]>::from([0u8; FIL_PAGE_SIZE]);
let mut counter = 0usize;
let mut index_counter = 0usize;

if let Some(output) = &self.arguments.output {
let file = File::create(output).expect("Can't open output file for write");
Expand All @@ -214,6 +215,9 @@ impl PageExplorer {
break;
}
let page = Page::from_bytes(&buffer).unwrap();
if page.header.page_type == PageType::Index {
index_counter += 1;
}
if let Some(page_id) = self.arguments.page_id {
if page.header.offset != page_id {
continue;
Expand All @@ -226,7 +230,7 @@ impl PageExplorer {
}

if let Some(limit) = self.arguments.limit {
if counter >= limit {
if index_counter >= limit {
info!("Exiting early due to --limit argument");
break;
}
Expand Down
38 changes: 31 additions & 7 deletions src/innodb/table/field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::u64;

use crate::innodb::charset::InnoDBCharset;
use tracing::trace;
use tracing::{info, trace};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldType {
Expand Down Expand Up @@ -80,13 +80,22 @@ impl Field {
if signed {
let numeric_value = num & ((1u64 << (len * 8 - 1)) - 1);
let is_positive = (num & (1u64 << (len * 8 - 1))) != 0;
FieldValue::SignedInt(if is_positive {
numeric_value as i64
let mask = if is_positive {
0u64
} else {
-(numeric_value as i64)
})
u64::MAX << (len * 8 - 1)
};
let signed_value = (numeric_value | mask) as i64;
if signed_value == -127 && len == 1 {
info!(
"DBG: numeric_value: {:#x}, pos: {}",
numeric_value, is_positive
);
}
FieldValue::SignedInt(signed_value)
} else {
FieldValue::UnsignedInt(num & !(u64::MAX << (len * 8)))
assert!(len == 8 || num < (1 << (len * 8)));
FieldValue::UnsignedInt(num)
}
}

Expand Down Expand Up @@ -140,7 +149,7 @@ mod test {
use super::{Field, FieldType};

#[test]
fn test_field_parse_int() {
fn test_field_parse_medium_int() {
let buf = [0x80, 0x00, 0x00];
let field = Field {
name: Default::default(),
Expand All @@ -153,4 +162,19 @@ mod test {
_ => unreachable!(),
}
}

#[test]
fn test_field_parse_tiny_int() {
let buf = [0x7F];
let field = Field {
name: Default::default(),
field_type: FieldType::TinyInt(true),
nullable: false,
};
let result = field.parse_int(&buf, 1, true);
match result {
super::FieldValue::SignedInt(val) => assert_eq!(val, -1),
_ => unreachable!(),
}
}
}

0 comments on commit 60f3b90

Please sign in to comment.