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



Jean-Yves Migeon wrote
> Le 25/11/2014 14:25, Jean-Yves Migeon a écrit :
> [snip]
> > I do not know about libressl, but last time I read about it a
> > compatibility library was discussed. Until now libressl remains
> > "designed by OpenBSD for OpenBSD".
> 
> As someone showed it to me privately, there _is_ a portable version of 
> libressl you can get from libressl.org directly.
> 
> So just ignore that previous mail.
> 
> Thanks,
> 

Hello,

Thanks for your remarks! OpenSSH, LibreSSL, mandoc are distributed with compat libraries, the majority of the code-base is not.

I saw that GCC 5.0 scheduled built-ins for overflow control:

<quote>
A new set of built-in functions for arithmetics with overflow checking has been added: __builtin_add_overflow, __builtin_sub_overflow and __builtin_mul_overflow and for compatibility with clang also other variants. These builtins have two integral arguments (which don't need to have the same type), the arguments are extended to infinite precision signed type, +, - or * is performed on those, and the result is stored in an integer variable pointed to by the last argument. If the stored value is equal to the infinite precision result, the built-in functions return false, otherwise true. The type of the integer variable that will hold the result can be different from the types of the first two arguments. The following snippet demonstrates how this can be used in computing the size for the calloc function:

    void *
    calloc (size_t x, size_t y)
    {
      size_t sz;
      if (__builtin_mul_overflow (x, y, &sz))
        return NULL;
      void *ret = malloc (sz);
      if (ret) memset (res, 0, sz);
      return ret;
    }

On e.g. i?86 or x86-64 the above will result in a mul instruction followed by a jump on overflow.
</quote>
https://gcc.gnu.org/gcc-5/changes.html

I will wrap around a modernized version of reallocarray(3), with support for the built-ins and show it bit later.

BTW. And objections to strtonum(3)?


Home | Main Index | Thread Index | Old Index