Don't free uninitialized hash_table

* src/dir.c (clear_directory_contents): We use ht_vec to mark when
we have initialized the hash_table: don't free if it's NULL.
(everywhere): Use NULL instead of 0 when working with pointers.
This commit is contained in:
Paul Smith 2023-01-01 09:44:59 -05:00
parent c4fbfe8b5e
commit ccbaf3861d

View File

@ -76,12 +76,12 @@ dosify (const char *filename)
char *df;
int i;
if (filename == 0 || _USE_LFN)
if (filename == NULL || _USE_LFN)
return filename;
/* FIXME: what about filenames which violate
8+3 constraints, like "config.h.in", or ".emacs"? */
if (strpbrk (filename, "\"*+,;<=>?[\\]|") != 0)
if (strpbrk (filename, "\"*+,;<=>?[\\]|") != NULL)
return filename;
df = dos_filename;
@ -105,7 +105,7 @@ dosify (const char *filename)
++filename;
if (*filename == '.')
return filename;
*df = 0;
*df = '\0';
return dos_filename;
}
#endif /* __MSDOS__ */
@ -125,8 +125,8 @@ downcase (const char *filename)
static PATH_VAR (new_filename);
char *df;
if (filename == 0)
return 0;
if (filename == NULL)
return NULL;
df = new_filename;
while (*filename != '\0')
@ -135,7 +135,7 @@ downcase (const char *filename)
++filename;
}
*df = 0;
*df = '\0';
return new_filename;
}
@ -203,14 +203,14 @@ vmsstat_dir (const char *name, struct stat *st)
DIR *dir;
dir = opendir (name);
if (dir == 0)
if (dir == NULL)
return -1;
closedir (dir);
s = strchr (name, ':'); /* find device */
if (s)
{
/* to keep the compiler happy we said "const char *name", now we cheat */
*s++ = 0;
*s++ = '\0';
st->st_dev = (char *)vms_hash (name);
h = vms_hash (s);
*(s-1) = ':';
@ -281,9 +281,10 @@ clear_directory_contents (struct directory_contents *dc)
{
--open_directories;
closedir (dc->dirstream);
dc->dirstream = 0;
dc->dirstream = NULL;
}
hash_free (&dc->dirfiles, 1);
if (dc->dirfiles.ht_vec != NULL)
hash_free (&dc->dirfiles, 1);
return NULL;
}
@ -610,10 +611,9 @@ find_directory (const char *name)
dc->counter = command_count;
ENULLLOOP (dc->dirstream, opendir (name));
if (dc->dirstream == 0)
/* Couldn't open the directory. Mark this by setting the
'files' member to a nil pointer. */
dc->dirfiles.ht_vec = 0;
if (dc->dirstream == NULL)
/* Couldn't open the directory: mark this by setting files to NULL. */
dc->dirfiles.ht_vec = NULL;
else
{
hash_init (&dc->dirfiles, DIRFILE_BUCKETS,
@ -623,7 +623,7 @@ find_directory (const char *name)
if (open_directories == MAX_OPEN_DIRECTORIES)
/* We have too many directories open already.
Read the entire directory and then close it. */
dir_contents_file_exists_p (dir, 0);
dir_contents_file_exists_p (dir, NULL);
}
}
@ -645,7 +645,7 @@ dir_contents_file_exists_p (struct directory *dir,
int rehash = 0;
#endif
if (dc == 0 || dc->dirfiles.ht_vec == 0)
if (dc == NULL || dc->dirfiles.ht_vec == NULL)
/* The directory could not be stat'd or opened. */
return 0;
@ -658,10 +658,10 @@ dir_contents_file_exists_p (struct directory *dir,
#endif
#ifdef __EMX__
if (filename != 0)
if (filename != NULL)
_fnlwr (filename); /* lower case for FAT drives */
#endif
if (filename != 0)
if (filename != NULL)
{
struct dirfile dirfile_key;
@ -680,7 +680,7 @@ dir_contents_file_exists_p (struct directory *dir,
/* The file was not found in the hashed list.
Try to read the directory further. */
if (dc->dirstream == 0)
if (dc->dirstream == NULL)
{
#ifdef WINDOWS32
/*
@ -692,7 +692,7 @@ dir_contents_file_exists_p (struct directory *dir,
{
if ((dc->fs_flags & FS_FAT) != 0)
{
dc->mtime = time ((time_t *) 0);
dc->mtime = time (NULL);
rehash = 1;
}
else if (stat (dc->path_key, &st) == 0 && st.st_mtime > dc->mtime)
@ -725,7 +725,7 @@ dir_contents_file_exists_p (struct directory *dir,
struct dirfile **dirfile_slot;
ENULLLOOP (d, readdir (dc->dirstream));
if (d == 0)
if (d == NULL)
{
if (errno)
OSS (fatal, NILF, "readdir %s: %s", dir->name, strerror (errno));
@ -772,7 +772,7 @@ dir_contents_file_exists_p (struct directory *dir,
hash_insert_at (&dc->dirfiles, df, dirfile_slot);
}
/* Check if the name matches the one we're searching for. */
if (filename != 0 && patheq (d->d_name, filename))
if (filename != NULL && patheq (d->d_name, filename))
return 1;
}
@ -819,17 +819,17 @@ file_exists_p (const char *name)
dirend = strrchr (name, '/');
#ifdef VMS
if (dirend == 0)
if (dirend == NULL)
{
dirend = strrchr (name, ']');
dirend == NULL ? dirend : dirend++;
}
if (dirend == 0)
if (dirend == NULL)
{
dirend = strrchr (name, '>');
dirend == NULL ? dirend : dirend++;
}
if (dirend == 0)
if (dirend == NULL)
{
dirend = strrchr (name, ':');
dirend == NULL ? dirend : dirend++;
@ -846,7 +846,7 @@ file_exists_p (const char *name)
dirend = name + 1;
}
#endif /* HAVE_DOS_PATHS */
if (dirend == 0)
if (dirend == NULL)
#ifndef _AMIGA
return dir_file_exists_p (".", name);
#else /* !AMIGA */
@ -920,7 +920,7 @@ file_impossible (const char *filename)
dirend = p + 1;
}
#endif /* HAVE_DOS_PATHS */
if (dirend == 0)
if (dirend == NULL)
#ifdef _AMIGA
dir = find_directory ("");
#else /* !AMIGA */
@ -957,16 +957,14 @@ file_impossible (const char *filename)
#endif
}
if (dir->contents == 0)
if (dir->contents == NULL)
/* The directory could not be stat'd. We allocate a contents
structure for it, but leave it out of the contents hash table. */
dir->contents = xcalloc (sizeof (struct directory_contents));
if (dir->contents->dirfiles.ht_vec == 0)
{
hash_init (&dir->contents->dirfiles, DIRFILE_BUCKETS,
dirfile_hash_1, dirfile_hash_2, dirfile_hash_cmp);
}
if (dir->contents->dirfiles.ht_vec == NULL)
hash_init (&dir->contents->dirfiles, DIRFILE_BUCKETS,
dirfile_hash_1, dirfile_hash_2, dirfile_hash_cmp);
/* Make a new entry and put it in the table. */
@ -1018,7 +1016,7 @@ file_impossible_p (const char *filename)
dirend = filename + 1;
}
#endif /* HAVE_DOS_PATHS */
if (dirend == 0)
if (dirend == NULL)
#ifdef _AMIGA
dir = find_directory ("")->contents;
#else /* !AMIGA */
@ -1055,7 +1053,7 @@ file_impossible_p (const char *filename)
#endif
}
if (dir == 0 || dir->dirfiles.ht_vec == 0)
if (dir == NULL || dir->dirfiles.ht_vec == NULL)
/* There are no files entered for this directory. */
return 0;
@ -1112,9 +1110,9 @@ print_dir_data_base (void)
struct directory *dir = *dir_slot;
if (! HASH_VACANT (dir))
{
if (dir->contents == 0)
if (dir->contents == NULL)
printf (_("# %s: could not be stat'd.\n"), dir->name);
else if (dir->contents->dirfiles.ht_vec == 0)
else if (dir->contents->dirfiles.ht_vec == NULL)
#ifdef WINDOWS32
printf (_("# %s (key %s, mtime %s): could not be opened.\n"),
dir->name, dir->contents->path_key,
@ -1171,7 +1169,7 @@ print_dir_data_base (void)
else
printf ("%u", im);
fputs (_(" impossibilities"), stdout);
if (dir->contents->dirstream == 0)
if (dir->contents->dirstream == NULL)
puts (".");
else
puts (_(" so far."));
@ -1214,15 +1212,15 @@ open_dirstream (const char *directory)
struct dirstream *new;
struct directory *dir = find_directory (directory);
if (dir->contents == 0 || dir->contents->dirfiles.ht_vec == 0)
if (dir->contents == NULL || dir->contents->dirfiles.ht_vec == NULL)
/* DIR->contents is nil if the directory could not be stat'd.
DIR->contents->dirfiles is nil if it could not be opened. */
return 0;
return NULL;
/* Read all the contents of the directory now. There is no benefit
in being lazy, since glob will want to see every file anyway. */
dir_contents_file_exists_p (dir, 0);
dir_contents_file_exists_p (dir, NULL);
new = xmalloc (sizeof (struct dirstream));
new->contents = dir->contents;
@ -1276,7 +1274,7 @@ read_dirstream (__ptr_t stream)
}
}
return 0;
return NULL;
}
/* On 64 bit ReliantUNIX (5.44 and above) in LFS mode, stat() is actually a