Memory problems with g_list_append()



I have a situation where I build a list of
valid shells (from /etc/shells) with one
function and then compare a user's shell with
that list using another function to determine
if the user has a valid shell. However, I'm
running into a problem of corrupted data when
I try to actually check the shell.

In util.c here are my build and check functions:

-- BEGIN --
static GList *valid_shells = NULL;

/*
** build_valid_shells - Builds a list of all
** shells valid for users.
*/
void build_valid_shells()
{
   gchar *shell;
   GList *shells;

   setusershell();
   while((shell = getusershell()) != NULL)
      valid_shells = g_list_append(valid_shells, shell);
   endusershell();

   shells = valid_shells;
   while(shells)
   {
      printf("Valid shell = %s\n", (gchar *)shells->data);
      shells = g_list_next(shells);
   }
}

/*
** check_valid_shell - Returns 1 if the given
** shell is valid, 0 otherwise.
*/
gint check_valid_shell(gchar *shell)
{
   GList *shells = valid_shells;

   printf("shell = '%s'\n", shell);
   while(shells)
   {
      printf("shells->data = '%s'\n", (gchar *)shells->data);
      if(!strcmp((gchar *)shells->data, shell))
         return 1;
      shells = g_list_next(shells);
   }
   return 0;
}
-- END --

In main.c (within main()), I call the following:

DPRINT1("Building valid shells... ");
build_valid_shells();
DPRINT1("Done!\n");

In check.c, I use the following code to check the
shell:

struct passwd *pwd;

pwd = getpwnam(g_strstrip(user->username));
if(!pwd || !check_valid_shell(pwd->pw_shell))
{
   ...
}

The problem I run into is when I call check_valid_shell().
I get the following output when I call build_valid_shells():

Building valid shells... Valid shell = /bin/sh
Valid shell = /bin/csh
Valid shell = /bin/bash
Valid shell = /bin/tcsh
Valid shell = /usr/bin/sh
Valid shell = /usr/bin/csh
Valid shell = /usr/bin/bash
Valid shell = /usr/bin/tcsh
Valid shell = /usr/local/bin/tcsh
Valid shell = /usr/local/bin/bash
Valid shell = /usr/local/bin/zsh
Valid shell = /fs/global/acct/bin/not-auth
Valid shell = /usr/local/adm/bin/not-auth
Done!

However, when I go to check the valid shell,
I get the following output:

shell = '/usr/local/bin/zsh'
shells->data = ' out loanable 493984 to user with ID --------- (Borrower: Kayo Komatsu).'
shells->data = 'nable 493984 to user with ID --------- (Borrower: Kayo Komatsu).'
shells->data = '984 to user with ID --------- (Borrower: Kayo Komatsu).'
shells->data = 'r with ID --------- (Borrower: Kayo Komatsu).'
shells->data = '537415006 (Borrower: Kayo Komatsu).'
shells->data = 'orrower: Kayo Komatsu).'
shells->data = ' Komatsu).'
shells->data = '!'
shells->data = '2 10:45:44'
shells->data = ''
shells->data = 'ut loanable 980091 to user with ID --------- (Borrower: Desh Gupta).'
shells->data = 'to user with ID --------- (Borrower: Desh Gupta).'
shells->data = 'rrower: Desh Gupta).'

All of that data is stuff from other parts of the
program! I've used global GList's before with no problem
so I don't think it's that. I've actually experienced a
problem in the past using g_list_append() possibly causing
my list to become corrupted similarly, but it was with a C
compiler I wrote for a university course and it was on the
night it was due so I never bothered to mail the list. However,
this program is much smaller and I really must know why this
is happening.

Any help you can provide would be most appreciated.

Thanks,

Jeff Shipman           E-Mail: jeff nmt edu
Systems Programmer     Phone: (505) 835-5748
NMIMT Computer Center  http://www.nmt.edu/~jeff




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