bug-glibc
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

--static link and segmentation fault in gethostbyname - is this a bug?


From: Ian Collins
Subject: --static link and segmentation fault in gethostbyname - is this a bug?
Date: Wed, 21 May 2003 16:31:47 +1200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20030507

At my wits end. This only exhibits using glibc (on Linux). Works fine on HPUX, AIX, SCO, .... (although the other architectures are not using glibc).

Linux configurations....

1. Redhat Linux 7.1
uname -a ...
Linux boom.kiwiplan2.co.nz 2.4.7-10smp #1 SMP Thu Sep 6 17:09:31 EDT 2001 i686 unknown
gcc -v ...
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)

2. Redhat Linux 9
uname -a ...
Linux ianc.kiwiplan2.co.nz 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux
gcc -v ...
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

Problem: commented in code snippet.

/* ******* snip ****** */
/*
JUNK PROGRAM SHOWING SEGMENTATION VIOLATION *ONLY* WHEN APPLICATION IS STATICALLY LINKED.

  On GLIBC22 Linux, use: gcc -static -DGLIBC_22 -o a.out file.c
  On >GLIBC22 Linux, use: gcc -static -o a.out file.c

I have tried moving things around, but it needs to be in following order to fail,
  i.e.,
  putenv
  getpwuid
  putenv
  putenv
  putenv
  gethostbyname (it gets a segmentation violation in here).

Workaround: If I put the host I am looking up in /etc/hosts, it seems to work ok (???).

 */
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

main(int argc, char **argv)
{
  char *s;
  int i;
  struct passwd *p;

  uid_t pp;
  struct hostent *hp;
  struct in_addr inaddr;
  char addr[40];

  s = (char *) malloc(15);
  strcpy(s, "VAR1=123456789");
  (void) putenv(s);

  p = (struct passwd*) getpwuid((uid_t) 1);

  s = (char *) malloc(15);
  strcpy(s, "VAR2=123456789");
  (void) putenv(s);
  s = (char *) malloc(15);
  strcpy(s, "VAR3=123456789");
  (void) putenv(s);
  s = (char *) malloc(15);
  strcpy(s, "VAR4=123456789");
  (void) putenv(s);

  strcpy(addr, "damn");

  if ((hp= gethostbyname((const char*)addr)) == 0) {
    printf("gethostbyname to %s failed with status %d\n",addr, errno);
  }
  else{
    printf("gethostbyname to %s ok\n", addr);
  }

  exit(0);
}

#ifdef GLIBC_22

/*
  NOTE: THIS IS JUNK CODE. IT IS NOT CALLED ANYWHERE ABOVE.
  ON GLIBC_22 PLATFORMS, IF IT IS NOT IN THE FILE, THEN THE SEGMENTATION
  VIOLATION GOES AWAY. WIERD.

  ON >GLIBC_22 THIS CODE ISN'T NEEDED TO SEE THE PROBLEM.
 */
int kwputenv(invar, invalue)
     char *invar;
     char *invalue;
{
  char *var;
  char *value;
  int lvar;

  char *thevar;
  int lvalue;
  int l;
  int iret;

  lvar = strlen(invar);
  lvalue = strlen(invalue);

  var = (char *) malloc(lvar+1);
  memcpy(var, invar, lvar);
  var[lvar] = '\0';

  iret = 0;
  value = (char *) malloc(lvalue+1);
  memcpy(value, invalue, lvalue);
  value[lvalue] = '\0';

  l = strlen(var) + strlen(value) + 2;
  if ((thevar = (char *) malloc(l)) != (char *) NULL) {
    sprintf(thevar, "%s=%s", var, value);
    iret = putenv(thevar);
    free(value);
  }
  free(var);
  return iret;
}

int skwputenv(invar, invalue, inover, lvar, inlvalue)
char *invar; char *invalue; int *inover; int lvar; int inlvalue;
{
  // char *var;
  char var[256];

  //  char *value;
  char value[256];

  char *thevar;
  int lvalue;
  int l;
  int iret;

  //  var = (char *) malloc(lvar+1);
  memcpy(var, invar, lvar);
  var[lvar] = '\0';

  lvalue = inlvalue;

  iret = 0;
  //  value = (char *) malloc(lvalue+1);
  memcpy(value, invalue, lvalue);
  value[lvalue] = '\0';

  l = strlen(var) + strlen(value) + 2;
  if ((thevar = (char *) malloc(l)) != (char *) NULL) {
    sprintf(thevar, "%s=%s", var, value);
    iret = putenv(thevar);
    //    free(value);
  }
  //  free(var);
  return iret;
}

int get_hostip()
{
  int hostNameSize;
  int hostipSize;
  struct hostent *hp;
  struct in_addr inaddr;
  char addr[40];

  strcpy(addr, "logif");

  if ((hp= gethostbyname((const char*)addr)) == 0) {
    printf("gethostbyname to %s failed with status %d\n",addr, errno);
  }
  else{
    printf("gethostbyname to %s ok\n", addr);
  }

  return 0;
}




void tmy_kiwi_vars()
{
  extern int tmy_kwputenv();

  (void) tmy_kwputenv("TERMINAL", "vt100");
  (void) tmy_kwputenv("PRINTER", "1");
  (void) tmy_kwputenv("EVALUATOR", "1");
  //  get_hostip();
  return;
}

int tmy_kwputenv(var, value)
     char *var;
     char *value;
{
  int l;
  int iret;
  char *thevar;

  iret = 0;

  l = strlen(var) + strlen(value) + 2;
  if ((thevar = (char *) malloc(l)) != (char *) NULL) {
    sprintf(thevar, "%s=%s", var, value);
    iret = putenv(thevar);
  }
  return iret;
}

#endif



/* ******* snip ****** */

--

Many Regards,
Ian Collins
System Manager
KIWIPLAN NZ Ltd.
================

The church saves sinners, but science seeks to stop their manufacture.
                -- Elbert Hubbard





reply via email to

[Prev in Thread] Current Thread [Next in Thread]