[or-cvs] Avoid strcat; use snprintf or strlcat instead

Nick Mathewson nickm at seul.org
Wed Oct 27 06:25:32 UTC 2004


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

Modified Files:
	rephist.c router.c routerparse.c 
Log Message:
Avoid strcat; use snprintf or strlcat instead

Index: rephist.c
===================================================================
RCS file: /home/or/cvsroot/src/or/rephist.c,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -d -r1.31 -r1.32
--- rephist.c	27 Oct 2004 05:53:07 -0000	1.31
+++ rephist.c	27 Oct 2004 06:25:29 -0000	1.32
@@ -307,8 +307,7 @@
   const char *name1;
 
   tmpfile = tor_malloc(strlen(filename)+5);
-  strcpy(tmpfile, filename);
-  strcat(tmpfile, "_tmp");
+  snprintf(tmpfile, strlen(filename)+5, "%s_tmp", filename);
 
   f = fopen(tmpfile, "w");
   if (!f) goto done;
@@ -548,7 +547,7 @@
         snprintf(cp, len-(cp-buf), "%d,", b->totals[i]);
       cp += strlen(cp);
     }
-    strcat(cp, "\n");
+    strlcat(cp, "\n", len-(cp-buf));
     ++cp;
   }
   return buf;

Index: router.c
===================================================================
RCS file: /home/or/cvsroot/src/or/router.c,v
retrieving revision 1.103
retrieving revision 1.104
diff -u -d -r1.103 -r1.104
--- router.c	27 Oct 2004 05:53:07 -0000	1.103
+++ router.c	27 Oct 2004 06:25:29 -0000	1.104
@@ -325,14 +325,14 @@
   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);
-  strcat(fingerprint, " ");
+  strlcpy(fingerprint, options.Nickname, sizeof(fingerprint));
+  strlcat(fingerprint, " ", sizeof(fingerprint));
   if (crypto_pk_get_fingerprint(get_identity_key(),
                                 fingerprint+strlen(fingerprint), 1)<0) {
     log_fn(LOG_ERR, "Error computing fingerprint");
     return -1;
   }
-  strcat(fingerprint, "\n");
+  strlcat(fingerprint, "\n", sizeof(fingerprint));
   if (write_str_to_file(keydir, fingerprint, 0))
     return -1;
   if(!authdir_mode())
@@ -717,7 +717,7 @@
       /* There is no port set; write ":*" */
       if (written > maxlen-4)
         return -1;
-      strcat(s+written, ":*\n");
+      strlcat(s+written, ":*\n", maxlen-written);
       written += 3;
     } else if (tmpe->prt_min == tmpe->prt_max) {
       /* There is only one port; write ":80". */
@@ -741,7 +741,7 @@
     return -1;
 
   /* Sign the directory */
-  strcat(s+written, "router-signature\n");
+  strlcat(s+written, "router-signature\n", maxlen-written);
   written += strlen(s+written);
   s[written] = '\0';
   if (router_get_router_hash(s, digest) < 0)
@@ -751,14 +751,14 @@
     log_fn(LOG_WARN, "Error signing digest");
     return -1;
   }
-  strcat(s+written, "-----BEGIN SIGNATURE-----\n");
+  strlcat(s+written, "-----BEGIN SIGNATURE-----\n", maxlen-written);
   written += strlen(s+written);
   if (base64_encode(s+written, maxlen-written, signature, 128) < 0) {
     log_fn(LOG_WARN, "Couldn't base64-encode signature");
     return -1;
   }
   written += strlen(s+written);
-  strcat(s+written, "-----END SIGNATURE-----\n");
+  strlcat(s+written, "-----END SIGNATURE-----\n", maxlen-written);
   written += strlen(s+written);
 
   if (written > maxlen-2)

Index: routerparse.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routerparse.c,v
retrieving revision 1.64
retrieving revision 1.65
diff -u -d -r1.64 -r1.65
--- routerparse.c	27 Oct 2004 00:48:51 -0000	1.64
+++ routerparse.c	27 Oct 2004 06:25:29 -0000	1.65
@@ -1032,14 +1032,10 @@
   newe = tor_malloc_zero(sizeof(struct exit_policy_t));
 
   newe->string = tor_malloc(8+strlen(arg));
-  if (tok->tp == K_REJECT) {
-    strcpy(newe->string, "reject ");
-    newe->policy_type = EXIT_POLICY_REJECT;
-  } else {
-    strcpy(newe->string, "accept ");
-    newe->policy_type = EXIT_POLICY_ACCEPT;
-  }
-  strcat(newe->string, arg); /* can't overflow */
+  snprintf(newe->string, 8+strlen(arg), "%s %s",
+           (tok->tp == K_REJECT) ? "reject" : "accept", arg);
+  newe->policy_type = (tok->tp == K_REJECT) ? EXIT_POLICY_REJECT
+    : EXIT_POLICY_ACCEPT;
 
   if (parse_addr_and_port_range(arg, &newe->addr, &newe->msk,
                                 &newe->prt_min, &newe->prt_max))



More information about the tor-commits mailing list