From f6f95b03a529effd99260033c2d3112fd6107f76 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Fri, 1 Sep 2023 23:30:30 -0600 Subject: [PATCH] Assert in-bounds access when using `operator[]` Out of bounds array access is undefined behavior so let's be sure to catch potential UB in debug builds. It's too easy to accidentally provide an invalid index to one of these public APIs. --- src/SFML/Graphics/ConvexShape.cpp | 4 ++++ src/SFML/Graphics/VertexArray.cpp | 4 ++++ src/SFML/System/String.cpp | 3 +++ 3 files changed, 11 insertions(+) diff --git a/src/SFML/Graphics/ConvexShape.cpp b/src/SFML/Graphics/ConvexShape.cpp index 978862e2..da446ddc 100644 --- a/src/SFML/Graphics/ConvexShape.cpp +++ b/src/SFML/Graphics/ConvexShape.cpp @@ -27,6 +27,8 @@ //////////////////////////////////////////////////////////// #include +#include + namespace sf { @@ -55,6 +57,7 @@ std::size_t ConvexShape::getPointCount() const //////////////////////////////////////////////////////////// void ConvexShape::setPoint(std::size_t index, const Vector2f& point) { + assert(index < m_points.size() && "Index is out of bounds"); m_points[index] = point; update(); } @@ -63,6 +66,7 @@ void ConvexShape::setPoint(std::size_t index, const Vector2f& point) //////////////////////////////////////////////////////////// Vector2f ConvexShape::getPoint(std::size_t index) const { + assert(index < m_points.size() && "Index is out of bounds"); return m_points[index]; } diff --git a/src/SFML/Graphics/VertexArray.cpp b/src/SFML/Graphics/VertexArray.cpp index 9067ec7a..027f2bee 100644 --- a/src/SFML/Graphics/VertexArray.cpp +++ b/src/SFML/Graphics/VertexArray.cpp @@ -28,6 +28,8 @@ #include #include +#include + namespace sf { @@ -51,6 +53,7 @@ std::size_t VertexArray::getVertexCount() const //////////////////////////////////////////////////////////// Vertex& VertexArray::operator[](std::size_t index) { + assert(index < m_vertices.size() && "Index is out of bounds"); return m_vertices[index]; } @@ -58,6 +61,7 @@ Vertex& VertexArray::operator[](std::size_t index) //////////////////////////////////////////////////////////// const Vertex& VertexArray::operator[](std::size_t index) const { + assert(index < m_vertices.size() && "Index is out of bounds"); return m_vertices[index]; } diff --git a/src/SFML/System/String.cpp b/src/SFML/System/String.cpp index cf2d95ae..7de8eea6 100644 --- a/src/SFML/System/String.cpp +++ b/src/SFML/System/String.cpp @@ -31,6 +31,7 @@ #include #include +#include #include @@ -213,6 +214,7 @@ String& String::operator+=(const String& right) //////////////////////////////////////////////////////////// char32_t String::operator[](std::size_t index) const { + assert(index < m_string.size() && "Index is out of bounds"); return m_string[index]; } @@ -220,6 +222,7 @@ char32_t String::operator[](std::size_t index) const //////////////////////////////////////////////////////////// char32_t& String::operator[](std::size_t index) { + assert(index < m_string.size() && "Index is out of bounds"); return m_string[index]; }