From d2cb638fcdc5a01018ebd1cbcc846ce0a781989f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 7 Mar 2019 20:31:35 -0500 Subject: [PATCH] Use our own function to parse int --- native/jni/core/daemon.cpp | 2 +- native/jni/core/db.cpp | 11 ++++++----- native/jni/magiskhide/magiskhide.h | 15 --------------- native/jni/su/su.cpp | 2 +- native/jni/utils/include/utils.h | 6 ++++++ native/jni/utils/misc.cpp | 15 +++++++++++++++ 6 files changed, 29 insertions(+), 22 deletions(-) diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index 96a610dcb..78db0d24c 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -120,7 +120,7 @@ static void main_daemon() { parse_prop_file("/system/build.prop", [](auto key, auto val) -> bool { if (key == "ro.build.version.sdk") { LOGI("* Device API level: %s\n", val.data()); - SDK_INT = atoi(val.data()); + SDK_INT = parse_int(val); return false; } return true; diff --git a/native/jni/core/db.cpp b/native/jni/core/db.cpp index 314d96202..5ffb38eed 100644 --- a/native/jni/core/db.cpp +++ b/native/jni/core/db.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #define DB_VERSION 9 @@ -42,7 +43,7 @@ int db_settings::getKeyIdx(string_view key) const { } static int ver_cb(void *ver, int, char **data, char **) { - *((int *) ver) = atoi(data[0]); + *((int *) ver) = parse_int(data[0]); return 0; } @@ -199,7 +200,7 @@ char *db_exec(const char *sql, const db_row_cb &fn) { int get_db_settings(db_settings &cfg, int key) { char *err; auto settings_cb = [&](db_row &row) -> bool { - cfg[row["key"]] = atoi(row["value"].data()); + cfg[row["key"]] = parse_int(row["value"]); LOGD("magiskdb: query %s=[%s]\n", row["key"].data(), row["value"].data()); return true; }; @@ -237,9 +238,9 @@ int get_uid_policy(int uid, su_access &su) { sprintf(query, "SELECT policy, logging, notification FROM policies " "WHERE uid=%d AND (until=0 OR until>%li)", uid, time(nullptr)); err = db_exec(query, [&](db_row &row) -> bool { - su.policy = (policy_t) atoi(row["policy"].data()); - su.log = atoi(row["logging"].data()); - su.notify = atoi(row["notification"].data()); + su.policy = (policy_t) parse_int(row["policy"]); + su.log = parse_int(row["logging"]); + su.notify = parse_int(row["notification"]); LOGD("magiskdb: query policy=[%d] log=[%d] notify=[%d]\n", su.policy, su.log, su.notify); return true; }); diff --git a/native/jni/magiskhide/magiskhide.h b/native/jni/magiskhide/magiskhide.h index d8984484a..097e1ea6b 100644 --- a/native/jni/magiskhide/magiskhide.h +++ b/native/jni/magiskhide/magiskhide.h @@ -38,21 +38,6 @@ void clean_magisk_props(); void crawl_procfs(const std::function &fn); bool proc_name_match(int pid, const char *name); -/* - * Bionic's atoi runs through strtol(). - * Use our own implementation for faster conversion. - */ -static inline int parse_int(const char *s) { - int val = 0; - char c; - while ((c = *(s++))) { - if (c > '9' || c < '0') - return -1; - val = val * 10 + c - '0'; - } - return val; -} - extern pthread_t proc_monitor_thread; extern bool hide_enabled; extern pthread_mutex_t monitor_lock; diff --git a/native/jni/su/su.cpp b/native/jni/su/su.cpp index 0bee463f6..318452f76 100644 --- a/native/jni/su/su.cpp +++ b/native/jni/su/su.cpp @@ -177,7 +177,7 @@ int su_client_main(int argc, char *argv[]) { if (pw) su_req.uid = pw->pw_uid; else - su_req.uid = atoi(argv[optind]); + su_req.uid = parse_int(argv[optind]); optind++; } diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index 6eed44b50..808a8edf9 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -83,6 +83,7 @@ int strend(const char *s1, const char *s2); char *rtrim(char *str); void init_argv0(int argc, char **argv); void set_nice_name(const char *name); +int parse_int(const char *s); #define getline __getline #define getdelim __getdelim @@ -203,6 +204,11 @@ void mmap_rw(const char *filename, B &buf, L &sz) { // misc.cpp +template +int parse_int(S __s) { + return parse_int(__s.data()); +} + int new_daemon_thread(void *(*start_routine) (void *), void *arg = nullptr, const pthread_attr_t *attr = nullptr); diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index 3dbc2b8ca..781759a08 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -230,3 +230,18 @@ char *rtrim(char *str) { str[len] = '\0'; return str; } + +/* + * Bionic's atoi runs through strtol(). + * Use our own implementation for faster conversion. + */ +int parse_int(const char *s) { + int val = 0; + char c; + while ((c = *(s++))) { + if (c > '9' || c < '0') + return -1; + val = val * 10 + c - '0'; + } + return val; +}