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

[v0.14.0] Array.h array subscript 1 is above array bounds of 'Attribute_Request_Callback<5> [1]' [-Werror=array-bounds=] #239

Open
AdrienAdB opened this issue Dec 10, 2024 · 1 comment

Comments

@AdrienAdB
Copy link
Contributor

Hello, I have upgraded sdk to v0.14.0, I can successfully build when OTA apis isn't included.

Build is not possible when OTA instance is initialized:

// Initialize used apis
OTA_Firmware_Update ota;
const std::array<IAPI_Implementation*, 1U> apis =
{
    &ota
};

// Initialize ThingsBoard instance with the maximum needed buffer size
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis);

Build error:

subscript 1 is above array bounds of 'Attribute_Request_Callback<5> [1]' [-Werror=array-bounds=]

I am able to build if I comment line 171 of Array.h
https://github.com/thingsboard/thingsboard-client-sdk/blob/master/src/Array.h#L171

Environment:

  • ESP-IDF 6.9.0
  • Platformio
  • Thingsboard sdk v0.14.0

Debug terminal

In member function 'void Array<T, Capacity>::erase(const InputIterator&) [with InputIterator = Attribute_Request_Callback<5>*; T = Attribute_Request_Callback<5>; unsigned int Capacity = 1]',
    inlined from 'static void Helper::remove(DataContainer&, const InputIterator&) [with DataContainer = Array<Attribute_Request_Callback<5>, 1>; InputIterator = Attribute_Request_Callback<5>*]' at .pio/libdeps/evisi-4-ntc/ThingsBoard/src/Helper.h:80:24,
    inlined from 'void Attribute_Request<MaxSubscriptions, MaxAttributes, Logger>::Process_Json_Response(const char*, const ArduinoJson::V6215HB2::JsonDocument&) [with unsigned int MaxSubscriptions = 1; unsigned int MaxAttributes = 5; Logger = DefaultLogger]' at .pio/libdeps/evisi-4-ntc/ThingsBoard/src/Attribute_Request.h:123:27:
.pio/libdeps/evisi-4-ntc/ThingsBoard/src/Array.h:171:43: error: array subscript 1 is above array bounds of 'Attribute_Request_Callback<5> [1]' [-Werror=array-bounds=]
  171 |                 m_elements[i] = m_elements[i + 1];
      |                                 ~~~~~~~~~~^
.pio/libdeps/evisi-4-ntc/ThingsBoard/src/Array.h: In member function 'void Attribute_Request<MaxSubscriptions, MaxAttributes, Logger>::Process_Json_Response(const char*, const ArduinoJson::V6215HB2::JsonDocument&) [with unsigned int MaxSubscriptions = 1; unsigned int MaxAttributes = 5; Logger = DefaultLogger]':
.pio/libdeps/evisi-4-ntc/ThingsBoard/src/Array.h:207:12: note: while referencing 'Array<Attribute_Request_Callback<5>, 1>::m_elements'
  207 |     T      m_elements[Capacity] = {}; // Underlying c-array holding our data
@MathewHDYT
Copy link
Contributor

MathewHDYT commented Dec 10, 2024

It seems that the compiler sees that the array is only of size 1 and therefore disallows assigning [x + 1], because it is alway out of bounds.

Of course it is never called if there is only one element because we check in the for loop for (size_t i = index; i < m_size - 1; ++i) {, so this should not actually be a problem. But perhaps we need to make that clearer to the compiler somehow.


Sadly I can not reproduce the error on my machine, because I use PlatformIO, so you would need to help me debug potenital solutions if that is okay.

Could you try to reverse the copy statement so instead of in the Array.h fle on Line 170-173:

for (size_t i = index; i < m_size - 1; ++i) {
    m_elements[i] = m_elements[i + 1];
}

Can you use this statement instead, perhaps this signals more clearly to the compiler that we never access out of bounds.

for (size_t i = index + 1; i < m_size; ++i) {
    m_elements[i - 1] = m_elements[i];
}

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

2 participants