[RFC weston] Add strtoint() helper

Bryce Harrington bryce at osg.samsung.com
Fri Jul 15 19:24:46 UTC 2016


On Thu, Jul 14, 2016 at 05:35:55PM -0700, Thiago Macieira wrote:
> On quinta-feira, 14 de julho de 2016 13:03:34 PDT Bryce Harrington wrote:
> > +       ret = strtol(str, &end, 10);
> 
> Here you made a cast from long to int, which may be a loss of data.
> 
> 	ret = strtol("4294967296", &end, 10);
> 
> Will produce no errno since it's in the valid range of long on LP64 systems, 
> and yet ret = 0.
> 
> You should define ret to be a long and then later do:
> 
> 	*value = (int)ret;
> 	if ((long)*value != ret) {
> 		errno = ERANGE;
> 		return false;
> 	}
> 
> On systems where long and int are the same (ILP32, LLP64), the conditional is 
> optimised out of existence by the compiler.
> 
> The casts are really optional, but since this in a header, some people may 
> want to try and use it with annoying compiler options like -Wconversion.

Okay, thanks.

Bryce

> -- 
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
>    Software Architect - Intel Open Source Technology Center
> 
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/wayland-devel


More information about the wayland-devel mailing list