This commit is contained in:
Sangeet 2026-09-03 16:02:30 +05:30 committed by GitHub
commit 1bc7063a51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 7 deletions

View File

@ -1360,9 +1360,11 @@ bool ParseInt32(const Message& src_text, const char* str, int32_t* value) {
char* end = nullptr;
const long long_value = strtol(str, &end, 10); // NOLINT
// Has strtol() consumed all characters in the string?
if (*end != '\0') {
// No - an invalid character was encountered.
// Has strtol() consumed at least one character and all characters in the
// string?
if (end == str || *end != '\0') {
// No - either no characters were consumed or an invalid character was
// encountered.
Message msg;
msg << "WARNING: " << src_text
<< " is expected to be a 32-bit integer, but actually"

View File

@ -1773,10 +1773,12 @@ TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueOverflows) {
printf("(expecting 2 warnings)\n");
int32_t value = 123;
EXPECT_FALSE(ParseFlag("--abc=12345678987654321", "abc", &value));
EXPECT_FALSE(ParseFlag("--" GTEST_FLAG_PREFIX_ "abc=12345678987654321", "abc",
&value));
EXPECT_EQ(123, value);
EXPECT_FALSE(ParseFlag("--abc=-12345678987654321", "abc", &value));
EXPECT_FALSE(ParseFlag("--" GTEST_FLAG_PREFIX_ "abc=-12345678987654321",
"abc", &value));
EXPECT_EQ(123, value);
}
@ -1787,10 +1789,20 @@ TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueIsInvalid) {
printf("(expecting 2 warnings)\n");
int32_t value = 123;
EXPECT_FALSE(ParseFlag("--abc=A1", "abc", &value));
EXPECT_FALSE(ParseFlag("--" GTEST_FLAG_PREFIX_ "abc=A1", "abc", &value));
EXPECT_EQ(123, value);
EXPECT_FALSE(ParseFlag("--abc=12X", "abc", &value));
EXPECT_FALSE(ParseFlag("--" GTEST_FLAG_PREFIX_ "abc=12X", "abc", &value));
EXPECT_EQ(123, value);
}
// Tests that ParseInt32Flag() rejects an empty value and doesn't change the
// output value.
TEST(ParseInt32FlagTest, RejectsEmptyValue) {
printf("(expecting 1 warning)\n");
int32_t value = 123;
EXPECT_FALSE(ParseFlag("--" GTEST_FLAG_PREFIX_ "abc=", "abc", &value));
EXPECT_EQ(123, value);
}