about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorCristian Cadar <c.cadar@imperial.ac.uk>2019-08-13 20:28:51 +0100
committerMartinNowack <martin.nowack@gmail.com>2019-08-14 16:26:48 +0100
commitdc414fde33f3ec22a6ae3cf29cfc6bc4a9ddae1a (patch)
tree56c1467c47fd45bdc2e3bad46b9b82b4186d4b8f
parent8426b2913e650970d46be9a4d088bf3945f41453 (diff)
downloadklee-dc414fde33f3ec22a6ae3cf29cfc6bc4a9ddae1a.tar.gz
Replace sprintf with snprintf throughout codebase
-rw-r--r--lib/Core/ExecutorTimers.cpp2
-rw-r--r--test/Runtime/POSIX/DirConsistency.c2
-rw-r--r--tools/gen-bout/gen-bout.cpp16
-rw-r--r--tools/klee-replay/file-creator.c17
-rw-r--r--tools/klee-replay/klee-replay.c7
-rw-r--r--tools/klee/main.cpp2
6 files changed, 23 insertions, 23 deletions
diff --git a/lib/Core/ExecutorTimers.cpp b/lib/Core/ExecutorTimers.cpp
index 22429221..e1d6684d 100644
--- a/lib/Core/ExecutorTimers.cpp
+++ b/lib/Core/ExecutorTimers.cpp
@@ -117,7 +117,7 @@ void Executor::processTimers(ExecutionState *current,
   if (ticks || dumpPTree || dumpStates) {
     if (dumpPTree) {
       char name[32];
-      sprintf(name, "ptree%08d.dot", (int) stats::instructions);
+      snprintf(name, sizeof(name), "ptree%08d.dot", (int) stats::instructions);
       auto os = interpreterHandler->openOutputFile(name);
       if (os) {
         processTree->dump(*os);
diff --git a/test/Runtime/POSIX/DirConsistency.c b/test/Runtime/POSIX/DirConsistency.c
index fa5c5314..688e11c1 100644
--- a/test/Runtime/POSIX/DirConsistency.c
+++ b/test/Runtime/POSIX/DirConsistency.c
@@ -60,7 +60,7 @@ int main(int argc, char **argv) {
 
   // Ensure atomic write
   char buf[64];
-  sprintf(buf, "COUNT: %d\n", count);
+  snprintf(buf, sizeof(buf), "COUNT: %d\n", count);
   fputs(buf, stdout);
   assert(hasA == foundA);
 
diff --git a/tools/gen-bout/gen-bout.cpp b/tools/gen-bout/gen-bout.cpp
index dfc02fda..477c302b 100644
--- a/tools/gen-bout/gen-bout.cpp
+++ b/tools/gen-bout/gen-bout.cpp
@@ -134,13 +134,13 @@ int main(int argc, char *argv[]) {
       static int total_args = 0;
 
       char arg[1024];
-      sprintf(arg, "arg%d", total_args++);
+      snprintf(arg, sizeof(arg), "arg%d", total_args++);
       push_obj(&b, (const char *)arg, nbytes, (unsigned char *)argv[i]);
 
       char *buf1 = (char *)malloc(1024);
       char *buf2 = (char *)malloc(1024);
       strcpy(buf1, "-sym-arg");
-      sprintf(buf2, "%ld", nbytes - 1);
+      snprintf(buf2, 1024, "%ld", nbytes - 1);
       argv_copy[argv_copy_idx++] = buf1;
       argv_copy[argv_copy_idx++] = buf2;
     }
@@ -182,9 +182,9 @@ int main(int argc, char *argv[]) {
     char *buf1 = (char *)malloc(1024);
     char *buf2 = (char *)malloc(1024);
     char *buf3 = (char *)malloc(1024);
-    sprintf(buf1, "-sym-files");
-    sprintf(buf2, "1");
-    sprintf(buf3, "%ld", nbytes);
+    snprintf(buf1, 1024, "-sym-files");
+    snprintf(buf2, 1024, "1");
+    snprintf(buf3, 1024, "%ld", nbytes);
     argv_copy[argv_copy_idx++] = buf1;
     argv_copy[argv_copy_idx++] = buf2;
     argv_copy[argv_copy_idx++] = buf3;
@@ -222,8 +222,8 @@ int main(int argc, char *argv[]) {
 
     char *buf1 = (char *)malloc(1024);
     char *buf2 = (char *)malloc(1024);
-    sprintf(buf1, "-sym-stdin");
-    sprintf(buf2, "%ld", file_stat.st_size);
+    snprintf(buf1, 1024, "-sym-stdin");
+    snprintf(buf2, 1024, "%ld", file_stat.st_size);
     argv_copy[argv_copy_idx++] = buf1;
     argv_copy[argv_copy_idx++] = buf2;
   }
@@ -257,7 +257,7 @@ int main(int argc, char *argv[]) {
     push_obj(&b, statname, sizeof(struct stat64), (unsigned char *)&file_stat);
 
     char *buf = (char *)malloc(1024);
-    sprintf(buf, "-sym-stdout");
+    snprintf(buf, 1024, "-sym-stdout");
     argv_copy[argv_copy_idx++] = buf;
   }
 
diff --git a/tools/klee-replay/file-creator.c b/tools/klee-replay/file-creator.c
index 6e32630f..9b6e66a8 100644
--- a/tools/klee-replay/file-creator.c
+++ b/tools/klee-replay/file-creator.c
@@ -53,8 +53,7 @@ static int create_link(const char *fname,
   char buf[64];
   struct stat64 *s = dfile->stat;
 
-  // XXX Broken, we want this path to be somewhere else most likely.
-  sprintf(buf, "%s.lnk", fname);
+  snprintf(buf, sizeof(buf), "%s.lnk", fname);
   s->st_mode = (s->st_mode & ~S_IFMT) | S_IFREG;
   create_file(-1, buf, dfile, tmpdir);
 
@@ -335,9 +334,9 @@ static int delete_dir(const char *path, int recurse) {
 
     if (d) {
       while ((de = readdir(d))) {
-        if (strcmp(de->d_name, ".")!=0 && strcmp(de->d_name, "..")!=0) {
+        if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) {
           char tmp[PATH_MAX];
-          sprintf(tmp, "%s/%s", path, de->d_name);
+          snprintf(tmp, sizeof(tmp), "%s/%s", path, de->d_name);
           delete_file(tmp, 0);
         }
       }
@@ -378,8 +377,8 @@ static void create_file(int target_fd,
   assert((target_fd == -1) ^ (target_name == NULL));
 
   if (target_name)
-    sprintf(tmpname, "%s/%s", tmpdir, target_name);
-  else sprintf(tmpname, "%s/fd%d", tmpdir, target_fd);
+    snprintf(tmpname, sizeof(tmpname), "%s/%s", tmpdir, target_name);
+  else snprintf(tmpname, sizeof(tmpname), "%s/fd%d", tmpdir, target_fd);
 
   target = tmpname;
 
@@ -418,7 +417,7 @@ static void create_file(int target_fd,
       // Only worry about 1 vs !1
       if (s->st_nlink > 1) {
         char tmp2[PATH_MAX];
-        sprintf(tmp2, "%s/%s.link2", tmpdir, target_name);
+        snprintf(tmp2, sizeof(tmp2), "%s/%s.link2", tmpdir, target_name);
         if (link(target_name, tmp2) < 0) {
           perror("link");
           exit(1);
@@ -449,7 +448,7 @@ void replay_create_files(exe_file_system_t *exe_fs) {
   umask(0);
   for (k=0; k < exe_fs->n_sym_files; k++) {
     char name[2];
-    sprintf(name, "%c", 'A' + k);
+    snprintf(name, sizeof(name), "%c", 'A' + k);
     create_file(-1, name, &exe_fs->sym_files[k], tmpdir);
   }
 
@@ -509,7 +508,7 @@ static void check_file(int index, exe_disk_file_t *dfile) {
   default:
     name[0] = 'A' + index;
     name[1] = '\0';
-    sprintf(fullname, "%s/%s", replay_dir, name);
+    snprintf(fullname, sizeof(fullname), "%s/%s", replay_dir, name);
     res = stat(fullname, &s);
 
     break;
diff --git a/tools/klee-replay/klee-replay.c b/tools/klee-replay/klee-replay.c
index f285ebf9..456c477d 100644
--- a/tools/klee-replay/klee-replay.c
+++ b/tools/klee-replay/klee-replay.c
@@ -69,7 +69,7 @@ static void stop_monitored(int process) {
       0
     };
     char pids[64];
-    sprintf(pids, "%d", process);
+    snprintf(pids, sizeof(pids), "%d", process);
 
     gdbargs[2] = pids;
     /* Make sure gdb doesn't talk to the user */
@@ -111,6 +111,7 @@ static void int_handler(int signal) {
     _exit(99);
   }
 }
+
 static void timeout_handler(int signal) {
   fprintf(stderr, "KLEE-REPLAY: NOTE: EXIT STATUS: TIMED OUT (%d seconds)\n",
           monitored_timeout);
@@ -139,7 +140,7 @@ void process_status(int status, time_t elapsed, const char *pfx) {
     if (rc == 0) {
       strcpy(msg, "NORMAL");
     } else {
-      sprintf(msg, "ABNORMAL %d", rc);
+      snprintf(msg, sizeof(msg), "ABNORMAL %d", rc);
     }
     fprintf(stderr, "KLEE-REPLAY: NOTE: EXIT STATUS: %s (%d seconds)\n", msg, (int) elapsed);
     _exit(rc);
@@ -406,7 +407,7 @@ int main(int argc, char** argv) {
 }
 
 
-/* Klee functions */
+/* KLEE functions */
 
 int __fputc_unlocked(int c, FILE *f) {
   return fputc_unlocked(c, f);
diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp
index 8235c261..09b3dea5 100644
--- a/tools/klee/main.cpp
+++ b/tools/klee/main.cpp
@@ -1005,7 +1005,7 @@ static void interrupt_handle_watchdog() {
 // the state data before going ahead and killing it.
 static void halt_via_gdb(int pid) {
   char buffer[256];
-  sprintf(buffer,
+  snprintf(buffer, sizeof(buffer),
           "gdb --batch --eval-command=\"p halt_execution()\" "
           "--eval-command=detach --pid=%d &> /dev/null",
           pid);