From 667c34fe9246d96fd50683012c4d518e2e1a4358 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 7 Oct 2021 01:29:33 +0200 Subject: [PATCH] fix out of bounds write --- src/ant.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ant.rs b/src/ant.rs index 53130c8..47895ef 100644 --- a/src/ant.rs +++ b/src/ant.rs @@ -6,6 +6,7 @@ use macroquad::prelude::*; use macroquad::rand::RandomRange; use std::f32::consts::PI; +#[derive(Debug)] pub(crate) struct Ant { pos: Vec2, dir: f32, @@ -31,9 +32,8 @@ impl Ant { let acceleration = Vec2::new(self.dir.sin(), -self.dir.cos()); self.pos += acceleration * ANT_SPEED * frame_time; - // TODO: this is not fixing out of bounds write - self.pos.x = self.pos.x.clamp(0., MAP_WIDTH as f32); - self.pos.y = self.pos.y.clamp(0., MAP_HEIGHT as f32); + self.pos.x = self.pos.x.clamp(0., (MAP_WIDTH - 1) as f32); + self.pos.y = self.pos.y.clamp(0., (MAP_HEIGHT - 1) as f32); // TODO: change color over time trail_map.set_pixel(self.pos.x as u32, self.pos.y as u32, RED);