tcptrace-bugs Static variable returns of HostName() are not consider by callers

From: Ilpo Järvinen (ilpo.jarvinen@helsinki.fi)
Date: 12/30/05


Date: Fri, 30 Dec 2005 17:24:12 +0200 (EET)
From: "Ilpo Järvinen" <ilpo.jarvinen@helsinki.fi>
Subject: tcptrace-bugs Static variable returns of HostName() are not consider by callers
Message-ID: <Pine.LNX.4.58.0512301657560.6307@kivilampi-30.cs.helsinki.fi>

Hi,

HostName() returns occassionally a pointer to the static variable
name_buf, and similarly, "gethostbyaddr() may
       return pointers to static data, which may be overwritten
       by later calls. Copying the struct hostent does not suf­
       fice, since it contains pointers - a deep copy is
       required." [from manpage]
Therefore it is invalid to use the return value more than once in a
printf, like in trace.c (I'm showing just this instance):

    if (debug > 3)
        printf("SameAddr(%s(%d),%s(%d)) returns %d\n",
               HostName(*paddr1), ADDR_VERSION(paddr1),
               HostName(*paddr2), ADDR_VERSION(paddr2),
               ret);

Relevant names.c part:

HostName(
    ipaddr ipaddress)
{
    tcelen len;
    static int cache = -1;
    struct hostent *phe;
    char *sb_host;
    static char name_buf[100];

[...snip...]
    if (calookup(cache,
                 (char *) &ipaddress, (tcelen) sizeof(ipaddress),
                 (char *) name_buf, &len) == OK) {
        if (debug > 2)
            fprintf(stderr,"Found host %s='%s' in cache\n",
                    adr, name_buf);
        return(name_buf);
    }

    if (ADDR_ISV6(&ipaddress))
        phe = gethostbyaddr ((char *)&ipaddress.un.ip6,
                             sizeof(ipaddress.un.ip6), AF_INET6);
    else
        phe = gethostbyaddr((char *)&ipaddress.un.ip4,
                            sizeof(ipaddress.un.ip4), AF_INET);
    if (phe != NULL) {
        sb_host = phe->h_name;
    } else {
        sb_host = adr;
    }

[...snip...]

    return(sb_host);
}

Also other statics, when returned, have similar hazards...

-- 
 i.


This archive was generated by hypermail 2.1.7 : 12/30/05 EST