Coverage Report

Created: 2025-06-13 06:41

/src/osquery/osquery/utils/system/posix/filepath.cpp
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * Copyright (c) 2014-present, The osquery authors
3
 *
4
 * This source code is licensed as defined by the LICENSE file found in the
5
 * root directory of this source tree.
6
 *
7
 * SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)
8
 */
9
10
#include <osquery/utils/system/filepath.h>
11
12
#include <climits>
13
#include <cstdlib>
14
#include <cstring>
15
#include <string>
16
17
namespace osquery {
18
19
0
const std::string canonicalize_file_name(const char* name) {
20
0
#ifdef PATH_MAX
21
  // On supported platforms where PATH_MAX is defined we can pass null
22
  // as buffer, and allow libc to alloced space
23
  // On centos/ubuntu libc will use realloc so we will be able to resolve
24
  // any path
25
  // On darwin libc will allocate PATH_MAX buffer for us
26
0
  char* resolved = realpath(name, nullptr);
27
0
  std::string result = (resolved == nullptr) ? name : resolved;
28
0
  free(resolved);
29
#else
30
#warning PATH_MAX is undefined, please read comment below
31
  // PATH_MAX is not defined, very likely it's not officially supported
32
  // os, our best guess is _PC_PATH_MAX if available
33
  // In case of failure fallback to "safe" buffer of 8K
34
35
  long int path_max = pathconf(name, _PC_PATH_MAX);
36
  if (path_max <= 0) {
37
    path_max = 8 * 1024;
38
  }
39
  char* buffer = static_cast<char*>(malloc(path_max));
40
  char* resolved = realpath(name, buffer);
41
  std::string result = (resolved == nullptr) ? name : resolved;
42
  free(buffer);
43
#endif
44
0
  return result;
45
0
}
46
47
} // namespace osquery