Skip to content

Commit

Permalink
fix: Fix bounds checking for JanetArray::insert
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed Nov 20, 2024
1 parent 4d4a730 commit ad98317
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused_imports)]

#[doc(hidden)]
#[macro_export]
macro_rules! count {
Expand Down Expand Up @@ -479,6 +481,19 @@ macro_rules! assert_deep_ne {
}};
}

pub(crate) use array;
pub(crate) use assert_deep_eq;
pub(crate) use assert_deep_ne;
pub(crate) use bad_slot;
pub(crate) use count;
pub(crate) use jcatch;
pub(crate) use jpanic;
#[cfg(feature = "std")]
pub(crate) use jtry;
pub(crate) use structs;
pub(crate) use table;
pub(crate) use tuple;

#[cfg(all(test, any(feature = "amalgation", feature = "link-system")))]
mod tests {
// use super::*;
Expand Down
5 changes: 4 additions & 1 deletion src/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ impl<'data> JanetArray<'data> {
/// ```
#[cfg_attr(feature = "inline-more", inline)]
pub fn insert(&mut self, index: i32, element: impl Into<Janet>) {
if index < 0 || index > self.len() + 1 {
if index < 0 || index > self.len() {
crate::jpanic!(
"insertion index (is {}) should be >= 0 and <= {})",
index,
Expand Down Expand Up @@ -3704,13 +3704,16 @@ mod tests {
assert_eq!(array.len(), 4);
assert_eq!(array[1], &Janet::integer(2));
assert_eq!(array[2], &Janet::integer(3));
assert_eq!(array[3], &Janet::integer(4));

array.insert(1, 10);

assert_eq!(array.len(), 5);
assert_eq!(array[1], &Janet::integer(10));
assert_eq!(array[2], &Janet::integer(2));
assert_eq!(array[3], &Janet::integer(3));
assert_eq!(array[4], &Janet::integer(4));

Ok(())
}

Expand Down

0 comments on commit ad98317

Please sign in to comment.