Skip to content

Commit

Permalink
fix clippy and update CI
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Apr 29, 2024
1 parent a6f9324 commit 3dd7d16
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 78 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ jobs:
audit:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: rust audit
uses: actions-rs/audit-check@v1
- uses: actions/checkout@v4
- uses: rustsec/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/change-status-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: covector status
Expand Down
30 changes: 7 additions & 23 deletions .github/workflows/clippy-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,17 @@ jobs:
clippy:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: install stable
uses: actions-rs/toolchain@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
override: true
components: clippy
- run: cargo clippy --release --all-targets --all-features -- -D warnings

- uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-targets --all-features -- -D warnings

fmt:
rustfmt:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: install stable
uses: actions-rs/toolchain@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt

- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
- run: cargo fmt --all -- --check
9 changes: 2 additions & 7 deletions .github/workflows/covector-version-or-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@ jobs:
successfulPublish: ${{ steps.covector.outputs.successfulPublish }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 16
check-latest: true
registry-url: 'https://registry.npmjs.org'
- uses: actions/setup-node@v4

- name: git config
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: install stable
uses: actions-rs/toolchain@v1
Expand All @@ -33,4 +33,4 @@ jobs:

- uses: actions-rs/cargo@v1
with:
command: test
command: test --release
5 changes: 4 additions & 1 deletion crates/nsis-fn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ impl Parse for NsisFn {
}
}

/// Generates a wrapper NSIS compliant dll export that calls `nsis_plugin_api::exdll_init`
/// automatically. This macro expects the function to return a `Result<(), nsis_plugin_api::Error>`
/// and will automatically push the error to NSIS stack on failure.
#[proc_macro_attribute]
pub fn nsis_fn(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
let tokens = parse_macro_input!(tokens as NsisFn);
Expand All @@ -23,7 +26,7 @@ pub fn nsis_fn(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
let block = func.block;
let attrs = func.attrs;

let new_ident = Ident::new(&format!("__{}", ident.to_string()), Span::call_site());
let new_ident = Ident::new(&format!("__{}", ident), Span::call_site());

quote! {
#[inline(always)]
Expand Down
67 changes: 51 additions & 16 deletions crates/nsis-plugin-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,21 @@ pub static mut G_STRINGSIZE: c_int = 0;
pub static mut G_VARIABLES: *mut wchar_t = core::ptr::null_mut();
pub static mut G_STACKTOP: *mut *mut stack_t = core::ptr::null_mut();

/// Initis the global variables used by NSIS functions: [`push`], [`pushstr`], [`pushint`], [`pop`], [`popstr`] and [`popint`]
///
/// # Safety
///
/// This function mutates static variables and should only be called in a function
#[inline(always)]
pub unsafe fn exdll_init(string_size: c_int, variables: *mut wchar_t, stacktop: *mut *mut stack_t) {
G_STRINGSIZE = string_size;
G_VARIABLES = variables;
G_STACKTOP = stacktop;
}

pub const ONE: [u16; 2] = [49, 0];
pub const ZERO: [u16; 2] = [48, 0];
pub const NEGATIVE_ONE: [u16; 3] = [45, 49, 0];
pub const ONE: &[u16; 2] = &[49, 0];
pub const ZERO: &[u16; 2] = &[48, 0];
pub const NEGATIVE_ONE: &[u16; 3] = &[45, 49, 0];

#[derive(Debug)]
pub enum Error {
Expand All @@ -66,10 +71,15 @@ impl Error {
}
}
pub fn push_err(&self) {
let _ = unsafe { pushstr(&self.description()) };
let _ = unsafe { pushstr(self.description()) };
}
}

/// Pushes some bytes onto the NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn push(bytes: &[u16]) -> Result<(), Error> {
if G_STACKTOP.is_null() {
return Err(Error::StackIsNull);
Expand All @@ -84,16 +94,31 @@ pub unsafe fn push(bytes: &[u16]) -> Result<(), Error> {
Ok(())
}

/// Pushes a string onto the NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn pushstr(str: &str) -> Result<(), Error> {
let bytes = encode_wide(str);
let bytes = encode_utf16(str);
push(&bytes)
}

/// Pushes an integer onto the NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn pushint(int: i32) -> Result<(), Error> {
let str = int.to_string();
pushstr(&str)
}

/// Pops bytes from NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn pop() -> Result<Vec<u16>, Error> {
if G_STACKTOP.is_null() || (*G_STACKTOP).is_null() {
return Err(Error::StackIsNull);
Expand All @@ -109,28 +134,38 @@ pub unsafe fn pop() -> Result<Vec<u16>, Error> {
Ok(out)
}

/// Pops a string from NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn popstr() -> Result<String, Error> {
let bytes = pop()?;
Ok(decode_wide(&bytes))
Ok(decode_utf16_lossy(&bytes))
}

/// Pops an integer from NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn popint() -> Result<i32, Error> {
let str = popstr()?;
str.parse().map_err(|_| Error::ParseIntError)
}

pub fn encode_wide(str: &str) -> Vec<u16> {
pub fn encode_utf16(str: &str) -> Vec<u16> {
str.encode_utf16()
.chain(iter::once(0))
.collect::<Vec<u16>>()
}

pub fn decode_wide(bytes: &[u16]) -> String {
pub fn decode_utf16_lossy(bytes: &[u16]) -> String {
let bytes = bytes
.iter()
.position(|c| *c == 0)
.map(|nul| &bytes[..nul])
.unwrap_or(&bytes);
.unwrap_or(bytes);
String::from_utf16_lossy(bytes)
}

Expand Down Expand Up @@ -183,21 +218,21 @@ macro_rules! nsis_plugin {
}

#[no_mangle]
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: isize) -> *mut u8 {
let mut i = 0;
while i < n {
*dest.offset(i as isize) = *src.offset(i as isize);
*dest.offset(i) = *src.offset(i);
i += 1;
}
return dest;
}

#[no_mangle]
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: isize) -> i32 {
let mut i = 0;
while i < n {
let a = *s1.offset(i as isize);
let b = *s2.offset(i as isize);
let a = *s1.offset(i);
let b = *s2.offset(i);
if a != b {
return a as i32 - b as i32;
}
Expand All @@ -207,10 +242,10 @@ macro_rules! nsis_plugin {
}

#[no_mangle]
pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: usize) -> *mut u8 {
pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: isize) -> *mut u8 {
let mut i = 0;
while i < n {
*s.offset(i as isize) = c as u8;
*s.offset(i) = c as u8;
i += 1;
}
return s;
Expand Down
24 changes: 12 additions & 12 deletions crates/nsis-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ fn FindProcess() -> Result<(), Error> {
let name = popstr()?;

if !get_processes(&name).is_empty() {
push(&ZERO)
push(ZERO)
} else {
push(&ONE)
push(ONE)
}
}

Expand All @@ -56,15 +56,15 @@ fn FindProcessCurrentUser() -> Result<(), Error> {
.into_iter()
.any(|pid| belongs_to_user(user_sid, pid))
{
push(&ZERO)
push(ZERO)
} else {
push(&ONE)
push(ONE)
}
// Fall back to perMachine checks if we can't get current user id
} else if processes.is_empty() {
push(&ONE)
push(ONE)
} else {
push(&ZERO)
push(ZERO)
}
}

Expand All @@ -80,9 +80,9 @@ fn KillProcess() -> Result<(), Error> {
let processes = get_processes(&name);

if !processes.is_empty() && processes.into_iter().map(kill).all(|b| b) {
push(&ZERO)
push(ZERO)
} else {
push(&ONE)
push(ONE)
}
}

Expand All @@ -98,7 +98,7 @@ fn KillProcessCurrentUser() -> Result<(), Error> {
let processes = get_processes(&name);

if processes.is_empty() {
return push(&ONE);
return push(ONE);
}

let success = if let Some(user_sid) = get_sid(GetCurrentProcessId()) {
Expand All @@ -112,9 +112,9 @@ fn KillProcessCurrentUser() -> Result<(), Error> {
};

if success {
push(&ZERO)
push(ZERO)
} else {
push(&ONE)
push(ONE)
}
}

Expand Down Expand Up @@ -196,7 +196,7 @@ fn get_processes(name: &str) -> Vec<u32> {
if Process32FirstW(handle, &mut process) != 0 {
while Process32NextW(handle, &mut process) != 0 {
if current_pid != process.th32ProcessID
&& decode_wide(&process.szExeFile).to_lowercase() == name.to_lowercase()
&& decode_utf16_lossy(&process.szExeFile).to_lowercase() == name.to_lowercase()
{
processes.push(process.th32ProcessID);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/nsis-semvercompare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ fn SemverCompare() -> Result<(), Error> {
let v2 = popstr()?;

match compare(&v1, &v2) {
-1 => push(&NEGATIVE_ONE)?,
0 => push(&ZERO)?,
1 => push(&ONE)?,
-1 => push(NEGATIVE_ONE)?,
0 => push(ZERO)?,
1 => push(ONE)?,
_ => unreachable!(),
}

Expand Down
Loading

0 comments on commit 3dd7d16

Please sign in to comment.