EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

2002  2003  2004  2005  2006  2007  2008  2009  <20102011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024  Index 2002  2003  2004  2005  2006  2007  2008  2009  <20102011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024 
<== Date ==> <== Thread ==>

Subject: Re: Bug in macLib
From: Eric Norum <[email protected]>
To: "Mark Rivers" <[email protected]>
Cc: Core-Talk <[email protected]>
Date: Wed, 31 Mar 2010 06:58:28 -0700
On Mar 31, 2010, at 6:43 AM, Mark Rivers wrote:

> Hi Eric,
> 
> I may be missing something, but this code actually looks OK to me.  Note
> that valend is a pointer, not a counter.  So if valend actually points
> to the last character in the buffer then the statement
> 
>            if ( v <= valend ) *v   = '\0';
> 
> should be OK, right?

Nope.
Despite its name, "valend" actually points to the location just past the end of the buffer.

To see this, have a look at one of the calls to trans, namely:
epicsShareAPI macExpandString(
    MAC_HANDLE  *handle,        /* opaque handle */
    const char  *src,           /* source string */
    char        *dest,          /* destination string */
    long        maxlen )        /* maximum number of characters to copy */
                                /* to destination string */
{
 ..........
    trans( handle, &entry, 0, "", &s, &d, d + maxlen );

I think the change that I proposed takes care of things, but maybe there needs to be a more thorough cleanup of all the code in this file so that the variable names more accurately reflect their function.

> 
> But it's certainly possible that valend has not been set correctly, and
> that it is pointing to one character past the end of the buffer.  I have
> not looked into that.
> 
> Mark
> 
> -----Original Message-----
> From: Eric Norum [mailto:[email protected]] 
> Sent: Tuesday, March 30, 2010 6:46 PM
> To: Andrew Johnson
> Cc: Mark Rivers; Core-Talk
> Subject: Re: Bug in macLib
> 
> Hmm.....
> Check the last 'if' statement in mcaCore.c: trans....
> 
> static void trans( MAC_HANDLE *handle, MAC_ENTRY *entry, int level,
>                   const char *term, const char **rawval, char **value,
>                   char *valend )
> {
>    char quote;
>    const char *r;
>    char *v;
>    int discard;
>    int macRef;
> 
>    /* return immediately if raw value is NULL */
>    if ( *rawval == NULL ) return;
> 
>    /* discard quotes and escapes if level is > 0 (i.e. if these aren't
>       the user's quotes and escapes) */
>    discard = ( level > 0 );
> 
>    /* 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 );
> 
>    /* initially not in quotes */
>    quote = 0;
> 
>    /* scan characters until hit terminator or end of string */
>    for ( r = *rawval, v = *value; strchr( term, *r ) == NULL; r++ ) {
> 
>        /* handle quoted characters (quotes are discarded if in name) */
>        if ( quote ) {
>            if ( *r == quote ) {
>                quote = 0;
>                if ( discard ) continue;
>            }
>        }
>        else if ( *r == '"' || *r == '\'' ) {
>            quote = *r;
>            if ( discard ) continue;
>        }
> 
>        /* macro reference if '$' followed by '(' or '{' */
>        macRef = ( *r == '$' &&
>                   *( r + 1 ) != '\0' &&
>                   strchr( "({", *( r + 1 ) ) != NULL );
> 
>        /* macros are not expanded in single quotes */
>        if ( macRef && quote != '\'' ) {
>            /* Handle macro reference */
>            refer ( handle, entry, level, &r, &v, valend );
>        }
> 
>        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;
>            }
> 
>            /* copy character to output */
>            else {
>                if ( v < valend ) *v++ = *r;
>            }
> 
>            /* ensure string remains properly terminated */
>            if ( v <= valend ) *v   = '\0';
>        }
>    }
> 
> 
> -- 
> Eric Norum
> [email protected]
> 
> 
> 
> 
> 

-- 
Eric Norum
[email protected]






Replies:
Re: Bug in macLib Andrew Johnson
References:
Bug in macLib Mark Rivers
Re: Bug in macLib Andrew Johnson
Re: Bug in macLib Eric Norum
RE: Bug in macLib Mark Rivers

Navigate by Date:
Prev: RE: Bug in macLib Mark Rivers
Next: Re: Bug in macLib Eric Norum
Index: 2002  2003  2004  2005  2006  2007  2008  2009  <20102011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024 
Navigate by Thread:
Prev: RE: Bug in macLib Mark Rivers
Next: Re: Bug in macLib Andrew Johnson
Index: 2002  2003  2004  2005  2006  2007  2008  2009  <20102011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  2023  2024 
ANJ, 02 Feb 2012 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·