pub fn read_c_string(self, buffer: &mut [u8]) -> Result<CStr, Error> {

        debug!("read_c_string({:?}, {:p} x {})", self, buffer, buffer.len());

        let min_vaddr = self.0 as usize;
        let min_vaddr_page = min_vaddr & !(PAGE_SIZE - 1);

        let mut size = 0;

        // SAFETY: reading the current pageset is always safe
        let pageset_ref = unsafe {
            current_pageset().expect("paging not initialized")
        };
        let pageset = pageset_ref.lock();

        let mut iter = pageset.from(min_vaddr_page);

        let mut page_base = min_vaddr_page;

        while let Some(Some((_, page_type))) = iter.next() {
            // Check that page is accessible.
            if page_type.is_user() {
                for vaddr in min_vaddr + size .. page_base + PAGE_SIZE {
                    if buffer.len() <= size {
                        return Err(Error::BufferTooSmall);
                    }

                    // SAFETY: we validated that the page is mapped, u8 does not
                    // have invalid data or alignment restrictions, and we hold
                    // the exclusive lock on the pageset so it shouldn't change
                    // from under us.
                    let byte = unsafe {
                        *(vaddr as *const u8)
                    };

                    buffer[size] = byte;

                    size += 1;

                    if byte == 0 {
                        // End of C string
                        return Ok(CStr::new(&buffer[0..size]));
                    }
                }
            } else {
                return Err(Error::InaccessiblePage);
            }

            page_base += PAGE_SIZE;
        }

        // Fall-through to inaccessible page error.
        Err(Error::InaccessiblePage)
    }
/// Read a C string from user memory safely.
Read a C string from user memory safely.
Read a C string from user memory safely .
pub fn read_c_string(self, buffer: &mut [u8]) -> Result<CStr, Error> {
        debug!("read_c_string({:?}, {:p} x {})", self, buffer, buffer.len());
        let min_vaddr = self.0 as usize;
        let min_vaddr_page = min_vaddr & !(PAGE_SIZE - 1);
        let mut size = 0;
        // SAFETY: reading the current pageset is always safe
        let pageset_ref = unsafe {
            current_pageset().expect("paging not initialized")
        };
        let pageset = pageset_ref.lock();
        let mut iter = pageset.from(min_vaddr_page);
        let mut page_base = min_vaddr_page;
        while let Some(Some((_, page_type))) = iter.next() {
            // Check that page is accessible.
            if page_type.is_user() {
                for vaddr in min_vaddr + size .. page_base + PAGE_SIZE {
                    if buffer.len() <= size {
                        return Err(Error::BufferTooSmall);
                    }
                    // SAFETY: we validated that the page is mapped, u8 does not
                    // have invalid data or alignment restrictions, and we hold
                    // the exclusive lock on the pageset so it shouldn't change
                    // from under us.
                    let byte = unsafe {
                        *(vaddr as *const u8)
                    };
                    buffer[size] = byte;
                    size += 1;
                    if byte == 0 {
                        // End of C string
                        return Ok(CStr::new(&buffer[0..size]));
                    }
                }
            } else {
                return Err(Error::InaccessiblePage);
            }
            page_base += PAGE_SIZE;
        }
        // Fall-through to inaccessible page error.
        Err(Error::InaccessiblePage)
    }
pub fn read_c_string ( self , buffer : & mut [ u8 ] ) -> Result < CStr , Error > { debug ! ( " " self buffer buffer len ( ) ) ; let min_vaddr = self . 0 as usize ; let min_vaddr_page = min_vaddr & ! ( PAGE_SIZE - 1 ) ; let mut size = 0 ; // SAFETY: reading the current pageset is always safe let pageset_ref = unsafe { current_pageset ( ) . expect ( " " ) } ; let pageset = pageset_ref . lock ( ) ; let mut iter = pageset . from ( min_vaddr_page ) ; let mut page_base = min_vaddr_page ; while let Some ( Some ( ( _ , page_type ) ) ) = iter . next ( ) { // Check that page is accessible. if page_type . is_user ( ) { for vaddr in min_vaddr + size .. page_base + PAGE_SIZE { if buffer . len ( ) <= size { return Err ( Error :: BufferTooSmall ) ; } // SAFETY: we validated that the page is mapped, u8 does not // have invalid data or alignment restrictions, and we hold // the exclusive lock on the pageset so it shouldn't change // from under us. let byte = unsafe { * ( vaddr as * const u8 ) } ; buffer [ size ] = byte ; size += 1 ; if byte == 0 { // End of C string return Ok ( CStr :: new ( & buffer [ 0 .. size ] ) ) ; } } } else { return Err ( Error :: InaccessiblePage ) ; } page_base += PAGE_SIZE ; } // Fall-through to inaccessible page error. Err ( Error :: InaccessiblePage ) }
Read a C string from user memory safely.
Read a C string from user memory safely .
self
buffer
&mut [u8]