Skip to content

Commit 39f240e

Browse files
committed
chore: fix core dump error in AsyncInlinable::read_inlined
1 parent a29d70d commit 39f240e

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

taos-query/src/common/raw/data.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,20 @@ impl crate::util::AsyncInlinable for RawData {
126126
reader: &mut R,
127127
) -> std::io::Result<Self> {
128128
use tokio::io::*;
129+
129130
let len = reader.read_u32_le().await?;
130131
let meta_type = reader.read_u16_le().await?;
131-
let mut vec: Vec<u8> = Vec::with_capacity(len as usize);
132-
reader.read_exact(&mut vec).await?;
132+
let layout = std::alloc::Layout::from_size_align(len as _, 1).map_err(|_| {
133+
std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid raw data length")
134+
})?;
135+
let ptr = unsafe { std::alloc::alloc(layout) };
136+
let buf = unsafe { std::slice::from_raw_parts_mut(ptr, len as _) };
137+
138+
// let mut vec: Vec<u8> = Vec::with_capacity(len as usize);
139+
reader.read_exact(buf).await?;
133140

134-
let ptr = Box::into_raw(vec.into_boxed_slice());
135141
let raw = raw_data_t {
136-
raw: ptr as _,
142+
raw: buf.as_ptr() as _,
137143
raw_len: len,
138144
raw_type: meta_type,
139145
};

taos/examples/raw-restore.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::time::Instant;
33

44
use clap::Parser;
55
use taos::*;
6+
use taos_query::common::RawData;
67

78
#[derive(Debug, Parser)]
89
struct Opts {
@@ -96,18 +97,15 @@ async fn main() -> anyhow::Result<()> {
9697
std::io::stdin().read_line(&mut String::new())?;
9798
}
9899
let mut file = std::fs::File::open(&path)?;
99-
let mut buf = Vec::new();
100-
let size = buf.len();
101-
file.read_to_end(&mut buf)?;
102-
let raw = RawMeta::new(bytes::Bytes::from(buf));
100+
let raw: RawMeta = Inlinable::read_inlined(&mut file)?;
103101
let instant = Instant::now();
104102
conn.write_raw_meta(&raw).await?;
105103
eprintln!("{}: write raw data from {:?} done", idx, path);
106104
println!(
107105
"{},{},{},{}",
108106
idx,
109107
path.display(),
110-
size,
108+
raw.raw_len(),
111109
instant.elapsed().as_millis()
112110
);
113111
idx += 1;
@@ -153,10 +151,8 @@ async fn main() -> anyhow::Result<()> {
153151
std::io::stdin().read_line(&mut String::new())?;
154152
}
155153
let mut file = std::fs::File::open(path)?;
156-
let mut buf = Vec::new();
157-
file.read_to_end(&mut buf)?;
158-
let size = buf.len();
159-
let raw = RawMeta::new(bytes::Bytes::from(buf));
154+
let raw: RawData = Inlinable::read_inlined(&mut file)?;
155+
let size = raw.raw_len() as usize + 6;
160156
senders[idx % opts.workers]
161157
.send((idx, path.clone(), raw, size))
162158
.await?;

taos/examples/ts5250.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::io::Read;
2-
31
use clap::Parser;
42
use taos::*;
53

@@ -51,9 +49,8 @@ async fn main() -> anyhow::Result<()> {
5149
println!("{}: press enter to write raw data from {:?}", idx, path);
5250
std::io::stdin().read_line(&mut String::new())?;
5351
let mut file = std::fs::File::open(&path)?;
54-
let mut buf = Vec::new();
55-
file.read_to_end(&mut buf)?;
56-
let raw = RawMeta::new(bytes::Bytes::from(buf));
52+
53+
let raw: RawMeta = Inlinable::read_inlined(&mut file)?;
5754
conn.write_raw_meta(&raw).await?;
5855
println!("{}: write raw data from {:?} done", idx, path);
5956
}

0 commit comments

Comments
 (0)