OSDN Git Service

ANRdaemon: replace sprintf with snprintf
authorWei Wang <wvw@google.com>
Wed, 12 Oct 2016 21:35:51 +0000 (14:35 -0700)
committerWei Wang <wvw@google.com>
Thu, 13 Oct 2016 03:50:42 +0000 (20:50 -0700)
Test: run ANRdaemon
Change-Id: I1b39647c2ea0a54c90ec210ccb9a14430944e21d

ANRdaemon/ANRdaemon.cpp

index 2419da3..c33414c 100644 (file)
@@ -134,7 +134,7 @@ static void get_cpu_stat(cpu_stat_t *cpu) {
 
     if ((fp = fopen("/proc/stat", "r")) == NULL) {
         err = true;
-        sprintf(err_msg, "can't read from /proc/stat with errno %d", errno);
+        snprintf(err_msg, sizeof(err_msg), "can't read from /proc/stat with errno %d", errno);
     } else {
         if (fscanf(fp, params, &cpu->utime, &cpu->ntime,
                 &cpu->stime, &cpu->itime, &cpu->iowtime, &cpu->irqtime,
@@ -193,7 +193,7 @@ static int dfs_enable(bool enable, const char* path) {
     int fd = open(path, O_WRONLY);
     if (fd == -1) {
         err = true;
-        sprintf(err_msg, "Can't open %s. Error: %d", path, errno);
+        snprintf(err_msg, sizeof(err_msg), "Can't open %s. Error: %d", path, errno);
         return -1;
     }
     const char* control = (enable?"1":"0");
@@ -206,7 +206,7 @@ static int dfs_enable(bool enable, const char* path) {
         }
 
         err = true;
-        sprintf(err_msg, "Error %d in writing to %s.", errno, path);
+        snprintf(err_msg, sizeof(err_msg), "Error %d in writing to %s.", errno, path);
     }
     close(fd);
     return (err?-1:0);
@@ -217,16 +217,16 @@ static int dfs_enable(bool enable, const char* path) {
  */
 static void dfs_set_property(uint64_t mtag, const char* mapp, bool enable) {
     char buf[64];
-    snprintf(buf, 64, "%#" PRIx64, mtag);
+    snprintf(buf, sizeof(buf), "%#" PRIx64, mtag);
     if (property_set(dfs_tags_property, buf) < 0) {
         err = true;
-        sprintf(err_msg, "Failed to set debug tags system properties.");
+        snprintf(err_msg, sizeof(err_msg), "Failed to set debug tags system properties.");
     }
 
     if (strlen(mapp) > 0
             && property_set(dfs_apps_property, mapp) < 0) {
         err = true;
-        sprintf(err_msg, "Failed to set debug applications.");
+        snprintf(err_msg, sizeof(err_msg), "Failed to set debug applications.");
     }
 
     if (log_sched) {
@@ -404,13 +404,13 @@ static int set_tracing_buffer_size(void) {
     int fd = open(dfs_buffer_size_path, O_WRONLY);
     if (fd == -1) {
         err = true;
-        sprintf(err_msg, "Can't open atrace buffer size file under /d/tracing.");
+        snprintf(err_msg, sizeof(err_msg), "Can't open atrace buffer size file under /d/tracing.");
         return -1;
     }
     ssize_t len = strlen(buf_size_kb);
     if (write(fd, buf_size_kb, len) != len) {
         err = true;
-        sprintf(err_msg, "Error in writing to atrace buffer size file.");
+        snprintf(err_msg, sizeof(err_msg), "Error in writing to atrace buffer size file.");
     }
     close(fd);
     return (err?-1:0);