Re: [patch] atoi.3: Document return value on under/overflow as undefined

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

 



On Sun, 10 Dec 2023 20:35:15 +0000, Alejandro Colomar <alx@xxxxxxxxxx> said:
> For extra fun, you could have quoted this together :)
>
> ```
>      except that atoi() does not detect errors.
> ```

Yeah, which of course makes no sense no matter if over/underflow is
supposed to be considered an "error".

> However, maybe we should instead try to fix glibc to do the right thing.
>
> 	int
> 	atoi(const char *nptr)
> 	{
> 		int   i, err;
>
> 		i = strtoi(nptr, NULL, 10, INT_MIN, INT_MAX, &err);
> 		if (err)
> 			errno = err;
> 		return i;
> 	}
>
> This is compatible with ISO C, since it behaves like
>
> 	(int) strtol(nptr, NULL, 10);
>
> "Except for the behavior on error", in which this atoi(3) implementation
> sets errno, but nothing forbids that (ISO C only says "need not affect
> the value of the integer expression errno on an error", which allows
> affecting errno).  POSIX also allows this implementation: "except that
> the handling of errors may differ".

If we don't change the manpage, then it should return 0 on error, not
the clamped value. Unless you mean that the manpage should be changed
to say it'll return the clamped value?

Portable code won't be able to rely on errno anyway, so might as well
not set it, in my opinion.

But at least this implementation won't trigger UB for any input.

--
typedef struct me_s {
  char name[]      = { "Thomas Habets" };
  char email[]     = { "thomas@xxxxxxxxx" };
  char kernel[]    = { "Linux" };
  char *pgpKey[]   = { "http://www.habets.pp.se/pubkey.txt"; };
  char pgp[] = { "9907 8698 8A24 F52F 1C2E  87F6 39A4 9EEA 460A 0169" };
  char coolcmd[]   = { "echo '. ./_&. ./_'>_;. ./_" };
} me_t;




[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux