From a5cf8700b6b69d5b9a389a98bfa2abbcd76388f7 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Thu, 12 Aug 2021 14:34:10 +0300 Subject: Fix NULL dereference in gdbmshell. Undefine "fd" upon closing the database. --- doc/gdbm.texi | 30 ++++++++++++++++++++++++------ src/gdbmshell.c | 1 + src/var.c | 10 ++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/doc/gdbm.texi b/doc/gdbm.texi index a989e48..9c54f74 100644 --- a/doc/gdbm.texi +++ b/doc/gdbm.texi @@ -3487,25 +3487,43 @@ See also @code{first}, above. @end deffn @deffn {command verb} open @var{filename} -Open the database file @var{filename}. If successful, any previously -open database is closed. Otherwise, if the operation fails, the -currently opened database remains unchanged. +@deffnx {command verb} open +Open the database file @var{filename}. If used without arguments, the +database name is taken from the variable @code{filename}. + +If successful, any previously open database is closed and the +@code{filename} variable is updated. Otherwise, if the operation +fails, the currently opened database remains unchanged. This command takes additional information from the following variables: -@table @samp +@table @code +@item filename +Name of the database to open, if no argument is given. + +@item fd +File descriptor to use. If set, this must be an open file descriptor +referring to a valid database file. The database will be opened using +@code{gdbm_fd_open} (@pxref{gdbm_fd_open}). The file descriptor will +be closed upon closing the database. + +@item filemode +Specifies the permissions to use in case a new file is created. + @item open The database access mode. @xref{openvar,, The @var{open} variable}, for a list of its values. + @item lock Whether or not to lock the database. Default is @code{on}. + @item mmap Use the memory mapping. Default is @code{on}. + @item sync Synchronize after each write. Default is @code{off}. -@item filemode -Specifies the permissions to use in case a new file is created. + @end table @xref{open parameters}, for a detailed description of these variables. diff --git a/src/gdbmshell.c b/src/gdbmshell.c index 8a2b07b..62402e1 100644 --- a/src/gdbmshell.c +++ b/src/gdbmshell.c @@ -67,6 +67,7 @@ closedb (void) datum_free (&key_data); datum_free (&return_data); + variable_unset ("fd"); } static int diff --git a/src/var.c b/src/var.c index 2772eb1..7b583e2 100644 --- a/src/var.c +++ b/src/var.c @@ -567,6 +567,8 @@ open_typeconv (struct variable *var, int type, void **retptr) static int format_sethook (struct variable *var, union value *v) { + if (!v) + return VAR_OK; return _gdbm_str2fmt (v->string) == -1 ? VAR_ERR_BADVALUE : VAR_OK; } @@ -584,6 +586,8 @@ format_typeconv (struct variable *var, int type, void **retptr) static int fd_sethook (struct variable *var, union value *v) { + if (!v) + return VAR_OK; if (v->num < 0) return VAR_ERR_BADVALUE; return VAR_OK; @@ -592,6 +596,8 @@ fd_sethook (struct variable *var, union value *v) static int cachesize_sethook (struct variable *var, union value *v) { + if (!v) + return VAR_OK; if (v->num < 0) return VAR_ERR_BADVALUE; return gdbmshell_setopt ("GDBM_SETCACHESIZE", GDBM_SETCACHESIZE, v->num) == 0 @@ -601,6 +607,8 @@ cachesize_sethook (struct variable *var, union value *v) static int centfree_sethook (struct variable *var, union value *v) { + if (!v) + return VAR_OK; return gdbmshell_setopt ("GDBM_SETCENTFREE", GDBM_SETCENTFREE, v->bool) == 0 ? VAR_OK : VAR_ERR_BADVALUE; } @@ -608,6 +616,8 @@ centfree_sethook (struct variable *var, union value *v) static int coalesce_sethook (struct variable *var, union value *v) { + if (!v) + return VAR_OK; return gdbmshell_setopt ("GDBM_SETCOALESCEBLKS", GDBM_SETCOALESCEBLKS, v->bool) == 0 ? VAR_OK : VAR_ERR_BADVALUE; } -- cgit v1.2.1