tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Reuse strtonum(3) and reallocarray(3) from OpenBSD



Christos Zoulas wrote:
> Just add:
> 
> 	int fd = open("/does/not/exist", O_RDONLY);
> 
> before your newfangled fullstrtou().

Please see below.

> Also you are using errx()
> instead of err() that hides the message from errno.
> 
Right!

> christos
> 

Hello,

I took some days to take a closer look at use-cases of proposed strtoi(3) and fullstrto(3) [1] and I found a hole in my design.. that will make these functions almost useless outside user-level UNIX.

The hole was pointed by Christos, however it wasn't pointed that there is additional and fatal design problem.

Well to make it short, my initial proposition depends entirely on errno.. however errno is not available in all environments [2] and so without errno strtol(3)-like functions provide possibility to check minimally partial check, when fullstrtoi(3) doesn't.

This shortage would limit the functions to use only in a full-features UNIX environment (a set-up with errno).

So to face the problems my proposition is to change the strtoi(3) and fullstrtoi(3) functions:
- add an 'int *error' as the last parameter (used on demand if not NULL)
- to deduplicate streams to pass information, errno is not affecting the initial value neither affected after finish

intmax_t strtoi(const char * __restrict ptr, char ** __restrict endptr, int base, intmax_t lo, intmax_t hi, int *error)
intmax_t strtou(const char * __restrict ptr, char ** __restrict endptr, int base, uintmax_t lo, uintmax_t hi, int *error)

intmax_t fullstrtoi(const char * ptr, int base, intmax_t lo, intmax_t hi, int *error)
intmax_t fullstrtou(const char * ptr, int base, uintmax_t lo, uintmax_t hi, int *error)

I propose to define these functions in src/common, making them available in src/lib/libc and src/sys (so in the kernel).
The proposed place to put the functions is: <intmax.h>

One shortcoming of the functions is that they won't be able to check errors not handled by underlying strtol(9)-like implementation -- value out of type range... perhaps reimplement strtoimax(3) in strtoi(3) to handle this case?

Christos' analysis was right. To use C outside libc better to keep errno for pointing out not whether there was an error but to pass additional details.

I'm looking forward for your feedback.

Regards,

[1] If I'm not mistaken last version on mailing-lists was here: http://mail-index.netbsd.org/tech-userlevel/2014/12/28/msg008869.html
[2] http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/stdlib/_strtol.h?rev=1.7&content-type=text/x-cvsweb-markup


Home | Main Index | Thread Index | Old Index