mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 22:31:09 +08:00
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:
parent
d6079ce526
commit
f6f95b03a5
@ -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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user