Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSVC] glsl::vector::end() - vector subscript is out of range #22

Open
Philosoph228 opened this issue Dec 18, 2024 · 0 comments
Open

[MSVC] glsl::vector::end() - vector subscript is out of range #22

Philosoph228 opened this issue Dec 18, 2024 · 0 comments

Comments

@Philosoph228
Copy link

Description

In the implementation of glsl::vector, the following methods are used to return a pointer to the element after the last element:

glsl-parser/util.h

Lines 31 to 33 in 8fd08ed

T* end() { return &m_data[size()]; }
const T* begin() const { return &m_data[0]; }
const T* end() const { return &m_data[size()]; }

However, in Visual Studio (MSVC), runtime assertions prevent accessing any index out of range, including the "one-past-the-end" pointer returned by end(). This leads to a runtime exception when trying to access this position.

Problem

The MSVC runtime checks throw an exception due to the pointer being out of bounds, even though the pointer is intentionally pointing to the "past-the-end" element (the valid position after the last element in the container). The runtime enforcement causes issues with valid code and is not necessary for such access.

Workaround

There is a workaround to disable the runtime assertions by using the following preprocessor directive:

#define _CONTAINER_DEBUG_LEVEL 0 // Disable the debug level for the container

However, this is not a desirable solution, as it bypasses helpful runtime checks and reduces the safety of the code.

Suggestion

It would be better to adjust the implementation to avoid triggering MSVC's runtime assertions. One possible solution is to use std::vector::data() and compute the "one-past-the-end" pointer without triggering any bounds checking:

T* end() { return m_data.data() + size(); }

This approach should be safe and avoid the MSVC runtime assertions while maintaining the correctness of the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant