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

Keith Packard keithp at keithp.com
Tue Feb 6 02:43:37 UTC 2018


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

> Not in the sense I mean above. If the string is a sequences of
> whitespace characters, strtod would return 0 as value (no conversion),
> and set endptr to the end of the string, because it would gobble the
> whitespace. This would be indistinguishable from it successfully
> parsing a string argument of, say, '0.0'.

No, strtod requires a non-empty sequence of digits.

        #include <stdio.h>
        #include <stdlib.h>

        int main(int argc, char **argv)
        {
                char	*blanks = "    ";
                char	*endptr;
                double	x;

                x = strtod(blanks, &endptr);
                printf("x: %f endptr: \"%s\"\n", x, endptr);
        }

        $ ./a.out
        x: 0.000000 endptr: "    "

strtod is actually not a terrible function (surprising for libc, I know)

> Hm. Since we have to do it both for scale and gamma, I wonder if it's
> overengineering if I try to refactor this parsing of "N or 1 positive
> values with separator S" into its own function.

Yes. I'd just use sscanf or strtod directly as you please.

-- 
-keith
-------------- 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/ce52850b/attachment.sig>


More information about the xorg-devel mailing list