Coverage Report

Created: 2026-03-16 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdbm/src/gdbmfetch.c
Line
Count
Source
1
/* gdbmfetch.c - Find a key and return the associated data.  */
2
3
/* This file is part of GDBM, the GNU data base manager.
4
   Copyright (C) 1990-2025 Free Software Foundation, Inc.
5
6
   GDBM is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3, or (at your option)
9
   any later version.
10
11
   GDBM is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */
18
19
/* Include system configuration before all else. */
20
#include "autoconf.h"
21
22
#include "gdbmdefs.h"
23
24
/* Look up a given KEY and return the information associated with that KEY.
25
   The pointer in the structure that is  returned is a pointer to dynamically
26
   allocated memory block.  */
27
28
datum
29
gdbm_fetch (GDBM_FILE dbf, datum key)
30
2.00k
{
31
2.00k
  datum  return_val;    /* The return value. */
32
2.00k
  int    elem_loc;    /* The location in the bucket. */
33
2.00k
  char  *find_data;   /* Returned from find_key. */
34
35
2.00k
  GDBM_DEBUG_DATUM (GDBM_DEBUG_READ, key, "%s: fetching key:", dbf->name);
36
37
  /* Set the default return value. */
38
2.00k
  return_val.dptr  = NULL;
39
2.00k
  return_val.dsize = 0;
40
41
  /* Return immediately if the database needs recovery */  
42
2.00k
  GDBM_ASSERT_CONSISTENCY (dbf, return_val);
43
  
44
  /* Initialize the gdbm_errno variable. */
45
2.00k
  gdbm_set_errno (dbf, GDBM_NO_ERROR, FALSE);
46
47
  /* Find the key and return a pointer to the data. */
48
2.00k
  elem_loc = _gdbm_findkey (dbf, key, &find_data, NULL);
49
50
  /* Copy the data if the key was found.  */
51
2.00k
  if (elem_loc >= 0)
52
280
    {
53
      /* This is the item.  Return the associated data. */
54
280
      return_val.dsize = dbf->bucket->h_table[elem_loc].data_size;
55
280
      if (return_val.dsize == 0)
56
62
  return_val.dptr = (char *) malloc (1);
57
218
      else
58
218
  return_val.dptr = (char *) malloc (return_val.dsize);
59
280
      if (return_val.dptr == NULL)
60
0
  {
61
0
    GDBM_SET_ERRNO2 (dbf, GDBM_MALLOC_ERROR, FALSE, GDBM_DEBUG_READ);
62
0
    return return_val;
63
0
  }
64
280
      memcpy (return_val.dptr, find_data, return_val.dsize);
65
      
66
280
      GDBM_DEBUG_DATUM (GDBM_DEBUG_READ, return_val,
67
280
      "%s: found", dbf->name);
68
280
    }
69
1.72k
  else
70
1.72k
    GDBM_DEBUG (GDBM_DEBUG_READ, "%s: key not found", dbf->name);
71
  
72
2.00k
  return return_val;
73
2.00k
}