Loading base/include/android-base/parseint.h +12 −0 Original line number Diff line number Diff line Loading @@ -33,6 +33,14 @@ namespace base { template <typename T> bool ParseUint(const char* s, T* out, T max = std::numeric_limits<T>::max(), bool allow_suffixes = false) { while (isspace(*s)) { s++; } if (s[0] == '-') { return false; } int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; errno = 0; char* end; Loading Loading @@ -79,6 +87,10 @@ template <typename T> bool ParseInt(const char* s, T* out, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) { while (isspace(*s)) { s++; } int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; errno = 0; char* end; Loading base/parseint_test.cpp +20 −0 Original line number Diff line number Diff line Loading @@ -25,8 +25,14 @@ TEST(parseint, signed_smoke) { ASSERT_TRUE(android::base::ParseInt("123", &i)); ASSERT_EQ(123, i); i = 0; EXPECT_TRUE(android::base::ParseInt(" 123", &i)); EXPECT_EQ(123, i); ASSERT_TRUE(android::base::ParseInt("-123", &i)); ASSERT_EQ(-123, i); i = 0; EXPECT_TRUE(android::base::ParseInt(" -123", &i)); EXPECT_EQ(-123, i); short s = 0; ASSERT_TRUE(android::base::ParseInt("1234", &s)); Loading @@ -49,7 +55,11 @@ TEST(parseint, unsigned_smoke) { ASSERT_TRUE(android::base::ParseUint("123", &i)); ASSERT_EQ(123u, i); i = 0u; EXPECT_TRUE(android::base::ParseUint(" 123", &i)); EXPECT_EQ(123u, i); ASSERT_FALSE(android::base::ParseUint("-123", &i)); EXPECT_FALSE(android::base::ParseUint(" -123", &i)); unsigned short s = 0u; ASSERT_TRUE(android::base::ParseUint("1234", &s)); Loading @@ -63,6 +73,10 @@ TEST(parseint, unsigned_smoke) { ASSERT_FALSE(android::base::ParseUint<unsigned short>("x", nullptr)); ASSERT_FALSE(android::base::ParseUint<unsigned short>("123x", nullptr)); ASSERT_TRUE(android::base::ParseUint<unsigned short>("1234", nullptr)); unsigned long long int lli; EXPECT_FALSE(android::base::ParseUint("-123", &lli)); EXPECT_FALSE(android::base::ParseUint(" -123", &lli)); } TEST(parseint, no_implicit_octal) { Loading @@ -79,10 +93,16 @@ TEST(parseint, explicit_hex) { int i = 0; ASSERT_TRUE(android::base::ParseInt("0x123", &i)); ASSERT_EQ(0x123, i); i = 0; EXPECT_TRUE(android::base::ParseInt(" 0x123", &i)); EXPECT_EQ(0x123, i); unsigned int u = 0u; ASSERT_TRUE(android::base::ParseUint("0x123", &u)); ASSERT_EQ(0x123u, u); u = 0u; EXPECT_TRUE(android::base::ParseUint(" 0x123", &u)); EXPECT_EQ(0x123u, u); } TEST(parseint, string) { Loading init/README.md +1 −0 Original line number Diff line number Diff line Loading @@ -506,6 +506,7 @@ Commands _resource_ is best specified using its text representation ('cpu', 'rtio', etc or 'RLIM_CPU', 'RLIM_RTIO', etc). It also may be specified as the int value that the resource enum corresponds to. _cur_ and _max_ can be 'unlimited' or '-1' to indicate an infinite rlimit. `start <service>` > Start a service running if it is not already running. Loading init/rlimit_parser.cpp +8 −2 Original line number Diff line number Diff line Loading @@ -65,12 +65,18 @@ Result<std::pair<int, rlimit>> ParseRlimit(const std::vector<std::string>& args) } rlimit limit; if (!ParseUint(args[2], &limit.rlim_cur)) { if (args[2] == "-1" || args[2] == "unlimited") { limit.rlim_cur = RLIM_INFINITY; } else if (!ParseUint(args[2], &limit.rlim_cur)) { return Error() << "Could not parse soft limit '" << args[2] << "'"; } if (!ParseUint(args[3], &limit.rlim_max)) { if (args[3] == "-1" || args[3] == "unlimited") { limit.rlim_max = RLIM_INFINITY; } else if (!ParseUint(args[3], &limit.rlim_max)) { return Error() << "Could not parse hard limit '" << args[3] << "'"; } return {resource, limit}; } Loading init/rlimit_parser_test.cpp +60 −56 Original line number Diff line number Diff line Loading @@ -96,10 +96,10 @@ TEST(rlimit, RlimitSuccess) { {{"9", "10", "10"}, {9, {10, 10}}}, {{"10", "10", "10"}, {10, {10, 10}}}, {{"11", "10", "10"}, {11, {10, 10}}}, {{"12", "10", "10"}, {12, {10, 10}}}, {{"13", "10", "10"}, {13, {10, 10}}}, {{"14", "10", "10"}, {14, {10, 10}}}, {{"15", "10", "10"}, {15, {10, 10}}}, {{"12", "unlimited", "10"}, {12, {RLIM_INFINITY, 10}}}, {{"13", "-1", "10"}, {13, {RLIM_INFINITY, 10}}}, {{"14", "10", "unlimited"}, {14, {10, RLIM_INFINITY}}}, {{"15", "10", "-1"}, {15, {10, RLIM_INFINITY}}}, }; for (const auto& [input, expected_result] : inputs_and_results) { Loading @@ -115,6 +115,10 @@ TEST(rlimit, RlimitFailure) { {{"RLIM_", "10", "10"}, "Could not parse resource 'RLIM_'"}, {{"cpu", "abc", "10"}, "Could not parse soft limit 'abc'"}, {{"cpu", "10", "abc"}, "Could not parse hard limit 'abc'"}, {{"cpu", "unlimit", "10"}, "Could not parse soft limit 'unlimit'"}, {{"cpu", "10", "unlimit"}, "Could not parse hard limit 'unlimit'"}, {{"cpu", "-2", "10"}, "Could not parse soft limit '-2'"}, {{"cpu", "10", "-2"}, "Could not parse hard limit '-2'"}, }; for (const auto& [input, expected_result] : inputs_and_results) { Loading Loading
base/include/android-base/parseint.h +12 −0 Original line number Diff line number Diff line Loading @@ -33,6 +33,14 @@ namespace base { template <typename T> bool ParseUint(const char* s, T* out, T max = std::numeric_limits<T>::max(), bool allow_suffixes = false) { while (isspace(*s)) { s++; } if (s[0] == '-') { return false; } int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; errno = 0; char* end; Loading Loading @@ -79,6 +87,10 @@ template <typename T> bool ParseInt(const char* s, T* out, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) { while (isspace(*s)) { s++; } int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; errno = 0; char* end; Loading
base/parseint_test.cpp +20 −0 Original line number Diff line number Diff line Loading @@ -25,8 +25,14 @@ TEST(parseint, signed_smoke) { ASSERT_TRUE(android::base::ParseInt("123", &i)); ASSERT_EQ(123, i); i = 0; EXPECT_TRUE(android::base::ParseInt(" 123", &i)); EXPECT_EQ(123, i); ASSERT_TRUE(android::base::ParseInt("-123", &i)); ASSERT_EQ(-123, i); i = 0; EXPECT_TRUE(android::base::ParseInt(" -123", &i)); EXPECT_EQ(-123, i); short s = 0; ASSERT_TRUE(android::base::ParseInt("1234", &s)); Loading @@ -49,7 +55,11 @@ TEST(parseint, unsigned_smoke) { ASSERT_TRUE(android::base::ParseUint("123", &i)); ASSERT_EQ(123u, i); i = 0u; EXPECT_TRUE(android::base::ParseUint(" 123", &i)); EXPECT_EQ(123u, i); ASSERT_FALSE(android::base::ParseUint("-123", &i)); EXPECT_FALSE(android::base::ParseUint(" -123", &i)); unsigned short s = 0u; ASSERT_TRUE(android::base::ParseUint("1234", &s)); Loading @@ -63,6 +73,10 @@ TEST(parseint, unsigned_smoke) { ASSERT_FALSE(android::base::ParseUint<unsigned short>("x", nullptr)); ASSERT_FALSE(android::base::ParseUint<unsigned short>("123x", nullptr)); ASSERT_TRUE(android::base::ParseUint<unsigned short>("1234", nullptr)); unsigned long long int lli; EXPECT_FALSE(android::base::ParseUint("-123", &lli)); EXPECT_FALSE(android::base::ParseUint(" -123", &lli)); } TEST(parseint, no_implicit_octal) { Loading @@ -79,10 +93,16 @@ TEST(parseint, explicit_hex) { int i = 0; ASSERT_TRUE(android::base::ParseInt("0x123", &i)); ASSERT_EQ(0x123, i); i = 0; EXPECT_TRUE(android::base::ParseInt(" 0x123", &i)); EXPECT_EQ(0x123, i); unsigned int u = 0u; ASSERT_TRUE(android::base::ParseUint("0x123", &u)); ASSERT_EQ(0x123u, u); u = 0u; EXPECT_TRUE(android::base::ParseUint(" 0x123", &u)); EXPECT_EQ(0x123u, u); } TEST(parseint, string) { Loading
init/README.md +1 −0 Original line number Diff line number Diff line Loading @@ -506,6 +506,7 @@ Commands _resource_ is best specified using its text representation ('cpu', 'rtio', etc or 'RLIM_CPU', 'RLIM_RTIO', etc). It also may be specified as the int value that the resource enum corresponds to. _cur_ and _max_ can be 'unlimited' or '-1' to indicate an infinite rlimit. `start <service>` > Start a service running if it is not already running. Loading
init/rlimit_parser.cpp +8 −2 Original line number Diff line number Diff line Loading @@ -65,12 +65,18 @@ Result<std::pair<int, rlimit>> ParseRlimit(const std::vector<std::string>& args) } rlimit limit; if (!ParseUint(args[2], &limit.rlim_cur)) { if (args[2] == "-1" || args[2] == "unlimited") { limit.rlim_cur = RLIM_INFINITY; } else if (!ParseUint(args[2], &limit.rlim_cur)) { return Error() << "Could not parse soft limit '" << args[2] << "'"; } if (!ParseUint(args[3], &limit.rlim_max)) { if (args[3] == "-1" || args[3] == "unlimited") { limit.rlim_max = RLIM_INFINITY; } else if (!ParseUint(args[3], &limit.rlim_max)) { return Error() << "Could not parse hard limit '" << args[3] << "'"; } return {resource, limit}; } Loading
init/rlimit_parser_test.cpp +60 −56 Original line number Diff line number Diff line Loading @@ -96,10 +96,10 @@ TEST(rlimit, RlimitSuccess) { {{"9", "10", "10"}, {9, {10, 10}}}, {{"10", "10", "10"}, {10, {10, 10}}}, {{"11", "10", "10"}, {11, {10, 10}}}, {{"12", "10", "10"}, {12, {10, 10}}}, {{"13", "10", "10"}, {13, {10, 10}}}, {{"14", "10", "10"}, {14, {10, 10}}}, {{"15", "10", "10"}, {15, {10, 10}}}, {{"12", "unlimited", "10"}, {12, {RLIM_INFINITY, 10}}}, {{"13", "-1", "10"}, {13, {RLIM_INFINITY, 10}}}, {{"14", "10", "unlimited"}, {14, {10, RLIM_INFINITY}}}, {{"15", "10", "-1"}, {15, {10, RLIM_INFINITY}}}, }; for (const auto& [input, expected_result] : inputs_and_results) { Loading @@ -115,6 +115,10 @@ TEST(rlimit, RlimitFailure) { {{"RLIM_", "10", "10"}, "Could not parse resource 'RLIM_'"}, {{"cpu", "abc", "10"}, "Could not parse soft limit 'abc'"}, {{"cpu", "10", "abc"}, "Could not parse hard limit 'abc'"}, {{"cpu", "unlimit", "10"}, "Could not parse soft limit 'unlimit'"}, {{"cpu", "10", "unlimit"}, "Could not parse hard limit 'unlimit'"}, {{"cpu", "-2", "10"}, "Could not parse soft limit '-2'"}, {{"cpu", "10", "-2"}, "Could not parse hard limit '-2'"}, }; for (const auto& [input, expected_result] : inputs_and_results) { Loading