]> git.michaelhowe.org Git - packages/o/openafs.git/commitdiff
DEVEL15-windows-osilog-safestring-unicode-20080624
authorAsanka Herath <asanka@secure-endpoints.com>
Thu, 26 Jun 2008 17:00:53 +0000 (17:00 +0000)
committerJeffrey Altman <jaltman@secure-endpoints.com>
Thu, 26 Jun 2008 17:00:53 +0000 (17:00 +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.

(cherry picked from commit ef58ff2ea67323e5260484597abe6bdfa63d8c72)

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

index 22e780e91379a14d83e35f67b57caf121501ae66..555d22fc96eba43985734185b29c6df9a2e08221 100644 (file)
@@ -71,3 +71,4 @@ EXPORTS
         osi_HexifyString        @64
         osi_QRemoveHT           @65
        lock_ConvertRToW        @66
+        osi_LogSaveStringW      @67
index 91c2f6ebe722122a2e3c238addf551223c3bfe7e..9b5beb16318077a05a77545dbdbc1f7457d5e964 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdio.h>
 #include <assert.h>
 #include <WINNT\afsreg.h>
+#include <strsafe.h>
 
 #define AFS_DAEMON_EVENT_NAME "TransarcAFSDaemon"
 
@@ -89,9 +90,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 */
@@ -109,8 +113,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);
 #ifndef DJGPP
        if (typep) {
@@ -224,11 +228,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);
         }
 
@@ -249,20 +253,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);
-#ifndef DJGPP
+               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))
-#else /* DJGPP */
-                if ((ioCount = fwrite(wholemsg, 1, strlen(wholemsg), handle)) == 0)
-#endif /* !DJGPP */
+                               &ioCount, NULL))
                        break;
        }
 
@@ -271,31 +271,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) 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)
+        StringCbPrintfA(saveplace, OSI_LOG_STRINGSIZE,
+                        "...%s",
+                        s + strlen(s) - (OSI_LOG_STRINGSIZE - 4));
+    else
+        StringCbCopyA(saveplace, OSI_LOG_STRINGSIZE, s);
 
-       if (strlen(s) >= OSI_LOG_STRINGSIZE)
-               sprintf(saveplace, "...%s",
-                       s + strlen(s) - (OSI_LOG_STRINGSIZE - 4));
-       else
-               strcpy(saveplace, s);
-       logp->stringindex++;
+    logp->stringindex++;
 
-       if (logp->stringindex >= logp->maxstringindex)
-           logp->stringindex = 0;
+    if (logp->stringindex >= logp->maxstringindex)
+        logp->stringindex = 0;
 
-        thrd_LeaveCrit(&logp->cs);
+    thrd_LeaveCrit(&logp->cs);
 
-       return saveplace;
+    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)
@@ -315,45 +347,43 @@ long osi_LogFDCreate(osi_fdType_t *typep, osi_fd_t **outpp)
         return 0;
 }
 
-#ifndef DJGPP
 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;
 }
-#endif /* !DJGPP */
 
 long osi_LogFDClose(osi_fd_t *ifdp)
 {
@@ -407,7 +437,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 e9ced175092108f14f3771485f17477e775e9741..6ec6f58cb9d851ac362c2068a9acd9b0b9931c18 100644 (file)
@@ -19,7 +19,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 {
@@ -32,7 +32,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
@@ -79,6 +79,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);