Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge "Move off std::sto* function which abort on failure."
Browse files Browse the repository at this point in the history
  • Loading branch information
enh-google authored and Gerrit Code Review committed Oct 14, 2016
2 parents c32d7fd + da46b39 commit a4398c1
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 54 deletions.
9 changes: 6 additions & 3 deletions adb/bugreport.cpp
Expand Up @@ -21,6 +21,7 @@
#include <string>
#include <vector>

#include <android-base/parseint.h>
#include <android-base/strings.h>

#include "sysdeps.h"
Expand Down Expand Up @@ -143,9 +144,11 @@ class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface
//
size_t idx1 = line.rfind(BUGZ_PROGRESS_PREFIX) + strlen(BUGZ_PROGRESS_PREFIX);
size_t idx2 = line.rfind(BUGZ_PROGRESS_SEPARATOR);
int progress = std::stoi(line.substr(idx1, (idx2 - idx1)));
int total = std::stoi(line.substr(idx2 + 1));
br_->UpdateProgress(line_message_, progress, total);
int progress, total;
if (android::base::ParseInt(line.substr(idx1, (idx2 - idx1)), &progress) &&
android::base::ParseInt(line.substr(idx2 + 1), &total)) {
br_->UpdateProgress(line_message_, progress, total);
}
} else {
invalid_lines_.push_back(line);
}
Expand Down
21 changes: 19 additions & 2 deletions base/include/android-base/parseint.h
Expand Up @@ -21,13 +21,15 @@
#include <stdlib.h>

#include <limits>
#include <string>

namespace android {
namespace base {

// Parses the unsigned decimal integer in the string 's' and sets 'out' to
// that value. Optionally allows the caller to define a 'max' beyond which
// otherwise valid values will be rejected. Returns boolean success.
// otherwise valid values will be rejected. Returns boolean success; 'out'
// is untouched if parsing fails.
template <typename T>
bool ParseUint(const char* s, T* out,
T max = std::numeric_limits<T>::max()) {
Expand All @@ -45,10 +47,17 @@ bool ParseUint(const char* s, T* out,
return true;
}

// TODO: string_view
template <typename T>
bool ParseUint(const std::string& s, T* out,
T max = std::numeric_limits<T>::max()) {
return ParseUint(s.c_str(), out, max);
}

// Parses the signed decimal integer in the string 's' and sets 'out' to
// that value. Optionally allows the caller to define a 'min' and 'max
// beyond which otherwise valid values will be rejected. Returns boolean
// success.
// success; 'out' is untouched if parsing fails.
template <typename T>
bool ParseInt(const char* s, T* out,
T min = std::numeric_limits<T>::min(),
Expand All @@ -67,6 +76,14 @@ bool ParseInt(const char* s, T* out,
return true;
}

// TODO: string_view
template <typename T>
bool ParseInt(const std::string& s, T* out,
T min = std::numeric_limits<T>::min(),
T max = std::numeric_limits<T>::max()) {
return ParseInt(s.c_str(), out, min, max);
}

} // namespace base
} // namespace android

Expand Down
36 changes: 28 additions & 8 deletions base/parseint_test.cpp
Expand Up @@ -19,7 +19,7 @@
#include <gtest/gtest.h>

TEST(parseint, signed_smoke) {
int i;
int i = 0;
ASSERT_FALSE(android::base::ParseInt("x", &i));
ASSERT_FALSE(android::base::ParseInt("123x", &i));

Expand All @@ -28,7 +28,7 @@ TEST(parseint, signed_smoke) {
ASSERT_TRUE(android::base::ParseInt("-123", &i));
ASSERT_EQ(-123, i);

short s;
short s = 0;
ASSERT_TRUE(android::base::ParseInt("1234", &s));
ASSERT_EQ(1234, s);

Expand All @@ -39,15 +39,15 @@ TEST(parseint, signed_smoke) {
}

TEST(parseint, unsigned_smoke) {
unsigned int i;
unsigned int i = 0u;
ASSERT_FALSE(android::base::ParseUint("x", &i));
ASSERT_FALSE(android::base::ParseUint("123x", &i));

ASSERT_TRUE(android::base::ParseUint("123", &i));
ASSERT_EQ(123u, i);
ASSERT_FALSE(android::base::ParseUint("-123", &i));

unsigned short s;
unsigned short s = 0u;
ASSERT_TRUE(android::base::ParseUint("1234", &s));
ASSERT_EQ(1234u, s);

Expand All @@ -58,21 +58,41 @@ TEST(parseint, unsigned_smoke) {
}

TEST(parseint, no_implicit_octal) {
int i;
int i = 0;
ASSERT_TRUE(android::base::ParseInt("0123", &i));
ASSERT_EQ(123, i);

unsigned int u;
unsigned int u = 0u;
ASSERT_TRUE(android::base::ParseUint("0123", &u));
ASSERT_EQ(123u, u);
}

TEST(parseint, explicit_hex) {
int i;
int i = 0;
ASSERT_TRUE(android::base::ParseInt("0x123", &i));
ASSERT_EQ(0x123, i);

unsigned int u;
unsigned int u = 0u;
ASSERT_TRUE(android::base::ParseUint("0x123", &u));
ASSERT_EQ(0x123u, u);
}

TEST(parseint, string) {
int i = 0;
ASSERT_TRUE(android::base::ParseInt(std::string("123"), &i));
ASSERT_EQ(123, i);

unsigned int u = 0u;
ASSERT_TRUE(android::base::ParseUint(std::string("123"), &u));
ASSERT_EQ(123u, u);
}

TEST(parseint, untouched_on_failure) {
int i = 123;
ASSERT_FALSE(android::base::ParseInt("456x", &i));
ASSERT_EQ(123, i);

unsigned int u = 123u;
ASSERT_FALSE(android::base::ParseInt("456x", &u));
ASSERT_EQ(123u, u);
}
4 changes: 2 additions & 2 deletions base/properties.cpp
Expand Up @@ -52,15 +52,15 @@ template <typename T>
T GetIntProperty(const std::string& key, T default_value, T min, T max) {
T result;
std::string value = GetProperty(key, "");
if (!value.empty() && android::base::ParseInt(value.c_str(), &result, min, max)) return result;
if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result;
return default_value;
}

template <typename T>
T GetUintProperty(const std::string& key, T default_value, T max) {
T result;
std::string value = GetProperty(key, "");
if (!value.empty() && android::base::ParseUint(value.c_str(), &result, max)) return result;
if (!value.empty() && android::base::ParseUint(value, &result, max)) return result;
return default_value;
}

Expand Down
2 changes: 1 addition & 1 deletion bootstat/boot_event_record_store.cpp
Expand Up @@ -59,7 +59,7 @@ bool ParseRecordEventTime(const std::string& path, int32_t* uptime) {
// Ignore existing bootstat records (which do not contain file content).
if (!content.empty()) {
int32_t value;
if (android::base::ParseInt(content.c_str(), &value)) {
if (android::base::ParseInt(content, &value)) {
bootstat::LogHistogram("bootstat_mtime_matches_content", value == *uptime);
}
}
Expand Down
4 changes: 2 additions & 2 deletions bootstat/bootstat.cpp
Expand Up @@ -60,7 +60,7 @@ void RecordBootEventFromCommandLine(
BootEventRecordStore boot_event_store;
if (!value_str.empty()) {
int32_t value = 0;
if (android::base::ParseInt(value_str.c_str(), &value)) {
if (android::base::ParseInt(value_str, &value)) {
boot_event_store.AddBootEventWithValue(event, value);
}
} else {
Expand Down Expand Up @@ -193,7 +193,7 @@ std::string CalculateBootCompletePrefix() {

std::string build_date_str = GetProperty("ro.build.date.utc");
int32_t build_date;
if (!android::base::ParseInt(build_date_str.c_str(), &build_date)) {
if (!android::base::ParseInt(build_date_str, &build_date)) {
return std::string();
}

Expand Down
6 changes: 3 additions & 3 deletions fastboot/fastboot.cpp
Expand Up @@ -755,7 +755,7 @@ static int64_t get_target_sparse_limit(Transport* transport) {
max_download_size = android::base::Trim(max_download_size);

uint64_t limit;
if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
if (!android::base::ParseUint(max_download_size, &limit)) {
fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
return 0;
}
Expand Down Expand Up @@ -903,7 +903,7 @@ static int get_slot_count(Transport* transport) {
if (!fb_getvar(transport, "slot-count", &var)) {
if (supports_AB_obsolete(transport)) return 2; // Legacy support
}
if (!android::base::ParseInt(var.c_str(), &count)) return 0;
if (!android::base::ParseInt(var, &count)) return 0;
return count;
}

Expand Down Expand Up @@ -1362,7 +1362,7 @@ static void fb_perform_format(Transport* transport,
}

int64_t size;
if (!android::base::ParseInt(partition_size.c_str(), &size)) {
if (!android::base::ParseInt(partition_size, &size)) {
fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
return;
}
Expand Down
3 changes: 2 additions & 1 deletion healthd/BatteryMonitor.cpp
Expand Up @@ -29,6 +29,7 @@
#include <memory>

#include <android-base/file.h>
#include <android-base/parseint.h>
#include <android-base/strings.h>
#include <batteryservice/BatteryService.h>
#include <cutils/klog.h>
Expand Down Expand Up @@ -182,7 +183,7 @@ int BatteryMonitor::getIntField(const String8& path) {
int value = 0;

if (readFromFile(path, &buf) > 0)
value = std::stoi(buf.c_str(), NULL, 0);
android::base::ParseInt(buf, &value);

return value;
}
Expand Down
39 changes: 22 additions & 17 deletions init/builtins.cpp
Expand Up @@ -304,7 +304,7 @@ static int do_mkdir(const std::vector<std::string>& args) {
/* mkdir <path> [mode] [owner] [group] */

if (args.size() >= 3) {
mode = std::stoul(args[2], 0, 8);
mode = std::strtoul(args[2].c_str(), 0, 8);
}

ret = make_dir(args[1].c_str(), mode);
Expand Down Expand Up @@ -637,10 +637,13 @@ static int do_setprop(const std::vector<std::string>& args) {
static int do_setrlimit(const std::vector<std::string>& args) {
struct rlimit limit;
int resource;
resource = std::stoi(args[1]);
limit.rlim_cur = std::stoi(args[2]);
limit.rlim_max = std::stoi(args[3]);
return setrlimit(resource, &limit);
if (android::base::ParseInt(args[1], &resource) &&
android::base::ParseUint(args[2], &limit.rlim_cur) &&
android::base::ParseUint(args[3], &limit.rlim_max)) {
return setrlimit(resource, &limit);
}
LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
return -1;
}

static int do_start(const std::vector<std::string>& args) {
Expand Down Expand Up @@ -709,7 +712,7 @@ static int do_powerctl(const std::vector<std::string>& args) {
std::string timeout = property_get("ro.build.shutdown_timeout");
unsigned int delay = 0;

if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
if (android::base::ParseUint(timeout, &delay) && delay > 0) {
Timer t;
// Ask all services to terminate.
ServiceManager::GetInstance().ForEachService(
Expand Down Expand Up @@ -764,13 +767,11 @@ static int do_rmdir(const std::vector<std::string>& args) {
}

static int do_sysclktz(const std::vector<std::string>& args) {
struct timezone tz;

memset(&tz, 0, sizeof(tz));
tz.tz_minuteswest = std::stoi(args[1]);
if (settimeofday(NULL, &tz))
return -1;
return 0;
struct timezone tz = {};
if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
return 0;
}
return -1;
}

static int do_verity_load_state(const std::vector<std::string>& args) {
Expand Down Expand Up @@ -914,7 +915,8 @@ static int do_restorecon_recursive(const std::vector<std::string>& args) {

static int do_loglevel(const std::vector<std::string>& args) {
// TODO: support names instead/as well?
int log_level = std::stoi(args[1]);
int log_level = -1;
android::base::ParseInt(args[1], &log_level);
android::base::LogSeverity severity;
switch (log_level) {
case 7: severity = android::base::DEBUG; break;
Expand Down Expand Up @@ -947,9 +949,12 @@ static int do_wait(const std::vector<std::string>& args) {
if (args.size() == 2) {
return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
} else if (args.size() == 3) {
return wait_for_file(args[1].c_str(), std::stoi(args[2]));
} else
return -1;
int timeout;
if (android::base::ParseInt(args[2], &timeout)) {
return wait_for_file(args[1].c_str(), timeout);
}
}
return -1;
}

/*
Expand Down

0 comments on commit a4398c1

Please sign in to comment.