gpg_err_code_from_errno usage
Werner Koch
wk at gnupg.org
Sat Mar 11 15:49:02 CET 2006
On Sat, 11 Mar 2006 11:52:26 +0100, Nikos Mavrogiannopoulos said:
> On Sat 11 Mar 2006 10:45, Marcus Brinkmann wrote:
>> > But how can you be sure that calloc() (or in that case gcry_calloc)
>> > will set the errno? calloc is a library call which may or may not
>> > use a system call. Even if it works on some systems, this is a very
>> > non-portable construction, which will result in no error checking
>> > in systems that do not set errno.
>> It's not in ISO C, but in POSIX. Single Unix v3 has:
>> http://www.opengroup.org/onlinepubs/009695399/functions/calloc.html
> Maybe but this doesn't change my argument. It is less portable than
> the ISO/ANSI C behavior. Given that libgcrypt is already ported to
> non-UNIX systems as well[0], I would consider this a bug.
To protect against ill behaving implementaions we even use some
failsafe code in the internal malloc code:
gcry_err_code_t
gcry_malloc (size_t n, unsigned int flags, void **mem)
{
[...]
/* Call standard malloc or user registered function. */
[...]
if (!m)
{
/* Make sure that ERRNO has been set in case a user supplied
memory handler didn't it correctly. */
if (!errno)
errno = ENOMEM;
err = gpg_err_code_from_errno (errno);
}
else
*mem = m;
return err;
}
Shalom-Salam,
Werner
More information about the Gcrypt-devel
mailing list