OSDN Git Service

dumpstate: convert sprintfs to snprintfs
authorNick Kralevich <nnk@google.com>
Sat, 14 May 2016 23:47:44 +0000 (16:47 -0700)
committerNick Kralevich <nnk@google.com>
Sat, 14 May 2016 23:47:44 +0000 (16:47 -0700)
Bug: 28731007
Change-Id: Icfa4d6dfaf69e989ec785146a6bde1afb6a6f345

cmds/dumpstate/dumpstate.cpp
cmds/dumpstate/utils.cpp

index b731c53..16514c3 100644 (file)
@@ -124,7 +124,7 @@ void do_mountinfo(int pid, const char *name) {
 
     // Gets the the content of the /proc/PID/ns/mnt link, so only unique mount points
     // are added.
-    sprintf(path, "/proc/%d/ns/mnt", pid);
+    snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
     char linkname[PATH_MAX];
     ssize_t r = readlink(path, linkname, PATH_MAX);
     if (r == -1) {
@@ -135,7 +135,7 @@ void do_mountinfo(int pid, const char *name) {
 
     if (mount_points.find(linkname) == mount_points.end()) {
         // First time this mount point was found: add it
-        sprintf(path, "/proc/%d/mountinfo", pid);
+        snprintf(path, sizeof(path), "/proc/%d/mountinfo", pid);
         if (add_zip_entry(ZIP_ROOT_DIR + path, path)) {
             mount_points.insert(linkname);
         } else {
@@ -1048,7 +1048,7 @@ int main(int argc, char *argv[]) {
     char last_id[PROPERTY_VALUE_MAX];
     property_get("dumpstate.last_id", last_id, "0");
     id = strtoul(last_id, NULL, 10) + 1;
-    sprintf(last_id, "%lu", id);
+    snprintf(last_id, sizeof(last_id), "%lu", id);
     property_set("dumpstate.last_id", last_id);
     MYLOGI("dumpstate id: %lu\n", id);
 
@@ -1313,7 +1313,7 @@ int main(int argc, char *argv[]) {
         /* check if user changed the suffix using system properties */
         char key[PROPERTY_KEY_MAX];
         char value[PROPERTY_VALUE_MAX];
-        sprintf(key, "dumpstate.%d.name", getpid());
+        snprintf(key, sizeof(key), "dumpstate.%d.name", getpid());
         property_get(key, value, "");
         bool change_suffix= false;
         if (value[0]) {
index f1a1ed6..09c2e7f 100644 (file)
@@ -188,7 +188,7 @@ static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
     char taskpath[255];
     for_each_tid_func *func = (for_each_tid_func *) arg;
 
-    sprintf(taskpath, "/proc/%d/task", pid);
+    snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid);
 
     if (!(d = opendir(taskpath))) {
         printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
@@ -210,7 +210,7 @@ static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
         if (tid == pid)
             continue;
 
-        sprintf(commpath,"/proc/%d/comm", tid);
+        snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid);
         memset(comm, 0, sizeof(comm));
         if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
             strcpy(comm, "N/A");
@@ -244,7 +244,7 @@ void show_wchan(int pid, int tid, const char *name) {
 
     memset(buffer, 0, sizeof(buffer));
 
-    sprintf(path, "/proc/%d/wchan", tid);
+    snprintf(path, sizeof(path), "/proc/%d/wchan", tid);
     if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
         printf("Failed to open '%s' (%s)\n", path, strerror(errno));
         return;
@@ -309,7 +309,7 @@ void show_showtime(int pid, const char *name) {
 
     memset(buffer, 0, sizeof(buffer));
 
-    sprintf(path, "/proc/%d/stat", pid);
+    snprintf(path, sizeof(path), "/proc/%d/stat", pid);
     if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
         printf("Failed to open '%s' (%s)\n", path, strerror(errno));
         return;
@@ -397,8 +397,8 @@ void do_showmap(int pid, const char *name) {
     char title[255];
     char arg[255];
 
-    sprintf(title, "SHOW MAP %d (%s)", pid, name);
-    sprintf(arg, "%d", pid);
+    snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name);
+    snprintf(arg, sizeof(arg), "%d", pid);
     run_command(title, 10, SU_PATH, "root", "showmap", "-q", arg, NULL);
 }
 
@@ -1191,8 +1191,8 @@ void update_progress(int delta) {
         int new_total = weight_total * 1.2;
         MYLOGD("Adjusting total weight from %d to %d\n", weight_total, new_total);
         weight_total = new_total;
-        sprintf(key, "dumpstate.%d.max", getpid());
-        sprintf(value, "%d", weight_total);
+        snprintf(key, sizeof(key), "dumpstate.%d.max", getpid());
+        snprintf(value, sizeof(value), "%d", weight_total);
         int status = property_set(key, value);
         if (status) {
             MYLOGE("Could not update max weight by setting system property %s to %s: %d\n",
@@ -1200,8 +1200,8 @@ void update_progress(int delta) {
         }
     }
 
-    sprintf(key, "dumpstate.%d.progress", getpid());
-    sprintf(value, "%d", progress);
+    snprintf(key, sizeof(key), "dumpstate.%d.progress", getpid());
+    snprintf(value, sizeof(value), "%d", progress);
 
     if (progress % 100 == 0) {
         // We don't want to spam logcat, so only log multiples of 100.