[test] fix off-by-one bounds check in mock platform SettingsGet (#13509)

In both tests/unit/test_platform.cpp and tests/gtest/fake_platform.cpp,
the mock platform implementation of otPlatSettingsGet / SettingsGet
checked if (aIndex > setting->second.size()) instead of >=.

When scanning stored settings with sequential indices (such as in
Settings::AddOrUpdateBrOnLinkPrefix()), querying the first out-of-bounds
index (aIndex == setting->second.size()) failed to return
OT_ERROR_NOT_FOUND. Instead, it proceeded to index setting->second
out-of-bounds (setting->second[aIndex]). When compiled with standard
library assertions (e.g. _GLIBCXX_ASSERTIONS), this triggered an
assertion failure and aborted the process in ot-test-routing_manager.

This commit updates the index validation in otPlatSettingsGet,
otPlatSettingsDelete, and FakePlatform::SettingsGet/SettingsDelete to
correctly check bounds, support aIndex == -1 for deletion of all
values, and reject negative or out-of-range indices.
This commit is contained in:
Jonathan Hui
2026-08-17 14:12:02 -07:00
committed by GitHub
parent d141f51f2f
commit ed3688f15b
2 changed files with 16 additions and 4 deletions
+8 -2
View File
@@ -225,7 +225,7 @@ otError FakePlatform::SettingsGet(uint16_t aKey, uint16_t aIndex, uint8_t *aValu
return OT_ERROR_NOT_FOUND;
}
if (aIndex > setting->second.size())
if (aIndex >= setting->second.size())
{
return OT_ERROR_NOT_FOUND;
}
@@ -280,7 +280,13 @@ otError FakePlatform::SettingsDelete(uint16_t aKey, int aIndex)
return OT_ERROR_NOT_FOUND;
}
if (static_cast<std::size_t>(aIndex) >= setting->second.size())
if (aIndex == -1)
{
mSettings.erase(setting);
return OT_ERROR_NONE;
}
if (aIndex < 0 || static_cast<std::size_t>(aIndex) >= setting->second.size())
{
return OT_ERROR_NOT_FOUND;
}
+8 -2
View File
@@ -319,7 +319,7 @@ OT_TOOL_WEAK otError otPlatSettingsGet(otInstance *, uint16_t aKey, int aIndex,
return OT_ERROR_NOT_FOUND;
}
if (aIndex > setting->second.size())
if (aIndex < 0 || static_cast<size_t>(aIndex) >= setting->second.size())
{
return OT_ERROR_NOT_FOUND;
}
@@ -373,7 +373,13 @@ OT_TOOL_WEAK otError otPlatSettingsDelete(otInstance *, uint16_t aKey, int aInde
return OT_ERROR_NOT_FOUND;
}
if (aIndex >= setting->second.size())
if (aIndex == -1)
{
settings.erase(setting);
return OT_ERROR_NONE;
}
if (aIndex < 0 || static_cast<size_t>(aIndex) >= setting->second.size())
{
return OT_ERROR_NOT_FOUND;
}