From 8c9be62c171ed3e24f091f65b1db286f3a95e796 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sun, 28 Aug 2022 13:03:02 +0200 Subject: [PATCH 1/3] Code formatting --- src/strcpy.rs | 61 +++++++++++++++++------------------- src/strtoul.rs | 85 +++++++++++++++++++++++++------------------------- 2 files changed, 71 insertions(+), 75 deletions(-) diff --git a/src/strcpy.rs b/src/strcpy.rs index c8018eb..a902ca6 100644 --- a/src/strcpy.rs +++ b/src/strcpy.rs @@ -7,42 +7,39 @@ use crate::CChar; /// Rust implementation of C library function `strcpy`. Passing NULL /// (core::ptr::null()) gives undefined behaviour. #[no_mangle] -pub unsafe extern "C" fn strcpy( - dest: *mut CChar, - src: *const CChar, -) -> *const CChar { - let mut i = 0; - loop { - *dest.offset(i) = *src.offset(i); - let c = *dest.offset(i); - if c == 0 { - break; - } - i += 1; - } - dest +pub unsafe extern "C" fn strcpy(dest: *mut CChar, src: *const CChar) -> *const CChar { + let mut i = 0; + loop { + *dest.offset(i) = *src.offset(i); + let c = *dest.offset(i); + if c == 0 { + break; + } + i += 1; + } + dest } #[cfg(test)] mod test { - use super::*; + use super::*; - #[test] - fn short() { - let src = b"hi\0"; - let mut dest = *b"abcdef"; // no null terminator - let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) }; - assert_eq!( - unsafe { core::slice::from_raw_parts(result, 5) }, - *b"hi\0de" - ); - } + #[test] + fn short() { + let src = b"hi\0"; + let mut dest = *b"abcdef"; // no null terminator + let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) }; + assert_eq!( + unsafe { core::slice::from_raw_parts(result, 5) }, + *b"hi\0de" + ); + } - #[test] - fn two() { - let src = b"hi\0"; - let mut dest = [0u8; 2]; // no space for null terminator - let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) }; - assert_eq!(unsafe { core::slice::from_raw_parts(result, 2) }, b"hi"); - } + #[test] + fn two() { + let src = b"hi\0"; + let mut dest = [0u8; 2]; // no space for null terminator + let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) }; + assert_eq!(unsafe { core::slice::from_raw_parts(result, 2) }, b"hi"); + } } diff --git a/src/strtoul.rs b/src/strtoul.rs index 53d57ee..e1587d1 100644 --- a/src/strtoul.rs +++ b/src/strtoul.rs @@ -3,7 +3,7 @@ //! Copyright (c) Jonathan 'theJPster' Pallant 2019 //! Licensed under the Blue Oak Model Licence 1.0.0 -use crate::{CChar, CULong, CStringIter}; +use crate::{CChar, CStringIter, CULong}; /// Rust implementation of C library function `strtoul`. /// @@ -13,56 +13,55 @@ use crate::{CChar, CULong, CStringIter}; /// function returns zero. #[no_mangle] pub unsafe extern "C" fn strtoul(s: *const CChar) -> CULong { - let mut result: CULong = 0; - for c in CStringIter::new(s) { - if c >= b'0' && c <= b'9' { - result *= 10; - result += (c - b'0') as CULong; - } else { - break; - } - } - result + let mut result: CULong = 0; + for c in CStringIter::new(s) { + if c >= b'0' && c <= b'9' { + result *= 10; + result += (c - b'0') as CULong; + } else { + break; + } + } + result } #[cfg(test)] mod test { - use super::strtoul; + use super::strtoul; - #[test] - fn empty() { - let result = unsafe { strtoul(b"\0".as_ptr()) }; - assert_eq!(result, 0); - } + #[test] + fn empty() { + let result = unsafe { strtoul(b"\0".as_ptr()) }; + assert_eq!(result, 0); + } - #[test] - fn non_digit() { - let result = unsafe { strtoul(b"1234x\0".as_ptr()) }; - assert_eq!(result, 1234); - } + #[test] + fn non_digit() { + let result = unsafe { strtoul(b"1234x\0".as_ptr()) }; + assert_eq!(result, 1234); + } - #[test] - fn bad_number() { - let result = unsafe { strtoul(b"x\0".as_ptr()) }; - assert_eq!(result, 0); - } + #[test] + fn bad_number() { + let result = unsafe { strtoul(b"x\0".as_ptr()) }; + assert_eq!(result, 0); + } - #[test] - fn one() { - let result = unsafe { strtoul(b"1\0".as_ptr()) }; - assert_eq!(result, 1); - } + #[test] + fn one() { + let result = unsafe { strtoul(b"1\0".as_ptr()) }; + assert_eq!(result, 1); + } - #[test] - fn hundredish() { - let result = unsafe { strtoul(b"123\0".as_ptr()) }; - assert_eq!(result, 123); - } - - #[test] - fn big_long() { - let result = unsafe { strtoul(b"2147483647\0".as_ptr()) }; - assert_eq!(result, 2147483647); - } + #[test] + fn hundredish() { + let result = unsafe { strtoul(b"123\0".as_ptr()) }; + assert_eq!(result, 123); + } + #[test] + fn big_long() { + let result = unsafe { strtoul(b"2147483647\0".as_ptr()) }; + assert_eq!(result, 2147483647); + } } From 1d5e13170ae243fb07b62821fcd981509e75b626 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sun, 28 Aug 2022 13:31:49 +0200 Subject: [PATCH 2/3] Always use C99 standard --- build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/build.rs b/build.rs index e2415ba..ab872d2 100644 --- a/build.rs +++ b/build.rs @@ -5,6 +5,7 @@ fn main() { cc::Build::new() .warnings(true) .extra_warnings(true) + .flag("-std=c99") .file("./src/snprintf.c") .compile("clocal"); } From 0b6c8089d06df73f83a2116c729a5c5642292043 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Tue, 18 Oct 2022 08:31:25 +0200 Subject: [PATCH 3/3] Add CI --- .github/workflows/build.yml | 111 ++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..59e1c61 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,111 @@ +on: + push: + branches: [ staging, trying, master ] + pull_request: + +name: Build + +#env: +# RUSTFLAGS: '--deny warnings' # clippy generates warnings at the moment + +jobs: + build: + runs-on: ubuntu-latest + env: + RUSTFLAGS: '--deny warnings' + strategy: + matrix: + rust: [stable] + TARGET: + - x86_64-unknown-linux-gnu + - x86_64-unknown-linux-musl + - arm-unknown-linux-gnueabi # Raspberry Pi 1 + - armv7-unknown-linux-gnueabihf # Raspberry Pi 2, 3, etc + # Bare metal + - thumbv6m-none-eabi + - thumbv7em-none-eabi + - thumbv7em-none-eabihf + - thumbv7m-none-eabi + + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.rust }} + target: ${{ matrix.TARGET }} + override: true + - uses: actions-rs/cargo@v1 + with: + use-cross: true + command: build + args: --target=${{ matrix.TARGET }} + + test: + name: Tests + env: + RUSTFLAGS: '--deny warnings' + runs-on: ubuntu-latest + strategy: + matrix: + rust: [stable, beta] + TARGET: [x86_64-unknown-linux-gnu] + + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.rust }} + target: ${{ matrix.TARGET }} + override: true + + - name: Test + uses: actions-rs/cargo@v1 + with: + use-cross: true + command: test + args: --target=${{ matrix.TARGET }} + + docs: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: rustfmt + - uses: actions-rs/cargo@v1 + with: + command: doc + + fmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: rustfmt + - uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check + + clippy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: 1.64.0 # clippy is too much of a moving target at the moment + override: true + components: clippy + - uses: actions-rs/clippy-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }}