libcutils: Use strnlen for default property values am: e67abec514
am: 6632536c0d
Change-Id: I1f0deb1837e96ec9d6bfc07a07b0c081c288ef9c
This commit is contained in:
commit
f957e56909
2 changed files with 53 additions and 7 deletions
|
@ -119,10 +119,7 @@ int property_get(const char *key, char *value, const char *default_value) {
|
|||
return len;
|
||||
}
|
||||
if (default_value) {
|
||||
len = strlen(default_value);
|
||||
if (len >= PROPERTY_VALUE_MAX) {
|
||||
len = PROPERTY_VALUE_MAX - 1;
|
||||
}
|
||||
len = strnlen(default_value, PROPERTY_VALUE_MAX - 1);
|
||||
memcpy(value, default_value, len);
|
||||
value[len] = '\0';
|
||||
}
|
||||
|
|
|
@ -159,19 +159,68 @@ TEST_F(PropertiesTest, SetString) {
|
|||
|
||||
TEST_F(PropertiesTest, GetString) {
|
||||
|
||||
// Try to use a default value that's too long => set fails
|
||||
// Try to use a default value that's too long => get truncates the value
|
||||
{
|
||||
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||
|
||||
std::string maxLengthString = std::string(PROPERTY_VALUE_MAX-1, 'a');
|
||||
std::string maxLengthString = std::string(PROPERTY_VALUE_MAX - 1, 'a');
|
||||
std::string oneLongerString = std::string(PROPERTY_VALUE_MAX, 'a');
|
||||
|
||||
// Expect that the value is truncated since it's too long (by 1)
|
||||
int len = property_get(PROPERTY_TEST_KEY, mValue, oneLongerString.c_str());
|
||||
EXPECT_EQ(PROPERTY_VALUE_MAX-1, len);
|
||||
EXPECT_EQ(PROPERTY_VALUE_MAX - 1, len);
|
||||
EXPECT_STREQ(maxLengthString.c_str(), mValue);
|
||||
ResetValue();
|
||||
}
|
||||
|
||||
// Try to use a default value that's the max length => get succeeds
|
||||
{
|
||||
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||
|
||||
std::string maxLengthString = std::string(PROPERTY_VALUE_MAX - 1, 'b');
|
||||
|
||||
// Expect that the value matches maxLengthString
|
||||
int len = property_get(PROPERTY_TEST_KEY, mValue, maxLengthString.c_str());
|
||||
EXPECT_EQ(PROPERTY_VALUE_MAX - 1, len);
|
||||
EXPECT_STREQ(maxLengthString.c_str(), mValue);
|
||||
ResetValue();
|
||||
}
|
||||
|
||||
// Try to use a default value of length one => get succeeds
|
||||
{
|
||||
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||
|
||||
std::string oneCharString = std::string(1, 'c');
|
||||
|
||||
// Expect that the value matches oneCharString
|
||||
int len = property_get(PROPERTY_TEST_KEY, mValue, oneCharString.c_str());
|
||||
EXPECT_EQ(1, len);
|
||||
EXPECT_STREQ(oneCharString.c_str(), mValue);
|
||||
ResetValue();
|
||||
}
|
||||
|
||||
// Try to use a default value of length zero => get succeeds
|
||||
{
|
||||
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||
|
||||
std::string zeroCharString = std::string(0, 'd');
|
||||
|
||||
// Expect that the value matches oneCharString
|
||||
int len = property_get(PROPERTY_TEST_KEY, mValue, zeroCharString.c_str());
|
||||
EXPECT_EQ(0, len);
|
||||
EXPECT_STREQ(zeroCharString.c_str(), mValue);
|
||||
ResetValue();
|
||||
}
|
||||
|
||||
// Try to use a NULL default value => get returns 0
|
||||
{
|
||||
ASSERT_OK(property_set(PROPERTY_TEST_KEY, ""));
|
||||
|
||||
// Expect a return value of 0
|
||||
int len = property_get(PROPERTY_TEST_KEY, mValue, NULL);
|
||||
EXPECT_EQ(0, len);
|
||||
ResetValue();
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(PropertiesTest, GetBool) {
|
||||
|
|
Loading…
Reference in a new issue