windows-osilog-safestring-unicode-20080624
authorAsanka Herath <asanka@secure-endpoints.com>
Tue, 24 Jun 2008 14:41:13 +0000 (14:41 +0000)
committerJeffrey Altman <jaltman@secure-endpoints.com>
Tue, 24 Jun 2008 14:41:13 +0000 (14:41 +0000)
LICENSE MIT

convert to using microsoft safe string functions.

add Unicode version of osi_LogSaveStringW.  This is safe to use export
even in none Unicode builds.

Some reformatting.

src/WINNT/client_osi/libosi.def
src/WINNT/client_osi/osilog.c
src/WINNT/client_osi/osilog.h

index 22e780e..555d22f 100644 (file)
@@ -71,3 +71,4 @@ EXPORTS
         osi_HexifyString        @64
         osi_QRemoveHT           @65
        lock_ConvertRToW        @66
+        osi_LogSaveStringW      @67
index 8d4d566..e56ea00 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <assert.h>
 #include <WINNT\afsreg.h>
+#include <strsafe.h>
 
 #define AFS_DAEMON_EVENT_NAME "TransarcAFSDaemon"
 
@@ -81,9 +82,12 @@ osi_log_t *osi_LogCreate(char *namep, size_t size)
 
         logp = malloc(sizeof(osi_log_t));
         memset(logp, 0, sizeof(osi_log_t));
-        logp->namep = malloc(strlen(namep)+1);
-        strcpy(logp->namep, namep);
-        
+        {
+            size_t namelen = strlen(namep) + 1;
+
+            logp->namep = malloc(namelen * sizeof(char));
+            StringCchCopyA(logp->namep, namelen, namep);
+        }
         osi_QAdd((osi_queue_t **) &osi_allLogsp, &logp->q);
 
        /* compute size we'll use */
@@ -101,8 +105,8 @@ osi_log_t *osi_LogCreate(char *namep, size_t size)
         /* and sync */
         thrd_InitCrit(&logp->cs);
         
-       strcpy(tbuffer, "log:");
-        strcat(tbuffer, namep);
+       StringCbCopyA(tbuffer, sizeof(tbuffer), "log:");
+        StringCbCatA(tbuffer, sizeof(tbuffer), namep);
        typep = osi_RegisterFDType(tbuffer, &osi_logFDOps, logp);
        if (typep) {
                /* add formatting info */
@@ -210,11 +214,11 @@ void osi_LogAdd(osi_log_t *logp, char *formatp, size_t p0, size_t p1, size_t p2,
         if(ISCLIENTDEBUGLOG(osi_TraceOption)) {
            char wholemsg[1024], msg[1000];
 
-           snprintf(msg, sizeof(msg), formatp,
-                    p0, p1, p2, p3);
-           snprintf(wholemsg, sizeof(wholemsg), 
-                    "tid[%d] %s\n",
-                    lep->tid, msg);
+           StringCbPrintfA(msg, sizeof(msg), formatp,
+                            p0, p1, p2, p3);
+           StringCbPrintfA(wholemsg, sizeof(wholemsg), 
+                            "tid[%d] %s\n",
+                            lep->tid, msg);
             OutputDebugStringA(wholemsg);
         }
 
@@ -235,16 +239,16 @@ void osi_LogPrint(osi_log_t *logp, FILE_HANDLE handle)
             i < logp->nused;
             i++, ix++, (ix >= logp->alloc ? ix -= logp->alloc : 0)) {
                lep = logp->datap + ix;         /* pointer arithmetic */
-               snprintf(msg, sizeof(msg), lep->formatp,
-                       lep->parms[0], lep->parms[1],
-                       lep->parms[2], lep->parms[3]);
-               snprintf(wholemsg, sizeof(wholemsg),
-                        "time %d.%06d, tid %d %s\r\n",
-                       lep->micros / 1000000,
-                       lep->micros % 1000000,
-                       lep->tid, msg);
+               StringCbPrintfA(msg, sizeof(msg), lep->formatp,
+                                lep->parms[0], lep->parms[1],
+                                lep->parms[2], lep->parms[3]);
+               StringCbPrintfA(wholemsg, sizeof(wholemsg),
+                                "time %d.%06d, tid %d %s\r\n",
+                                lep->micros / 1000000,
+                                lep->micros % 1000000,
+                                lep->tid, msg);
                if (!WriteFile(handle, wholemsg, strlen(wholemsg),
-                               &ioCount, NULL))
+                               &ioCount, NULL))
                        break;
        }
 
@@ -253,31 +257,63 @@ void osi_LogPrint(osi_log_t *logp, FILE_HANDLE handle)
 
 char *osi_LogSaveString(osi_log_t *logp, char *s)
 {
-       char *saveplace;
+    char *saveplace;
 
-        if (!logp) return s;
+    if (!logp) return s;
 
-        if (!logp->enabled) return s;
+    if (!logp->enabled) return s;
 
-       if (s == NULL) return NULL;
+    if (s == NULL) return NULL;
 
-        thrd_EnterCrit(&logp->cs);
+    thrd_EnterCrit(&logp->cs);
 
-        saveplace = logp->stringsp[logp->stringindex];
+    saveplace = logp->stringsp[logp->stringindex];
 
-       if (strlen(s) >= OSI_LOG_STRINGSIZE)
-               sprintf(saveplace, "...%s",
-                       s + strlen(s) - (OSI_LOG_STRINGSIZE - 4));
-       else
-               strcpy(saveplace, s);
-       logp->stringindex++;
+    if (strlen(s) >= OSI_LOG_STRINGSIZE)
+        StringCbPrintfA(saveplace, OSI_LOG_STRINGSIZE,
+                        "...%s",
+                        s + strlen(s) - (OSI_LOG_STRINGSIZE - 4));
+    else
+        StringCbCopyA(saveplace, OSI_LOG_STRINGSIZE, s);
 
-       if (logp->stringindex >= logp->maxstringindex)
-           logp->stringindex = 0;
+    logp->stringindex++;
 
-        thrd_LeaveCrit(&logp->cs);
+    if (logp->stringindex >= logp->maxstringindex)
+        logp->stringindex = 0;
 
-       return saveplace;
+    thrd_LeaveCrit(&logp->cs);
+
+    return saveplace;
+}
+
+wchar_t *osi_LogSaveStringW(osi_log_t *logp, wchar_t *s)
+{
+    wchar_t *saveplace;
+
+    if (!logp) return s;
+
+    if (!logp->enabled) return s;
+
+    if (s == NULL) return NULL;
+
+    thrd_EnterCrit(&logp->cs);
+
+    saveplace = (wchar_t *) (logp->stringsp[logp->stringindex]);
+
+    if (wcslen(s)*sizeof(wchar_t) >= OSI_LOG_STRINGSIZE)
+        StringCbPrintfW(saveplace, OSI_LOG_STRINGSIZE, L"...%s",
+                        (s + wcslen(s) - (OSI_LOG_STRINGSIZE/sizeof(wchar_t) - 4)));
+    else
+        StringCbCopyW(saveplace, OSI_LOG_STRINGSIZE, s);
+
+    logp->stringindex++;
+
+    if (logp->stringindex >= logp->maxstringindex)
+        logp->stringindex = 0;
+
+    thrd_LeaveCrit(&logp->cs);
+
+    return saveplace;
 }
 
 long osi_LogFDCreate(osi_fdType_t *typep, osi_fd_t **outpp)
@@ -299,40 +335,40 @@ long osi_LogFDCreate(osi_fdType_t *typep, osi_fd_t **outpp)
 
 long osi_LogFDGetInfo(osi_fd_t *ifd, osi_remGetInfoParms_t *outp)
 {
-       osi_logFD_t *lfdp;
-        osi_log_t *logp;
-        osi_logEntry_t *lep;
-        char tbuffer[256];
-        long ix;
+    osi_logFD_t *lfdp;
+    osi_log_t *logp;
+    osi_logEntry_t *lep;
+    char tbuffer[256];
+    long ix;
         
-        lfdp = (osi_logFD_t *) ifd;
-        logp = lfdp->logp;
+    lfdp = (osi_logFD_t *) ifd;
+    logp = lfdp->logp;
         
-       /* see if we're done */
-       if (lfdp->current >= lfdp->nused) return OSI_DBRPC_EOF;
+    /* see if we're done */
+    if (lfdp->current >= lfdp->nused) return OSI_DBRPC_EOF;
         
-       /* grab lock */
-       thrd_EnterCrit(&logp->cs);
-
-        /* compute which one we want */
-        ix = lfdp->first + lfdp->current;
-        if (ix >= logp->alloc) ix -= logp->alloc;
-        lfdp->current++;
-        lep = logp->datap + ix;        /* ptr arith to current index */
-
-       sprintf(tbuffer, lep->formatp, lep->parms[0], lep->parms[1],
-               lep->parms[2], lep->parms[3]);
-
-       /* now copy out info */
-        strcpy(outp->sdata[0], tbuffer);
-        sprintf(tbuffer, "%5.6f", ((double)lep->micros)/1000000.0);
-        strcpy(outp->sdata[1], tbuffer);
-        outp->idata[0] = lep->tid;
-        outp->scount = 2;
-        outp->icount = 1;
-
-       thrd_LeaveCrit(&logp->cs);
-        return 0;
+    /* grab lock */
+    thrd_EnterCrit(&logp->cs);
+
+    /* compute which one we want */
+    ix = lfdp->first + lfdp->current;
+    if (ix >= logp->alloc) ix -= logp->alloc;
+    lfdp->current++;
+    lep = logp->datap + ix;    /* ptr arith to current index */
+
+    StringCbPrintfA(tbuffer, sizeof(tbuffer), lep->formatp, lep->parms[0], lep->parms[1],
+                    lep->parms[2], lep->parms[3]);
+
+    /* now copy out info */
+    StringCbCopyA(outp->sdata[0], sizeof(outp->sdata[0]), tbuffer);
+    StringCbPrintfA(tbuffer, sizeof(tbuffer), "%5.6f", ((double)lep->micros)/1000000.0);
+    StringCbCopyA(outp->sdata[1], sizeof(outp->sdata[0]), tbuffer);
+    outp->idata[0] = lep->tid;
+    outp->scount = 2;
+    outp->icount = 1;
+
+    thrd_LeaveCrit(&logp->cs);
+    return 0;
 }
 
 long osi_LogFDClose(osi_fd_t *ifdp)
@@ -387,7 +423,7 @@ void osi_LogEvent(char *a,char *b,char *c,...)
                return;
     h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
        va_start(marker,c);
-       _vsnprintf(buf,MAXBUF_,c,marker);
+       StringCbVPrintfA(buf,MAXBUF_,c,marker);
        ptbuf[0] = buf;
        ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (const char **)ptbuf, NULL);\
        DeregisterEventSource(h);
index 3ee612f..3ecee9f 100644 (file)
@@ -17,7 +17,7 @@
 #include "osiqueue.h"
 
 #define OSI_LOG_DEFAULTSIZE    1000
-#define OSI_LOG_STRINGSIZE     128
+#define OSI_LOG_STRINGSIZE     256
 #define OSI_LOG_MAXPARMS       4       /* max # of int parms */
 
 typedef struct osi_logEntry {
@@ -30,7 +30,7 @@ typedef struct osi_logEntry {
 typedef struct osi_log {
        osi_queue_t q;                  /* queue of all logs */
        char *namep;                    /* name */
-       size_t alloc;                   /* allocated size */
+        long alloc;                    /* allocated size */
         long nused;                    /* number currently in use */
         long first;                    /* index of first entry */
         Crit_Sec cs;                   /* use this, rather than a higher-level
@@ -75,6 +75,7 @@ extern void osi_LogPanic(char *filep, size_t line);
 
 extern void osi_LogPrint(osi_log_t *logp, FILE_HANDLE handle);
 
+extern wchar_t *osi_LogSaveStringW(osi_log_t *logp, wchar_t *s);
 extern char *osi_LogSaveString(osi_log_t *logp, char *s);
 extern void osi_InitTraceOption();
 extern void osi_LogEvent0(char *a,char *b);