Coverage Report

Created: 2026-01-09 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/common/mischelp.c
Line
Count
Source
1
/* mischelp.c - Miscellaneous helper functions
2
 * Copyright (C) 1998, 2000, 2001, 2006, 2007 Free Software Foundation, Inc.
3
 *
4
 * This file is part of GnuPG.
5
 *
6
 * GnuPG is free software; you can redistribute and/or modify this
7
 * part of GnuPG under the terms of either
8
 *
9
 *   - the GNU Lesser General Public License as published by the Free
10
 *     Software Foundation; either version 3 of the License, or (at
11
 *     your option) any later version.
12
 *
13
 * or
14
 *
15
 *   - the GNU General Public License as published by the Free
16
 *     Software Foundation; either version 2 of the License, or (at
17
 *     your option) any later version.
18
 *
19
 * or both in parallel, as here.
20
 *
21
 * GnuPG is distributed in the hope that it will be useful, but
22
 * WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24
 * General Public License for more details.
25
 *
26
 * You should have received a copies of the GNU General Public License
27
 * and the GNU Lesser General Public License along with this program;
28
 * if not, see <https://www.gnu.org/licenses/>.
29
 */
30
31
#include <config.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include <time.h>
35
#ifdef HAVE_W32_SYSTEM
36
# define WIN32_LEAN_AND_MEAN
37
# include <windows.h>
38
#else /*!HAVE_W32_SYSTEM*/
39
# include <sys/types.h>
40
# include <sys/stat.h>
41
# include <unistd.h>
42
#endif /*!HAVE_W32_SYSTEM*/
43
#include <errno.h>
44
45
#include "util.h"
46
#include "common-defs.h"
47
#include "stringhelp.h"
48
#include "utf8conv.h"
49
#include "mischelp.h"
50
51
52
void
53
wipememory (void *ptr, size_t len)
54
0
{
55
#if defined(HAVE_W32_SYSTEM) && defined(SecureZeroMemory)
56
  SecureZeroMemory (ptr, len);
57
#elif defined(HAVE_EXPLICIT_BZERO)
58
  explicit_bzero (ptr, len);
59
#else
60
  /* Prevent compiler from optimizing away the call to memset by accessing
61
     memset through volatile pointer. */
62
  static void *(*volatile memset_ptr)(void *, int, size_t) = (void *)memset;
63
  memset_ptr (ptr, 0, len);
64
#endif
65
0
}
66
67
68
/* Check whether the files NAME1 and NAME2 are identical.  This is for
69
   example achieved by comparing the inode numbers of the files.  */
70
int
71
same_file_p (const char *name1, const char *name2)
72
0
{
73
0
  int yes;
74
75
  /* First try a shortcut.  */
76
0
  if (!compare_filenames (name1, name2))
77
0
    yes = 1;
78
0
  else
79
0
    {
80
#ifdef HAVE_W32_SYSTEM
81
      HANDLE file1, file2;
82
      BY_HANDLE_FILE_INFORMATION info1, info2;
83
      wchar_t *wname;
84
85
      wname = gpgrt_fname_to_wchar (name1);
86
      if (wname)
87
        {
88
          /* Note that if the desiredAccess (2nd arg) is zero, we are
89
           * allowed to query certain metadata even if GENERIC_READ
90
           * would have not been granted.  */
91
          file1 = CreateFileW (wname, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
92
          xfree (wname);
93
        }
94
      else
95
        file1 = INVALID_HANDLE_VALUE;
96
97
      if (file1 == INVALID_HANDLE_VALUE)
98
        yes = 0; /* If we can't open the file, it is not the same.  */
99
      else
100
        {
101
          wname = gpgrt_fname_to_wchar (name2);
102
          if (wname)
103
            {
104
              file2 = CreateFileW (wname, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
105
              xfree (wname);
106
            }
107
          else
108
            file2 = INVALID_HANDLE_VALUE;
109
110
          if (file2 == INVALID_HANDLE_VALUE)
111
            yes = 0; /* If we can't open the file, it is not the same.  */
112
          else
113
            {
114
              yes = (GetFileInformationByHandle (file1, &info1)
115
                     && GetFileInformationByHandle (file2, &info2)
116
                     && info1.dwVolumeSerialNumber==info2.dwVolumeSerialNumber
117
                     && info1.nFileIndexHigh == info2.nFileIndexHigh
118
                     && info1.nFileIndexLow == info2.nFileIndexLow);
119
              CloseHandle (file2);
120
            }
121
          CloseHandle (file1);
122
        }
123
#else /*!HAVE_W32_SYSTEM*/
124
0
      struct stat info1, info2;
125
126
0
      yes = (!stat (name1, &info1) && !stat (name2, &info2)
127
0
             && info1.st_dev == info2.st_dev && info1.st_ino == info2.st_ino);
128
0
#endif /*!HAVE_W32_SYSTEM*/
129
0
    }
130
0
  return yes;
131
0
}