Skip to content

Commit

Permalink
fix UB compiling error
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatthieu3 committed Jul 17, 2024
1 parent ea2b86e commit 66dff0c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
11 changes: 6 additions & 5 deletions src/hdu/data/asciitable.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt::Debug;
use std::io::{BufReader, Cursor, Read};

use async_trait::async_trait;
use futures::AsyncRead;
use std::cell::UnsafeCell;
use std::fmt::Debug;
use std::io::{BufReader, Cursor, Read};

use crate::error::Error;

Expand Down Expand Up @@ -38,9 +38,10 @@ where

let bytes = &bytes[start_byte_pos..end_byte_pos];

let x_ptr = bytes as *const [u8] as *mut [u8];
let w = bytes as *const [u8] as *mut UnsafeCell<[u8]>;
unsafe {
let x_mut_ref = &mut *x_ptr;
let word: &UnsafeCell<[u8]> = &*w;
let x_mut_ref = &mut *word.get();

let (_, data, _) = x_mut_ref.align_to_mut::<u8>();
let data = &data[..num_bytes_read];
Expand Down
6 changes: 4 additions & 2 deletions src/hdu/data/bintable.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::UnsafeCell;
use std::fmt::Debug;
use std::io::{BufReader, Cursor, Read};

Expand Down Expand Up @@ -37,9 +38,10 @@ where

let bytes = &bytes[start_byte_pos..end_byte_pos];

let x_ptr = bytes as *const [u8] as *mut [u8];
let w = bytes as *const [u8] as *mut UnsafeCell<[u8]>;
unsafe {
let x_mut_ref = &mut *x_ptr;
let word: &UnsafeCell<[u8]> = &*w;
let x_mut_ref = &mut *word.get();

let (_, data, _) = x_mut_ref.align_to_mut::<u8>();
let data = &data[..num_bytes_read];
Expand Down
15 changes: 9 additions & 6 deletions src/hdu/data/image.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pub use super::Access;
use super::DataAsyncBufRead;
use crate::error::Error;
use crate::hdu::header::BitpixValue;
use async_trait::async_trait;
use byteorder::{BigEndian, ByteOrder};
use futures::AsyncRead;
use std::cell::UnsafeCell;
use std::io::{BufReader, Cursor, Read};

pub use super::Access;
use super::DataAsyncBufRead;
use crate::hdu::header::BitpixValue;

use std::fmt::Debug;

use super::iter;
Expand Down Expand Up @@ -39,9 +39,12 @@ where
let end_byte_pos = pos + num_bytes_read;

let bytes = &bytes[start_byte_pos..end_byte_pos];
let x_ptr = bytes as *const [u8] as *mut [u8];

let w = bytes as *const [u8] as *mut UnsafeCell<[u8]>;
//let x_ptr = UnsafeCell::new(bytes as *const [u8] as *mut [u8];
unsafe {
let x_mut_ref = &mut *x_ptr;
let word: &UnsafeCell<[u8]> = &*w;
let x_mut_ref = &mut *word.get();

match bitpix {
BitpixValue::U8 => {
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ mod tests {
#[test_case("samples/fits.gsfc.nasa.gov/Astro_UIT.fits", 1, 0, 0)]
#[test_case("samples/fits.gsfc.nasa.gov/EUVE.fits", 5, 0, 4)]
#[test_case("samples/fits.gsfc.nasa.gov/HST_FGS.fits", 1, 1, 0)]
#[test_case("samples/fits.gsfc.nasa.gov/IUE_LWP.fits", 1, 0, 1)]
#[test_case("samples/misc/ngc5457K.fits", 1, 0, 0)]
#[test_case("samples/fits.gsfc.nasa.gov/HST_FOC.fits", 1, 1, 0)]
#[test_case("samples/fits.gsfc.nasa.gov/HST_FOS.fits", 1, 1, 0)]
#[test_case("samples/fits.gsfc.nasa.gov/HST_HRS.fits", 1, 1, 0)]
#[test_case("samples/fits.gsfc.nasa.gov/HST_NICMOS.fits", 6, 0, 0)]
#[test_case("samples/fits.gsfc.nasa.gov/HST_WFPC_II.fits", 1, 1, 0)]
#[test_case("samples/fits.gsfc.nasa.gov/HST_WFPC_II_bis.fits", 1, 0, 0)]
#[test_case("samples/fits.gsfc.nasa.gov/IUE_LWP.fits", 1, 0, 1)]
fn test_count_hdu(
filename: &str,
num_image_ext: usize,
Expand Down Expand Up @@ -159,7 +160,8 @@ mod tests {
match hdu.get_data() {
&InMemData::I16(data) => {
assert!(
data.len() as u64 == xtension.get_naxisn(1).unwrap() * xtension.get_naxisn(2).unwrap()
data.len() as u64
== xtension.get_naxisn(1).unwrap() * xtension.get_naxisn(2).unwrap()
)
}
_ => unreachable!(),
Expand Down Expand Up @@ -422,9 +424,9 @@ mod tests {
if xtension.get_naxis() == 2 {
let naxis1 = *xtension.get_naxisn(1).unwrap();
let naxis2 = *xtension.get_naxisn(2).unwrap();

let num_pixels = (naxis2 * naxis1) as usize;

match hdu.get_data_mut() {
stream::Data::U8(stream) => {
let data = stream.try_collect::<Vec<_>>().await.unwrap();
Expand Down Expand Up @@ -504,7 +506,7 @@ mod tests {

let it_bytes = xhdu.get_data_mut();
let data = it_bytes.collect::<Vec<_>>().await;
assert_eq!(num_bytes as usize , data.len());
assert_eq!(num_bytes as usize, data.len());
}
}

Expand Down

0 comments on commit 66dff0c

Please sign in to comment.