[or-cvs] Replace sprintf with snprintf

Nick Mathewson nickm at seul.org
Wed Oct 27 05:53:11 UTC 2004


Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv389/src/or

Modified Files:
	circuitbuild.c directory.c dirserv.c main.c rendservice.c 
	rephist.c router.c routerlist.c test.c 
Log Message:
Replace sprintf with snprintf

Index: circuitbuild.c
===================================================================
RCS file: /home/or/cvsroot/src/or/circuitbuild.c,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -d -r1.42 -r1.43
--- circuitbuild.c	17 Oct 2004 21:51:19 -0000	1.42
+++ circuitbuild.c	27 Oct 2004 05:53:07 -0000	1.43
@@ -1105,7 +1105,7 @@
 
     for(i=0; i < smartlist_len(rl->routers); i++) {
       r = smartlist_get(rl->routers, i);
-      sprintf(buf, "%d", r->or_port);
+      snprintf(buf, sizeof(buf), "%d", r->or_port);
       if(!smartlist_string_isin(options.FirewallPorts, buf))
          smartlist_add(excluded, r);
     }

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/src/or/directory.c,v
retrieving revision 1.152
retrieving revision 1.153
diff -u -d -r1.152 -r1.153
--- directory.c	25 Oct 2004 06:16:26 -0000	1.152
+++ directory.c	27 Oct 2004 05:53:07 -0000	1.153
@@ -131,7 +131,7 @@
        * descriptor -- those use Tor. */
       if (options.FascistFirewall && purpose == DIR_PURPOSE_UPLOAD_DIR &&
           !options.HttpProxy) {
-        sprintf(buf,"%d",ds->dir_port);
+        snprintf(buf,sizeof(buf),"%d",ds->dir_port);
         if (!smartlist_string_isin(options.FirewallPorts, buf))
           continue;
       }
@@ -325,10 +325,10 @@
   if(conn->port == 80) {
     strlcpy(hoststring, conn->address, sizeof(hoststring));
   } else {
-    sprintf(hoststring, "%s:%d", conn->address, conn->port);
+    snprintf(hoststring, sizeof(hoststring),"%s:%d",conn->address, conn->port);
   }
   if(options.HttpProxy) {
-    sprintf(proxystring, "http://%s", hoststring);
+    snprintf(proxystring, sizeof(proxystring),"http://%s", hoststring);
   } else {
     proxystring[0] = 0;
   }
@@ -361,7 +361,7 @@
       conn->rend_query[payload_len] = 0;
 
       httpcommand = "GET";
-      sprintf(url, "%s/rendezvous/%s", use_newer ? "/tor" : "", payload);
+      snprintf(url, sizeof(url), "%s/rendezvous/%s", use_newer ? "/tor" : "", payload);
 
       /* XXX We're using payload here to mean something other than
        * payload of the http post. This is probably bad, and should
@@ -373,7 +373,7 @@
     case DIR_PURPOSE_UPLOAD_RENDDESC:
       tor_assert(payload);
       httpcommand = "POST";
-      sprintf(url, "%s/rendezvous/publish", use_newer ? "/tor" : "");
+      snprintf(url, sizeof(url), "%s/rendezvous/publish", use_newer ? "/tor" : "");
       break;
   }
 

Index: dirserv.c
===================================================================
RCS file: /home/or/cvsroot/src/or/dirserv.c,v
retrieving revision 1.100
retrieving revision 1.101
diff -u -d -r1.100 -r1.101
--- dirserv.c	27 Oct 2004 00:48:51 -0000	1.100
+++ dirserv.c	27 Oct 2004 05:53:07 -0000	1.101
@@ -835,7 +835,7 @@
 #endif
   published_on = time(NULL);
   format_iso_time(published, published_on);
-  sprintf(s, "network-status\n"
+  snprintf(s, len, "network-status\n"
              "published %s\n"
              "router-status %s\n"
              "opt dir-signing-key %s\n"

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.339
retrieving revision 1.340
diff -u -d -r1.339 -r1.340
--- main.c	24 Oct 2004 01:22:40 -0000	1.339
+++ main.c	27 Oct 2004 05:53:07 -0000	1.340
@@ -757,7 +757,7 @@
   }
   if(authdir_mode()) {
     /* reload the approved-routers file */
-    sprintf(keydir,"%s/approved-routers", get_data_directory(&options));
+    snprintf(keydir,sizeof(keydir),"%s/approved-routers", get_data_directory(&options));
     log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
     if(dirserv_parse_fingerprint_file(keydir) < 0) {
       log_fn(LOG_WARN, "Error reloading fingerprints. Continuing with old list.");
@@ -772,7 +772,7 @@
     dnsworkers_rotate();
     /* Rebuild fresh descriptor as needed. */
     router_rebuild_descriptor();
-    sprintf(keydir,"%s/router.desc", get_data_directory(&options));
+    snprintf(keydir,sizeof(keydir),"%s/router.desc", get_data_directory(&options));
     log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
     if (write_str_to_file(keydir, router_get_my_descriptor(), 0)) {
       return -1;

Index: rendservice.c
===================================================================
RCS file: /home/or/cvsroot/src/or/rendservice.c,v
retrieving revision 1.94
retrieving revision 1.95
diff -u -d -r1.94 -r1.95
--- rendservice.c	24 Oct 2004 23:09:48 -0000	1.94
+++ rendservice.c	27 Oct 2004 05:53:07 -0000	1.95
@@ -309,7 +309,7 @@
       log_fn(LOG_WARN, "Directory name too long: '%s'", s->directory);
       return -1;
     }
-    sprintf(buf, "%s.onion\n", s->service_id);
+    snprintf(buf, sizeof(buf),"%s.onion\n", s->service_id);
     if (write_str_to_file(fname,buf,0)<0)
       return -1;
   }

Index: rephist.c
===================================================================
RCS file: /home/or/cvsroot/src/or/rephist.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- rephist.c	14 Oct 2004 02:47:09 -0000	1.30
+++ rephist.c	27 Oct 2004 05:53:07 -0000	1.31
@@ -528,7 +528,7 @@
   for (r=0;r<2;++r) {
     b = r?read_array:write_array;
     format_iso_time(t, b->next_period-NUM_SECS_BW_SUM_INTERVAL);
-    sprintf(cp, "opt %s %s (%d s) ", r?"read-history ":"write-history", t,
+    snprintf(cp, len-(cp-buf), "opt %s %s (%d s) ", r?"read-history ":"write-history", t,
             NUM_SECS_BW_SUM_INTERVAL);
     cp += strlen(cp);
 
@@ -543,9 +543,9 @@
     for (n=0; n<b->num_maxes_set; ++n,++i) {
       while (i >= NUM_TOTALS) i -= NUM_TOTALS;
       if (n==(b->num_maxes_set-1))
-        sprintf(cp, "%d", b->totals[i]);
+        snprintf(cp, len-(cp-buf), "%d", b->totals[i]);
       else
-        sprintf(cp, "%d,", b->totals[i]);
+        snprintf(cp, len-(cp-buf), "%d,", b->totals[i]);
       cp += strlen(cp);
     }
     strcat(cp, "\n");

Index: router.c
===================================================================
RCS file: /home/or/cvsroot/src/or/router.c,v
retrieving revision 1.102
retrieving revision 1.103
diff -u -d -r1.102 -r1.103
--- router.c	19 Oct 2004 18:19:59 -0000	1.102
+++ router.c	27 Oct 2004 05:53:07 -0000	1.103
@@ -101,9 +101,10 @@
   char fname[512];
   char fname_prev[512];
   crypto_pk_env_t *prkey;
-  sprintf(fname,"%s/keys/secret_onion_key",get_data_directory(&options));
-  sprintf(fname_prev,"%s/keys/secret_onion_key.old",
-          get_data_directory(&options));
+  snprintf(fname,sizeof(fname),
+           "%s/keys/secret_onion_key",get_data_directory(&options));
+  snprintf(fname_prev,sizeof(fname_prev),
+           "%s/keys/secret_onion_key.old",get_data_directory(&options));
   if (!(prkey = crypto_new_pk_env())) {
     log(LOG_ERR, "Error creating crypto environment.");
     goto error;
@@ -263,27 +264,27 @@
     return -1;
   }
   /* Check the key directory. */
-  sprintf(keydir,"%s/keys", datadir);
+  snprintf(keydir,sizeof(keydir),"%s/keys", datadir);
   if (check_private_dir(keydir, 1)) {
     return -1;
   }
   cp = keydir + strlen(keydir); /* End of string. */
 
   /* 1. Read identity key. Make it if none is found. */
-  sprintf(keydir,"%s/keys/identity.key",datadir);
-  sprintf(keydir2,"%s/keys/secret_id_key",datadir);
+  snprintf(keydir,sizeof(keydir),"%s/keys/identity.key",datadir);
+  snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_id_key",datadir);
   log_fn(LOG_INFO,"Reading/making identity key %s...",keydir2);
   prkey = init_key_from_file_name_changed(keydir,keydir2);
   if (!prkey) return -1;
   set_identity_key(prkey);
   /* 2. Read onion key.  Make it if none is found. */
-  sprintf(keydir,"%s/keys/onion.key",datadir);
-  sprintf(keydir2,"%s/keys/secret_onion_key",datadir);
+  snprintf(keydir,sizeof(keydir),"%s/keys/onion.key",datadir);
+  snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_onion_key",datadir);
   log_fn(LOG_INFO,"Reading/making onion key %s...",keydir2);
   prkey = init_key_from_file_name_changed(keydir,keydir2);
   if (!prkey) return -1;
   set_onion_key(prkey);
-  sprintf(keydir,"%s/keys/secret_onion_key.old",datadir);
+  snprintf(keydir,sizeof(keydir),"%s/keys/secret_onion_key.old",datadir);
   if (file_status(keydir) == FN_FILE) {
     prkey = init_key_from_file(keydir);
     if (prkey)
@@ -315,13 +316,13 @@
     }
   }
 
-  sprintf(keydir,"%s/router.desc", datadir);
+  snprintf(keydir,sizeof(keydir),"%s/router.desc", datadir);
   log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
   if (write_str_to_file(keydir, mydesc,0)) {
     return -1;
   }
   /* 5. Dump fingerprint to 'fingerprint' */
-  sprintf(keydir,"%s/fingerprint", datadir);
+  snprintf(keydir,sizeof(keydir),"%s/fingerprint", datadir);
   log_fn(LOG_INFO,"Dumping fingerprint to %s...",keydir);
   tor_assert(strlen(options.Nickname) <= MAX_NICKNAME_LEN);
   strcpy(fingerprint, options.Nickname);
@@ -337,7 +338,7 @@
   if(!authdir_mode())
     return 0;
   /* 6. [authdirserver only] load approved-routers file */
-  sprintf(keydir,"%s/approved-routers", datadir);
+  snprintf(keydir,sizeof(keydir),"%s/approved-routers", datadir);
   log_fn(LOG_INFO,"Loading approved fingerprints from %s...",keydir);
   if(dirserv_parse_fingerprint_file(keydir) < 0) {
     log_fn(LOG_ERR, "Error loading fingerprints");
@@ -349,7 +350,7 @@
     add_trusted_dir_server(options.Address, (uint16_t)options.DirPort, digest);
   }
   /* 7. [authdirserver only] load old directory, if it's there */
-  sprintf(keydir,"%s/cached-directory", datadir);
+  snprintf(keydir,sizeof(keydir),"%s/cached-directory", datadir);
   log_fn(LOG_INFO,"Loading cached directory from %s...",keydir);
   cp = read_file_to_str(keydir,0);
   if(!cp) {

Index: routerlist.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routerlist.c,v
retrieving revision 1.166
retrieving revision 1.167
diff -u -d -r1.166 -r1.167
--- routerlist.c	27 Oct 2004 00:48:51 -0000	1.166
+++ routerlist.c	27 Oct 2004 05:53:07 -0000	1.167
@@ -167,7 +167,7 @@
     if(requireothers && router_is_me(router))
       continue;
     if(fascistfirewall) {
-      sprintf(buf,"%d",router->dir_port);
+      snprintf(buf,sizeof(buf),"%d",router->dir_port);
       if (!smartlist_string_isin(options.FirewallPorts, buf))
         continue;
     }
@@ -202,7 +202,7 @@
           !memcmp(me->identity_digest, d->digest, DIGEST_LEN))
         continue;
       if (fascistfirewall) {
-        sprintf(buf,"%d",d->dir_port);
+        snprintf(buf,sizeof(buf),"%d",d->dir_port);
         if (!smartlist_string_isin(options.FirewallPorts, buf))
           continue;
       }

Index: test.c
===================================================================
RCS file: /home/or/cvsroot/src/or/test.c,v
retrieving revision 1.133
retrieving revision 1.134
diff -u -d -r1.133 -r1.134
--- test.c	20 Oct 2004 23:30:38 -0000	1.133
+++ test.c	27 Oct 2004 05:53:07 -0000	1.134
@@ -50,7 +50,7 @@
   int r;
   if (is_setup) return;
 
-  sprintf(temp_dir, "/tmp/tor_test_%d", (int) getpid());
+  snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
 #ifdef MS_WINDOWS
   r = mkdir(temp_dir);
 #else
@@ -69,7 +69,7 @@
 {
   static char buf[1024];
   setup_directory();
-  sprintf(buf,"%s/%s",temp_dir,name);
+  snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
   return buf;
 }
 



More information about the tor-commits mailing list