[PATCH xrandr v4 2/5] xrandr: stricter --scale argument parsing

Keith Packard keithp at keithp.com
Tue Feb 6 00:01:57 UTC 2018

Giuseppe Bilotta <giuseppe.bilotta at gmail.com> writes:

> I'm not a big fan of strtod because with it it's impossible to know if
> a conversion actually happened. xrandr --scale '  ' would actually be
> accepted (resulting in a scale value of 0), while the scanf catches
> it.

strtod takes an 'endptr' argument which can be used for precisely this
purpose, but I think your use of sscanf is easier to read as it does
both conversions in one call, and lets you pick the two different
versions easily (--scale 2 and --scale 2x1.5). One could imagine doing

        xscale = strtod(string, &endptr);
        if (*endptr) {
                if (endptr == string || *endptr != 'x')
                        syntax error;
                string = endptr + 1;
                yscale = strtod(string, &endptr);
                if (endptr == string || *endptr)
                        syntax error;
        } else {
                yscale = xscale;

That's a lot more code than two calls to sscanf...

> Of course there's also to be said that we could reject a scale factor
> of 0, regardless of whether it comes from a correct parsing of the
> string '0.0' or from the parse of an empty string (but of course then
> we couldn't customize the error message to differentiate between
> “incorrect parse” and “value out of range”).

Probably a good thing to catch.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <https://lists.x.org/archives/xorg-devel/attachments/20180205/e5ac5783/attachment.sig>

More information about the xorg-devel mailing list