Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 20 Feb 2001 18:14:42 +0300
From:      "Andrey Simonenko" <simon@comsys.ntu-kpi.kiev.ua>
To:        freebsd-hackers@freebsd.org
Subject:   Re: Staticaly allocated buffers in library. Is it correct?
Message-ID:  <96u5ao$70r$1@igloo.uran.net.ua>
References:  <Pine.BSF.4.21.0102171202110.400-100000@scorpion.cosmos.all.net> <200102192046.f1JKkl738082@earth.backplane.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Let's look at implementation of getaddrinfo(3) function (there are some
functions more which
do the same way). We can find source for this function in
/usr/src/lib/libc/net/getaddrinfo.c file.

This functions in some case reads /etc/hosts file and try to find out there
host name. getaddrinfo(3)
calls some functions and function _gethtent() tries to read line by linefrom
/etc/hosts file:

static struct addrinfo *
_gethtent(hostf, name, pai)
        FILE *hostf;
        const char *name;
        const struct addrinfo *pai;
{
        char *p;
        char *cp, *tname, *cname;
        struct addrinfo hints, *res0, *res;
        int error;
        const char *addr;
        char hostbuf[8*1024];

 again:
        if (!(p = fgets(hostbuf, sizeof hostbuf, hostf)))
                return (NULL);
        if (*p == '#')
                goto again;
        if (!(cp = strpbrk(p, "#\n")))
                goto again;
        *cp = '\0';
        if (!(cp = strpbrk(p, " \t")))
                goto again;
        *cp++ = '\0';

We can see if line is bigger than 8k, then _gethtent() reads until the end
of line.
In most case this function doesn't find needed host name in such lines, but
in some case it can find part of
line as correct host name and tries to fetch IP address, but it also will
not work, because we lose
beginning of line when "goto again".

This code can be simply rewriten as loop with fgets(), strlen()/strchr() and
realloc(), but it causes
speed lost in this function.

Also I understand that 8k for line in /etc/hosts is enough and should not be
problem for most of _real life_
situations.


Matt Dillon <dillon@earth.backplane.com> wrote in message
news:200102192046.f1JKkl738082@earth.backplane.com...
> :>     fgets() with the proper length limitation, using a statically
allocated
> :>     buffer is not a big deal.  Most configuration files couldn't have
long
> :>     lines and still be legal anyway.
> :
> :Note that the classical loop
> :    while (fgets(buf, n, fp) != NULL) {
> :         tokenize(buf, args...);
> :         ...
> :   }
> :may have problems if the line is too long, so one needs to detect it by
> :looking for the '\n'. if none is found, then one can either abort on
error
> :or ignore the line. In the latter case, you need to read the remaining
chars
> :so that the next fgets won't get them.
> :
> :regards,
> :mouss
>
>     Yes, but we are talking about simple stupid config files here.
Programs
>     which actually tokenize an input stream typically do not use fgets().
>     Tokenizers either use [f]lex, [f]getc(), read() (and handle the
buffering
>     themselves), or mmap().
>



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?96u5ao$70r$1>