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



On Dec 28,  2:44am, n54%gmx.com@localhost ("Kamil Rytarowski") wrote:
-- Subject: Re: Reuse strtonum(3) and reallocarray(3) from OpenBSD

| 1)
| Is errno usage allowed only for libc functions? If not then why not
| drop the last parameter and replace it with errno?
| 
| It's a responsibility of a developer to save or handle errno.

Then you have strtol() with min/max and read the man page about the
messiness of handling errno.

| 2)
| The proposed strtoi(3) returns information of partial conversion
| twice: in endptr and in rerror (kind of imitation of errno on demand).
| 
| It's duplication of information.

Not necessarily, since *eptr can be NULL or *rerror can be NULL.

| 3)
| Partial conversion returns error (copied from errno(2)):
| 
|      86 ENOTSUP Not supported. An attempt was made to set or change a
|              parameter to an unsupported value.
| 
| Does it match well? Maybe 22 EINVAL describes the things better:
| 
|      22 EINVAL Invalid argument. Some invalid argument was supplied.  (For
|              example, specifying an undefined signal to a signal(3) or kill(2)
|              function).

Then you have 3 reasons for returning EINVAL
	1 wrong base
	2 wrong range
	3 partial conversion

| 4)
| The original intention of strtonum(3) and probably majority cases when it will be used is when
| a developer wants to just translate only one number from a string to a single integer. In case
| of partial conversion handle the overall operation as failed.
| 
| 5) In this API there are many input parameters... 4 of strtonum(3) vs 6 of strtoi(3).
| 
| To summarize I don't think that people wanting to just have the job done will like strtoi(3) better than strto(3)...
| 
| My counter proposition is to use errno "as God intended" and split strtoi(3) to two functions (kind of low and high):
| - strtoi(3) with partial conversion handling
| - fullstrtoi(3) without partial conversion handling, just try to translate the whole string and shut up

God did not intend to use errno to detect between INTMAX_MAX as overflow and
INTMAX_MAX as a valid return. This is what makes error handling in strtol()
complicated, and this is why you have to set errno to 0 before calling
the function which is highly unusual. You don't set errno to 0 before
calling each function do you?

| This split makes the code more clean, see how these functions are now specialized at doing one thing well.
| Also fullstrtoi(3), being the direct equivalent to strtonum(3), takes the same number of parameters (4 vs 4).
| Our fullstrtoi will be better than strtonum(3) having dynamic base support, and so it will be a replacement of strtonum() from ZFS (it's changing hex string to integers).
| 
| Here is the proposed API with an embedded example (compile and test as home-work).
| 

Suffers from the original strtol() problem, if lo == INTMAX_MIN or
hi == INTMAX_MAX, so we've come full cycle.
christos


Home | Main Index | Thread Index | Old Index