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.
This commit is contained in:
Chris Thrasher 2023-09-01 23:30:30 -06:00
parent d6079ce526
commit f6f95b03a5
3 changed files with 11 additions and 0 deletions

View File

@ -27,6 +27,8 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Graphics/ConvexShape.hpp> #include <SFML/Graphics/ConvexShape.hpp>
#include <cassert>
namespace sf namespace sf
{ {
@ -55,6 +57,7 @@ std::size_t ConvexShape::getPointCount() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void ConvexShape::setPoint(std::size_t index, const Vector2f& point) void ConvexShape::setPoint(std::size_t index, const Vector2f& point)
{ {
assert(index < m_points.size() && "Index is out of bounds");
m_points[index] = point; m_points[index] = point;
update(); update();
} }
@ -63,6 +66,7 @@ void ConvexShape::setPoint(std::size_t index, const Vector2f& point)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Vector2f ConvexShape::getPoint(std::size_t index) const Vector2f ConvexShape::getPoint(std::size_t index) const
{ {
assert(index < m_points.size() && "Index is out of bounds");
return m_points[index]; return m_points[index];
} }

View File

@ -28,6 +28,8 @@
#include <SFML/Graphics/RenderTarget.hpp> #include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/VertexArray.hpp> #include <SFML/Graphics/VertexArray.hpp>
#include <cassert>
namespace sf namespace sf
{ {
@ -51,6 +53,7 @@ std::size_t VertexArray::getVertexCount() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Vertex& VertexArray::operator[](std::size_t index) Vertex& VertexArray::operator[](std::size_t index)
{ {
assert(index < m_vertices.size() && "Index is out of bounds");
return m_vertices[index]; return m_vertices[index];
} }
@ -58,6 +61,7 @@ Vertex& VertexArray::operator[](std::size_t index)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const Vertex& VertexArray::operator[](std::size_t index) const const Vertex& VertexArray::operator[](std::size_t index) const
{ {
assert(index < m_vertices.size() && "Index is out of bounds");
return m_vertices[index]; return m_vertices[index];
} }

View File

@ -31,6 +31,7 @@
#include <iterator> #include <iterator>
#include <utility> #include <utility>
#include <cassert>
#include <cstring> #include <cstring>
@ -213,6 +214,7 @@ String& String::operator+=(const String& right)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
char32_t String::operator[](std::size_t index) const char32_t String::operator[](std::size_t index) const
{ {
assert(index < m_string.size() && "Index is out of bounds");
return m_string[index]; 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) char32_t& String::operator[](std::size_t index)
{ {
assert(index < m_string.size() && "Index is out of bounds");
return m_string[index]; return m_string[index];
} }