Coverage Report

Created: 2026-09-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/path/include/PathCchAppend.h
Line
Count
Source
1
2
/*
3
#define DEFINE_UNICODE    FALSE
4
#define CUR_PATH_SEPARATOR_CHR  '\\'
5
#define CUR_PATH_SEPARATOR_STR  "\\"
6
#define PATH_CCH_APPEND   PathCchAppendA
7
*/
8
9
#include <string.h>
10
11
#include <winpr/wtypes.h>
12
#include <winpr/error.h>
13
#include <winpr/path.h>
14
15
#if defined(DEFINE_UNICODE) && (DEFINE_UNICODE != 0)
16
17
HRESULT PATH_CCH_APPEND(PWSTR pszPath, size_t cchPath, PCWSTR pszMore)
18
0
{
19
0
  if (!pszPath)
20
0
    return E_INVALIDARG;
21
22
0
  if (!pszMore)
23
0
    return S_OK;
24
25
0
  if ((cchPath == 0) || (cchPath > PATHCCH_MAX_CCH))
26
0
    return E_INVALIDARG;
27
28
0
  const size_t pszMoreLength = _wcsnlen(pszMore, cchPath);
29
0
  const size_t pszPathLength = _wcsnlen(pszPath, cchPath);
30
31
0
  BOOL pathBackslash = FALSE;
32
0
  if (pszPathLength > 0)
33
0
    pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
34
35
0
  const BOOL moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
36
37
0
  if (pathBackslash && moreBackslash)
38
0
  {
39
0
    if (pszMoreLength < 1)
40
0
      return E_INVALIDARG;
41
42
0
    if ((pszPathLength + pszMoreLength - 1) < cchPath)
43
0
    {
44
0
      WCHAR* ptr = &pszPath[pszPathLength];
45
0
      *ptr = '\0';
46
0
      _wcsncat(ptr, &pszMore[1], pszMoreLength - 1);
47
0
      return S_OK;
48
0
    }
49
0
  }
50
0
  else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
51
0
  {
52
0
    if ((pszPathLength + pszMoreLength) < cchPath)
53
0
    {
54
0
      WCHAR* ptr = &pszPath[pszPathLength];
55
0
      *ptr = '\0';
56
0
      _wcsncat(ptr, pszMore, pszMoreLength);
57
0
      return S_OK;
58
0
    }
59
0
  }
60
0
  else if (!pathBackslash && !moreBackslash)
61
0
  {
62
0
    if ((pszPathLength + pszMoreLength + 1) < cchPath)
63
0
    {
64
0
      WCHAR* ptr = &pszPath[pszPathLength];
65
0
      *ptr = '\0';
66
0
      _wcsncat(ptr, CUR_PATH_SEPARATOR_STR,
67
0
               _wcsnlen(CUR_PATH_SEPARATOR_STR, ARRAYSIZE(CUR_PATH_SEPARATOR_STR)));
68
0
      _wcsncat(ptr, pszMore, pszMoreLength);
69
0
      return S_OK;
70
0
    }
71
0
  }
72
73
0
  return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
74
0
}
Unexecuted instantiation: PathCchAppendW
Unexecuted instantiation: UnixPathCchAppendW
Unexecuted instantiation: NativePathCchAppendW
75
76
#else
77
78
HRESULT PATH_CCH_APPEND(PSTR pszPath, size_t cchPath, PCSTR pszMore)
79
45.0k
{
80
45.0k
  BOOL pathBackslash = FALSE;
81
45.0k
  BOOL moreBackslash = FALSE;
82
83
45.0k
  if (!pszPath)
84
0
    return E_INVALIDARG;
85
86
45.0k
  if (!pszMore)
87
0
    return S_OK;
88
89
45.0k
  if ((cchPath == 0) || (cchPath > PATHCCH_MAX_CCH))
90
0
    return E_INVALIDARG;
91
92
45.0k
  const size_t pszPathLength = strnlen(pszPath, cchPath);
93
45.0k
  if (pszPathLength > 0)
94
45.0k
    pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
95
96
45.0k
  const size_t pszMoreLength = strnlen(pszMore, cchPath);
97
45.0k
  if (pszMoreLength > 0)
98
45.0k
    moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
99
100
45.0k
  if (pathBackslash && moreBackslash)
101
0
  {
102
0
    if ((pszPathLength + pszMoreLength - 1) < cchPath)
103
0
    {
104
0
      if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", &pszMore[1]) < 0)
105
0
        return E_FAIL;
106
0
      return S_OK;
107
0
    }
108
0
  }
109
45.0k
  else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
110
0
  {
111
0
    if ((pszPathLength + pszMoreLength) < cchPath)
112
0
    {
113
0
      if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", pszMore) < 0)
114
0
        return E_FAIL;
115
0
      return S_OK;
116
0
    }
117
0
  }
118
45.0k
  else if (!pathBackslash && !moreBackslash)
119
45.0k
  {
120
45.0k
    if ((pszPathLength + pszMoreLength + 1) < cchPath)
121
45.0k
    {
122
45.0k
      if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s%s",
123
45.0k
                    CUR_PATH_SEPARATOR_STR, pszMore) < 0)
124
0
        return E_FAIL;
125
45.0k
      return S_OK;
126
45.0k
    }
127
45.0k
  }
128
129
0
  return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
130
45.0k
}
Unexecuted instantiation: PathCchAppendA
Unexecuted instantiation: UnixPathCchAppendA
NativePathCchAppendA
Line
Count
Source
79
45.0k
{
80
45.0k
  BOOL pathBackslash = FALSE;
81
45.0k
  BOOL moreBackslash = FALSE;
82
83
45.0k
  if (!pszPath)
84
0
    return E_INVALIDARG;
85
86
45.0k
  if (!pszMore)
87
0
    return S_OK;
88
89
45.0k
  if ((cchPath == 0) || (cchPath > PATHCCH_MAX_CCH))
90
0
    return E_INVALIDARG;
91
92
45.0k
  const size_t pszPathLength = strnlen(pszPath, cchPath);
93
45.0k
  if (pszPathLength > 0)
94
45.0k
    pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
95
96
45.0k
  const size_t pszMoreLength = strnlen(pszMore, cchPath);
97
45.0k
  if (pszMoreLength > 0)
98
45.0k
    moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
99
100
45.0k
  if (pathBackslash && moreBackslash)
101
0
  {
102
0
    if ((pszPathLength + pszMoreLength - 1) < cchPath)
103
0
    {
104
0
      if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", &pszMore[1]) < 0)
105
0
        return E_FAIL;
106
0
      return S_OK;
107
0
    }
108
0
  }
109
45.0k
  else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
110
0
  {
111
0
    if ((pszPathLength + pszMoreLength) < cchPath)
112
0
    {
113
0
      if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", pszMore) < 0)
114
0
        return E_FAIL;
115
0
      return S_OK;
116
0
    }
117
0
  }
118
45.0k
  else if (!pathBackslash && !moreBackslash)
119
45.0k
  {
120
45.0k
    if ((pszPathLength + pszMoreLength + 1) < cchPath)
121
45.0k
    {
122
45.0k
      if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s%s",
123
45.0k
                    CUR_PATH_SEPARATOR_STR, pszMore) < 0)
124
0
        return E_FAIL;
125
45.0k
      return S_OK;
126
45.0k
    }
127
45.0k
  }
128
129
0
  return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
130
45.0k
}
131
132
#endif
133
134
/*
135
#undef DEFINE_UNICODE
136
#undef CUR_PATH_SEPARATOR_CHR
137
#undef CUR_PATH_SEPARATOR_STR
138
#undef PATH_CCH_APPEND
139
*/