[or-cvs] Use tor_snprintf, not snprintf
Nick Mathewson
nickm at seul.org
Wed Oct 27 06:37:36 UTC 2004
Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv6462/src/common
Modified Files:
log.c tortls.c util.c
Log Message:
Use tor_snprintf, not snprintf
Index: log.c
===================================================================
RCS file: /home/or/cvsroot/src/common/log.c,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -d -r1.56 -r1.57
--- log.c 27 Oct 2004 05:53:07 -0000 1.56
+++ log.c 27 Oct 2004 06:37:34 -0000 1.57
@@ -16,12 +16,6 @@
#include "./util.h"
#include "./log.h"
-
-#ifdef MS_WINDOWS
-#define vsnprintf _vsnprintf
-#define snprintf _snprintf
-#endif
-
#define TRUNCATED_STR "[...truncated]"
#define TRUNCATED_STR_LEN 14
@@ -65,19 +59,19 @@
time_t t;
struct timeval now;
size_t n;
+ int r;
tor_gettimeofday(&now);
t = (time_t)now.tv_sec;
n = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));
- n += snprintf(buf+n, buf_len-n,
+ r = tor_snprintf(buf+n, buf_len-n,
".%.3ld [%s] ",
(long)now.tv_usec / 1000, sev_to_string(severity));
- if(n > buf_len)
- n = buf_len-1; /* the *nprintf funcs return how many bytes they
- * _would_ print, if the output is truncated.
- * Subtract one because the count doesn't include the \0 */
- return n;
+ if (r<0)
+ return buf_len-1;
+ else
+ return n+r;
}
/** If lf refers to an actual file that we have just opened, and the file
@@ -99,12 +93,9 @@
/* We are resetting, but we aren't at the start of the file; no
* need to log again. */
return;
- n = _log_prefix(buf, 250, LOG_NOTICE);
- n += snprintf(buf+n, 250-n, "Tor %s opening %slog file.\n", VERSION,
- is_new?"new ":"");
- if (n>250)
- n = 250;
- buf[n+1]='\0';
+ n = _log_prefix(buf, sizeof(buf), LOG_NOTICE);
+ tor_snprintf(buf+n, sizeof(buf)-n,
+ "Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
fputs(buf, lf->file);
}
@@ -118,6 +109,7 @@
const char *format, va_list ap)
{
size_t n;
+ int r;
char *end_of_prefix;
buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
@@ -125,15 +117,18 @@
end_of_prefix = buf+n;
if (funcname) {
- n += snprintf(buf+n, buf_len-n, "%s(): ", funcname);
- if(n > buf_len)
- n = buf_len-1;
+ r = tor_snprintf(buf+n, buf_len-n, "%s(): ", funcname);
+ if (r<0)
+ n = strlen(buf);
+ else
+ n += r;
}
-
- n += vsnprintf(buf+n,buf_len-n,format,ap);
- if(n > buf_len) {
- n = buf_len-1;
- strcpy(buf+n-TRUNCATED_STR_LEN, TRUNCATED_STR);
+
+ n += tor_vsnprintf(buf+n,buf_len-n,format,ap);
+ if(n < 0) {
+ n = buf_len-2;
+ strlcpy(buf+buf_len-TRUNCATED_STR_LEN-1, TRUNCATED_STR,
+ buf_len-(buf_len-TRUNCATED_STR_LEN-1));
}
buf[n]='\n';
buf[n+1]='\0';
@@ -142,7 +137,7 @@
/** Helper: sends a message to the appropriate logfiles, at loglevel
* <b>severity</b>. If provided, <b>funcname</b> is prepended to the
- * message. The actual message is derived as from vsnprintf(format,ap).
+ * message. The actual message is derived as from tor_snprintf(format,ap).
*/
static void
logv(int severity, const char *funcname, const char *format, va_list ap)
Index: tortls.c
===================================================================
RCS file: /home/or/cvsroot/src/common/tortls.c,v
retrieving revision 1.70
retrieving revision 1.71
diff -u -d -r1.70 -r1.71
--- tortls.c 27 Oct 2004 05:53:07 -0000 1.70
+++ tortls.c 27 Oct 2004 06:37:34 -0000 1.71
@@ -299,7 +299,7 @@
SSL_CTX **ctx;
if (!nickname)
nickname = "null";
- snprintf(nn2, sizeof(nn2), "%s <identity>", nickname);
+ tor_snprintf(nn2, sizeof(nn2), "%s <identity>", nickname);
tor_tls_init();
Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.155
retrieving revision 1.156
diff -u -d -r1.155 -r1.156
--- util.c 27 Oct 2004 06:26:23 -0000 1.155
+++ util.c 27 Oct 2004 06:37:34 -0000 1.156
@@ -1773,7 +1773,7 @@
* Round up to 16 in case we can't do math. */
len = strlen(home)+strlen(filename)+16;
result = tor_malloc(len);
- snprintf(result,len,"%s/%s",home,filename+2);
+ tor_snprintf(result,len,"%s/%s",home,filename+2);
return result;
} else {
return tor_strdup(filename);
@@ -1847,7 +1847,7 @@
#ifdef HAVE_UNAME
if (uname(&u) != -1) {
/* (linux says 0 is success, solaris says 1 is success) */
- snprintf(uname_result, 255, "%s %s %s",
+ tor_snprintf(uname_result, 255, "%s %s %s",
u.sysname, u.nodename, u.machine);
uname_result[255] = '\0';
} else
More information about the tor-commits
mailing list