[rrd-developers] Re: printf format patch

Alex van den Bogaerdt alex at slot.hollandcasino.nl
Mon Apr 29 00:58:11 MEST 2002


Tobias Oetiker wrote:

> > Woops some bitrot occured between then and now, those else's should be
> > moved up a couple lines under the *ptr == '%' not the == 'l'.
> >
> 
> ok, we got
> 
>                         }
>                         if (*ptr == '\0') return 1;
>                         else if (*ptr == ' ') ptr++;
>                         else if (*ptr == '-') ptr++;
>                         else if (*ptr == '+') ptr++;
>                         if (*ptr == 'l') {
>                                 ptr++;
>                                 n++;
>                                 if (*ptr == '\0') return 1;
> 
> now>

This isn't the correct place I think.  Here's the larger part of the
function, with my comments at position 1:

        while (*ptr != '\0') {
                if (*ptr == '%') {ptr++;
                        if (*ptr == '\0') return 1;
This is the position where
the 3 lines should go IMHO.
                        while ((*ptr >= '0' && *ptr <= '9') || *ptr == '.') {
                                ptr++;
                        }
                        if (*ptr == '\0') return 1;
                        else if (*ptr == ' ') ptr++;
                        else if (*ptr == '-') ptr++;
                        else if (*ptr == '+') ptr++;
This would allow for formats
such as "%6.2+lf" which isn't
valid
                        if (*ptr == 'l') {
                                ptr++;
                                n++;
                                if (*ptr == '\0') return 1;
                                if (*ptr == 'e' || *ptr == 'f') {
                                        ptr++;
                                        } else { return 1; }
                        }
                        else if (*ptr == 's' || *ptr == 'S' || *ptr == '%') { ++ptr; }
This line also seems to be
wrong.  "%6.2s" isn't valid,
nor is "%6.2%"  I think this
check should be moved up so
that "%s", "%S" and "%%" is
checked and nothing more.
                        else { return 1; }
                } else {
                        ++ptr;
                }
        }


So, it should probably become:
(warning, I didn't even check if it compiles)

    while (*ptr != '\0')
        if (*ptr++ == '%') {

            /* line cannot end with percent char */
            if (*ptr == '\0') return 1;

            /* '%s', '%S' and '%%' are allowed */
            if (*ptr == 's' || *ptr == 'S' || *ptr == '%') ptr++;

            /* or else '% 6.2lf' and such are allowed */
            else {

                /* optional padding character */
                if (*ptr == ' ' || *ptr == '+' || *ptr == '-') ptr++;

                /* This should take care of 'm.n' with all three optional */
                while (*ptr >= '0' && *ptr <= '9') ptr++;
		if (*ptr == '.') ptr++;
                while (*ptr >= '0' && *ptr <= '9') ptr++;

                /* Either 'le' or 'lf' must follow here */
                if (*ptr++ != 'l') return 1;
                if (*ptr == 'e' || *ptr == 'f') ptr++;
                else return 1;
                n++;
            }
        };

cheers
-- 
   __________________________________________________________________
 / alex at slot.hollandcasino.nl                  alex at ergens.op.het.net \
| work                                                         private |
| My employer is capable of speaking therefore I speak only for myself |
+----------------------------------------------------------------------+
| Technical questions sent directly to me will be nuked. Use the list. | 
+----------------------------------------------------------------------+
| http://faq.mrtg.org/                                                 |
| http://rrdtool.eu.org  --> tutorial                                  |
+----------------------------------------------------------------------+

--
Unsubscribe mailto:rrd-developers-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:rrd-developers-request at list.ee.ethz.ch?subject=help
Archive     http://www.ee.ethz.ch/~slist/rrd-developers
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the rrd-developers mailing list