malloc: errno: 22: Invalid argument

Sami Kerola kerolasa at iki.fi
Sat Apr 9 13:44:34 UTC 2011


Hello FreeBSD answers,

I am a maintainer of a dhcp analysis software, which is before exit
checking errno. I got report from FreeBSD user, Roar who is CC:d, that
my software is reporting invalid argument at that section of the
program. After sending debugging messages back and forth issue was
pinpointed to following code sample.

-- snip
#include <err.h>
#include <errno.h>
#include <stdlib.h>

int main(void)
{
        int *i;
        warn("errno: %d", errno);
        i = malloc(sizeof(int));
        warn("errno: %d", errno);
        free(i);
        return (errno);
}
-- snip

which will give following output:

# ./a.out
a.out: errno: 0: Unknown error: 0
a.out: errno: 22: Invalid argument

Characteristics of the system are;

FreeBSD 8.2-STABLE

# gcc -v
Using built-in specs.
Target: i386-undermydesk-freebsd
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 4.2.1 20070719  [FreeBSD]

/lib/libc.so.7

There is no malloc.conf file on system.

I don't understand why the invalid argument is set. I do not feel
comfortable to write 'if the system is FreeBSD do not care malloc
related errno 22' to my code. That simply sounds wrong. Does someone
have explanation what might be going on and/or advice how I should
deal this thing in the code? Or is the whole thing some how
environment related?

Unfortunately I don't have FreeBSD installation. So even if you would
not be a malloc specialist you can say reply and tell that the sample
is or is not setting invalid argument. That will be evidence that the
problem happens either on none, some or all other systems as well.

Thanks in advance for help.

-- 
   Sami Kerola
   http://www.iki.fi/kerolasa/


More information about the freebsd-questions mailing list