summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2021-08-11 08:44:34 +0300
committerSergey Poznyakoff <gray@gnu.org>2021-08-11 08:44:34 +0300
commit3d47e1c06c2bc5ed0dfd6345cf5cc0477b23b5b8 (patch)
treeafa102b84423de241edf661782fb14bdd0747856
parent7ef79d63fdabba3a9bf49468ef9ecfbcb0bf117f (diff)
downloadgdbm-3d47e1c06c2bc5ed0dfd6345cf5cc0477b23b5b8.tar.gz
Fix memory leaks in gdbm_fd_open
* src/gdbmopen.c (gdbm_fd_open): Close the database before returning on error.
-rw-r--r--src/gdbmopen.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/gdbmopen.c b/src/gdbmopen.c
index ac7ed46..27896d4 100644
--- a/src/gdbmopen.c
+++ b/src/gdbmopen.c
@@ -290,6 +290,7 @@ gdbm_fd_open (int fd, const char *file_name, int block_size,
{
if (flags & GDBM_CLOERROR)
close (fd);
+ _gdbm_cache_free (dbf);
free (dbf);
GDBM_SET_ERRNO2 (NULL, GDBM_MALLOC_ERROR, FALSE, GDBM_DEBUG_OPEN);
return NULL;
@@ -326,10 +327,9 @@ gdbm_fd_open (int fd, const char *file_name, int block_size,
/* Zero-length file can't be a reader... */
if (((flags & GDBM_OPENMASK) == GDBM_READER) && (file_stat.st_size == 0))
{
- if (flags & GDBM_CLOERROR)
- close (dbf->desc);
- free (dbf->name);
- free (dbf);
+ if (!(flags & GDBM_CLOERROR))
+ dbf->desc = -1;
+ gdbm_close (dbf);
GDBM_SET_ERRNO2 (NULL, GDBM_EMPTY_DATABASE, FALSE, GDBM_DEBUG_OPEN);
return NULL;
}
@@ -342,10 +342,9 @@ gdbm_fd_open (int fd, const char *file_name, int block_size,
{
if (_gdbm_lock_file (dbf) == -1)
{
- if (flags & GDBM_CLOERROR)
- close (dbf->desc);
- free (dbf->name);
- free (dbf);
+ if (!(flags & GDBM_CLOERROR))
+ dbf->desc = -1;
+ SAVE_ERRNO (gdbm_close (dbf));
GDBM_SET_ERRNO2 (NULL,
(flags & GDBM_OPENMASK) == GDBM_READER
? GDBM_CANT_BE_READER : GDBM_CANT_BE_WRITER,
@@ -371,10 +370,9 @@ gdbm_fd_open (int fd, const char *file_name, int block_size,
if (gdbm_last_errno (dbf))
{
- if (flags & GDBM_CLOERROR)
- close (dbf->desc);
- free (dbf->name);
- free (dbf);
+ if (!(flags & GDBM_CLOERROR))
+ dbf->desc = -1;
+ SAVE_ERRNO (gdbm_close (dbf));
return NULL;
}
}
@@ -594,6 +592,7 @@ gdbm_fd_open (int fd, const char *file_name, int block_size,
{
if (!(flags & GDBM_CLOERROR))
dbf->desc = -1;
+ gdbm_close (dbf);
GDBM_SET_ERRNO2 (NULL, GDBM_BAD_HEADER, FALSE, GDBM_DEBUG_OPEN);
return NULL;
}