diff -ur /tmp/e/base/src/as/asLibRoutines.c ./as/asLibRoutines.c --- /tmp/e/base/src/as/asLibRoutines.c 2010-03-31 10:13:33.000000000 -0700 +++ ./as/asLibRoutines.c 2010-03-31 10:08:13.000000000 -0700 @@ -201,7 +201,7 @@ fgetsRtn = fgets(mac_input_buffer,BUF_SIZE,stream); if(fgetsRtn) { n = macExpandString(macHandle,mac_input_buffer, - my_buffer,BUF_SIZE-1); + my_buffer,BUF_SIZE); if(n<0) { epicsPrintf("access security: macExpandString failed\n" "input line: %s\n",mac_input_buffer); diff -ur /tmp/e/base/src/dbStatic/dbLexRoutines.c ./dbStatic/dbLexRoutines.c --- /tmp/e/base/src/dbStatic/dbLexRoutines.c 2010-03-31 10:13:43.000000000 -0700 +++ ./dbStatic/dbLexRoutines.c 2010-03-31 10:07:32.000000000 -0700 @@ -312,7 +312,7 @@ pinputFileNow->fp); if(fgetsRtn) { n = macExpandString(macHandle,mac_input_buffer, - my_buffer,MY_BUFFER_SIZE-1); + my_buffer,MY_BUFFER_SIZE); if(n<0) { errPrintf(0,__FILE__, __LINE__, "macExpandString failed for file %s", diff -ur /tmp/e/base/src/libCom/macLib/macCore.c ./libCom/macLib/macCore.c --- /tmp/e/base/src/libCom/macLib/macCore.c 2010-03-31 10:13:49.000000000 -0700 +++ ./libCom/macLib/macCore.c 2010-03-31 10:11:42.000000000 -0700 @@ -69,11 +69,11 @@ static long expand( MAC_HANDLE *handle ); static void trans ( MAC_HANDLE *handle, MAC_ENTRY *entry, int level, const char *term, const char **rawval, char **value, - char *valend ); + char *lastValid ); static void refer ( MAC_HANDLE *handle, MAC_ENTRY *entry, int level, - const char **rawval, char **value, char *valend ); + const char **rawval, char **value, char *lastValid ); -static void cpy2val( const char *src, char **value, char *valend ); +static void cpy2val( const char *src, char **value, char *lastValid ); static char *Strdup( const char *string ); @@ -180,8 +180,7 @@ char *dest, /* destination string */ - long maxlen ) /* maximum number of characters to copy */ - /* to destination string */ + long capacity ) /* capacity of destination buffer (dest) */ { MAC_ENTRY entry; const char *s; @@ -196,7 +195,11 @@ /* debug output */ if ( handle->debug & 1 ) - printf( "macExpandString( %s, maxlen = %ld )\n", src, maxlen ); + printf( "macExpandString( %s, capacity = %ld )\n", src, capacity ); + + /* Check size */ + if (capacity <= 1) + return -1; /* expand raw values if necessary */ if ( expand( handle ) < 0 ) @@ -211,7 +214,7 @@ s = src; d = dest; *d = '\0'; - trans( handle, &entry, 0, "", &s, &d, d + maxlen ); + trans( handle, &entry, 0, "", &s, &d, d + capacity - 1 ); /* return +/- #chars copied depending on successful expansion */ length = d - dest; @@ -295,8 +298,7 @@ char *value, /* string to receive macro value or name */ /* argument if macro is undefined */ - long maxlen ) /* maximum number of characters to copy */ - /* to value */ + long capacity ) /* capcity of destination buffer (value) */ { MAC_ENTRY *entry; /* pointer to this macro's entry structure */ long length; /* number of characters returned */ @@ -314,31 +316,31 @@ /* look up macro name */ entry = lookup( handle, name, FALSE ); - /* if maxlen <= 0 or VALUE == NULL just return -1 / 0 for undefined / + /* if capacity <= 1 or VALUE == NULL just return -1 / 0 for undefined / defined macro */ - if ( maxlen <= 0 || value == NULL ) { + if ( capacity <= 1 || value == NULL ) { return ( entry == NULL ) ? -1 : 0; } /* if not found, copy name to value and return minus #chars copied */ if ( entry == NULL ) { - strncpy( value, name, maxlen ); - return ( value[maxlen-1] == '\0' ) ? - (long) strlen( name ) : -maxlen; + strncpy( value, name, capacity ); + return ( value[capacity-1] == '\0' ) ? - (long) strlen( name ) : -capacity; } /* expand raw values if necessary; if fail (can only fail because of memory allocation failure), return same as if not found */ if ( expand( handle ) < 0 ) { errlogPrintf( "macGetValue: failed to expand raw values\n" ); - strncpy( value, name, maxlen ); - return ( value[maxlen-1] == '\0' ) ? - (long) strlen( name ) : -maxlen; + strncpy( value, name, capacity ); + return ( value[capacity-1] == '\0' ) ? - (long) strlen( name ) : -capacity; } /* copy value and return +/- #chars copied depending on successful expansion */ /* FIXME: nul-terminator */ - strncpy( value, entry->value, maxlen ); - length = ( value[maxlen-1] == '\0' ) ? entry->length : maxlen; + strncpy( value, entry->value, capacity ); + length = ( value[capacity-1] == '\0' ) ? entry->length : capacity; return ( entry->error ) ? -length : length; } @@ -677,7 +679,7 @@ */ static void trans( MAC_HANDLE *handle, MAC_ENTRY *entry, int level, const char *term, const char **rawval, char **value, - char *valend ) + char *lastValid ) { char quote; const char *r; @@ -694,8 +696,8 @@ /* debug output */ if ( handle->debug & 2 ) - printf( "trans-> entry = %p, level = %d, maxlen = %u, discard = %s, " - "rawval = %s\n", entry, level, (unsigned int)(valend - *value), discard ? "T" : "F", *rawval ); + printf( "trans-> entry = %p, level = %d, capacity = %u, discard = %s, " + "rawval = %s\n", entry, level, (unsigned int)(lastValid - *value), discard ? "T" : "F", *rawval ); /* initially not in quotes */ quote = 0; @@ -723,23 +725,23 @@ /* macros are not expanded in single quotes */ if ( macRef && quote != '\'' ) { /* Handle macro reference */ - refer ( handle, entry, level, &r, &v, valend ); + refer ( handle, entry, level, &r, &v, lastValid ); } else { /* handle escaped characters (escape is discarded if in name) */ if ( *r == '\\' && *( r + 1 ) != '\0' ) { - if ( v < valend && !discard ) *v++ = '\\'; - if ( v < valend ) *v++ = *++r; + if ( v < lastValid && !discard ) *v++ = '\\'; + if ( v < lastValid ) *v++ = *++r; } /* copy character to output */ else { - if ( v < valend ) *v++ = *r; + if ( v < lastValid ) *v++ = *r; } /* ensure string remains properly terminated */ - if ( v <= valend ) *v = '\0'; + if ( v <= lastValid ) *v = '\0'; } } @@ -763,7 +765,7 @@ * pulled out for ease of understanding. */ static void refer ( MAC_HANDLE *handle, MAC_ENTRY *entry, int level, - const char **rawval, char **value, char *valend ) + const char **rawval, char **value, char *lastValid ) { const char *r = *rawval; char *v = *value; @@ -777,8 +779,8 @@ /* debug output */ if ( handle->debug & 2 ) - printf( "refer-> entry = %p, level = %d, maxlen = %u, rawval = %s\n", - entry, level, (unsigned int)(valend - *value), *rawval ); + printf( "refer-> entry = %p, level = %d, capacity = %u, rawval = %s\n", + entry, level, (unsigned int)(lastValid - *value), *rawval ); /* step over '$(' or '${' */ r++; @@ -858,13 +860,13 @@ /* reference is good, use it */ if ( !handle->dirty ) { /* copy the already-expanded value, and its error status! */ - cpy2val( refentry->value, &v, valend ); + cpy2val( refentry->value, &v, lastValid ); entry->error = refentry->error; } else { /* translate raw value */ const char *rv = refentry->rawval; refentry->visited = TRUE; - trans( handle, entry, level + 1, "", &rv, &v, valend ); + trans( handle, entry, level + 1, "", &rv, &v, lastValid ); refentry->visited = FALSE; } goto cleanup; @@ -881,7 +883,7 @@ /* no macro found by this name */ if ( defval ) { /* there was a default value, translate that instead */ - trans( handle, entry, level + 1, macEnd+1, &defval, &v, valend ); + trans( handle, entry, level + 1, macEnd+1, &defval, &v, lastValid ); goto cleanup; } entry->error = TRUE; @@ -893,10 +895,10 @@ } /* Bad reference, insert $(name,errval) */ - if ( v < valend ) *v++ = '$'; - if ( v < valend ) *v++ = '('; - cpy2val( refname, &v, valend ); - cpy2val( errval, &v, valend ); + if ( v < lastValid ) *v++ = '$'; + if ( v < lastValid ) *v++ = '('; + cpy2val( refname, &v, lastValid ); + cpy2val( errval, &v, lastValid ); cleanup: if (pop) { @@ -917,10 +919,10 @@ * Copy a string, honoring the 'end of destination string' pointer * Returns with **value pointing to the '\0' terminator */ -static void cpy2val(const char *src, char **value, char *valend) +static void cpy2val(const char *src, char **value, char *lastValid) { char *v = *value; - while ((v < valend) && (*v = *src++)) { v++; } + while ((v < lastValid) && (*v = *src++)) { v++; } *v = '\0'; *value = v; } diff -ur /tmp/e/base/src/libCom/macLib/macLib.h ./libCom/macLib/macLib.h --- /tmp/e/base/src/libCom/macLib/macLib.h 2010-03-31 10:13:49.000000000 -0700 +++ ./libCom/macLib/macLib.h 2010-03-31 10:24:33.000000000 -0700 @@ -65,7 +65,7 @@ int falseTrue /*0 means issue, 1 means suppress*/ ); -epicsShareFunc long /* #chars copied, <0 if any macros are */ +epicsShareFunc long /* #chars copied (not including trailing nul), <0 if any macros are */ /* undefined */ epicsShareAPI macExpandString( MAC_HANDLE *handle, /* opaque handle */ @@ -74,8 +74,7 @@ char *dest, /* destination string */ - long maxlen /* maximum number of characters to copy */ - /* to destination string */ + long capacity /* capacity of destination buffer (dest) */ ); @@ -97,8 +96,7 @@ char *value, /* string to receive macro value or name */ /* argument if macro is undefined */ - long maxlen /* maximum number of characters to copy */ - /* to value */ + long capacity /* capacity of destination buffer (value) */ ); epicsShareFunc long /* 0 = OK; <0 = ERROR */