Coverage Report

Created: 2025-12-14 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/uri/uriparser/src/UriResolve.c
Line
Count
Source
1
/*
2
 * uriparser - RFC 3986 URI parsing library
3
 *
4
 * Copyright (C) 2007, Weijia Song <songweijia@gmail.com>
5
 * Copyright (C) 2007, Sebastian Pipping <sebastian@pipping.org>
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source  and binary forms, with or without
9
 * modification, are permitted provided  that the following conditions
10
 * are met:
11
 *
12
 *     1. Redistributions  of  source  code   must  retain  the  above
13
 *        copyright notice, this list  of conditions and the following
14
 *        disclaimer.
15
 *
16
 *     2. Redistributions  in binary  form  must  reproduce the  above
17
 *        copyright notice, this list  of conditions and the following
18
 *        disclaimer  in  the  documentation  and/or  other  materials
19
 *        provided with the distribution.
20
 *
21
 *     3. Neither the  name of the  copyright holder nor the  names of
22
 *        its contributors may be used  to endorse or promote products
23
 *        derived from  this software  without specific  prior written
24
 *        permission.
25
 *
26
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27
 * "AS IS" AND  ANY EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT
28
 * LIMITED TO,  THE IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS
29
 * FOR  A  PARTICULAR  PURPOSE  ARE  DISCLAIMED.  IN  NO  EVENT  SHALL
30
 * THE  COPYRIGHT HOLDER  OR CONTRIBUTORS  BE LIABLE  FOR ANY  DIRECT,
31
 * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32
 * (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE GOODS OR
33
 * SERVICES; LOSS OF USE, DATA,  OR PROFITS; OR BUSINESS INTERRUPTION)
34
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35
 * STRICT  LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE  OR  OTHERWISE)
36
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
37
 * OF THE POSSIBILITY OF SUCH DAMAGE.
38
 */
39
40
/* What encodings are enabled? */
41
#include <uriparser/UriDefsConfig.h>
42
#if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE))
43
/* Include SELF twice */
44
#  ifdef URI_ENABLE_ANSI
45
#    define URI_PASS_ANSI 1
46
#    include "UriResolve.c"
47
#    undef URI_PASS_ANSI
48
#  endif
49
#  ifdef URI_ENABLE_UNICODE
50
#    define URI_PASS_UNICODE 1
51
#    include "UriResolve.c"
52
#    undef URI_PASS_UNICODE
53
#  endif
54
#else
55
#  ifdef URI_PASS_ANSI
56
#    include <uriparser/UriDefsAnsi.h>
57
#  else
58
#    include <uriparser/UriDefsUnicode.h>
59
#    include <wchar.h>
60
#  endif
61
62
#  ifndef URI_DOXYGEN
63
#    include <uriparser/Uri.h>
64
#    include "UriCommon.h"
65
#    include "UriMemory.h"
66
#  endif
67
68
/* Appends a relative URI to an absolute. The last path segment of
69
 * the absolute URI is replaced. */
70
static URI_INLINE UriBool URI_FUNC(MergePath)(URI_TYPE(Uri) * absWork,
71
                                              const URI_TYPE(Uri) * relAppend,
72
0
                                              UriMemoryManager * memory) {
73
0
    URI_TYPE(PathSegment) * sourceWalker;
74
0
    URI_TYPE(PathSegment) * destPrev;
75
0
    if (relAppend->pathHead == NULL) {
76
0
        return URI_TRUE;
77
0
    }
78
79
    /* Replace last segment ("" if trailing slash) with first of append chain */
80
0
    if (absWork->pathHead == NULL) {
81
0
        URI_TYPE(PathSegment) * const dup =
82
0
            memory->malloc(memory, sizeof(URI_TYPE(PathSegment)));
83
0
        if (dup == NULL) {
84
0
            return URI_FALSE; /* Raises malloc error */
85
0
        }
86
0
        dup->next = NULL;
87
0
        absWork->pathHead = dup;
88
0
        absWork->pathTail = dup;
89
0
    }
90
0
    absWork->pathTail->text.first = relAppend->pathHead->text.first;
91
0
    absWork->pathTail->text.afterLast = relAppend->pathHead->text.afterLast;
92
93
    /* Append all the others */
94
0
    sourceWalker = relAppend->pathHead->next;
95
0
    if (sourceWalker == NULL) {
96
0
        return URI_TRUE;
97
0
    }
98
0
    destPrev = absWork->pathTail;
99
100
0
    for (;;) {
101
0
        URI_TYPE(PathSegment) * const dup =
102
0
            memory->malloc(memory, sizeof(URI_TYPE(PathSegment)));
103
0
        if (dup == NULL) {
104
0
            destPrev->next = NULL;
105
0
            absWork->pathTail = destPrev;
106
0
            return URI_FALSE; /* Raises malloc error */
107
0
        }
108
0
        dup->text = sourceWalker->text;
109
0
        destPrev->next = dup;
110
111
0
        if (sourceWalker->next == NULL) {
112
0
            absWork->pathTail = dup;
113
0
            absWork->pathTail->next = NULL;
114
0
            break;
115
0
        }
116
0
        destPrev = dup;
117
0
        sourceWalker = sourceWalker->next;
118
0
    }
119
120
0
    return URI_TRUE;
121
0
}
Unexecuted instantiation: UriResolve.c:uriMergePathA
Unexecuted instantiation: UriResolve.c:uriMergePathW
122
123
static int URI_FUNC(ResolveAbsolutePathFlag)(URI_TYPE(Uri) * absWork,
124
0
                                             UriMemoryManager * memory) {
125
0
    if (absWork == NULL) {
126
0
        return URI_ERROR_NULL;
127
0
    }
128
129
0
    if (URI_FUNC(HasHost)(absWork) && absWork->absolutePath) {
130
        /* Empty segment needed, instead? */
131
0
        if (absWork->pathHead == NULL) {
132
0
            URI_TYPE(PathSegment) * const segment =
133
0
                memory->malloc(memory, sizeof(URI_TYPE(PathSegment)));
134
0
            if (segment == NULL) {
135
0
                return URI_ERROR_MALLOC;
136
0
            }
137
0
            segment->text.first = URI_FUNC(SafeToPointTo);
138
0
            segment->text.afterLast = URI_FUNC(SafeToPointTo);
139
0
            segment->next = NULL;
140
141
0
            absWork->pathHead = segment;
142
0
            absWork->pathTail = segment;
143
0
        }
144
145
0
        absWork->absolutePath = URI_FALSE;
146
0
    }
147
148
0
    return URI_SUCCESS;
149
0
}
Unexecuted instantiation: UriResolve.c:uriResolveAbsolutePathFlagA
Unexecuted instantiation: UriResolve.c:uriResolveAbsolutePathFlagW
150
151
static int URI_FUNC(AddBaseUriImpl)(URI_TYPE(Uri) * absDest,
152
                                    const URI_TYPE(Uri) * relSource,
153
                                    const URI_TYPE(Uri) * absBase,
154
                                    UriResolutionOptions options,
155
0
                                    UriMemoryManager * memory) {
156
0
    UriBool relSourceHasScheme;
157
158
0
    if (absDest == NULL) {
159
0
        return URI_ERROR_NULL;
160
0
    }
161
0
    URI_FUNC(ResetUri)(absDest);
162
163
0
    if ((relSource == NULL) || (absBase == NULL)) {
164
0
        return URI_ERROR_NULL;
165
0
    }
166
167
    /* absBase absolute? */
168
0
    if (absBase->scheme.first == NULL) {
169
0
        return URI_ERROR_ADDBASE_REL_BASE;
170
0
    }
171
172
    /* NOTE: The curly brackets here force deeper indent (and that's all) */
173
0
    {
174
0
        {
175
0
            {
176
                /* clang-format off */
177
    /* [00/32] -- A non-strict parser may ignore a scheme in the reference */
178
    /* [00/32] -- if it is identical to the base URI's scheme. */
179
    /* [00/32] if ((not strict) and (R.scheme == Base.scheme)) then */
180
                /* clang-format on */
181
0
                relSourceHasScheme =
182
0
                    (relSource->scheme.first != NULL) ? URI_TRUE : URI_FALSE;
183
0
                if ((options & URI_RESOLVE_IDENTICAL_SCHEME_COMPAT)
184
0
                    && (absBase->scheme.first != NULL)
185
0
                    && (relSource->scheme.first != NULL)
186
0
                    && (0
187
0
                        == URI_FUNC(CompareRange)(&(absBase->scheme),
188
0
                                                  &(relSource->scheme)))) {
189
                    /* clang-format off */
190
    /* [00/32]     undefine(R.scheme); */
191
                    /* clang-format on */
192
0
                    relSourceHasScheme = URI_FALSE;
193
                    /* clang-format off */
194
    /* [00/32] endif; */
195
                    /* clang-format on */
196
0
                }
197
198
                /* clang-format off */
199
    /* [01/32] if defined(R.scheme) then */
200
                /* clang-format on */
201
0
                if (relSourceHasScheme) {
202
                    /* clang-format off */
203
    /* [02/32]     T.scheme = R.scheme; */
204
                    /* clang-format on */
205
0
                    absDest->scheme = relSource->scheme;
206
                    /* clang-format off */
207
    /* [03/32]     T.authority = R.authority; */
208
                    /* clang-format on */
209
0
                    if (!URI_FUNC(CopyAuthority)(absDest, relSource, memory)) {
210
0
                        return URI_ERROR_MALLOC;
211
0
                    }
212
                    /* clang-format off */
213
    /* [04/32]     T.path = remove_dot_segments(R.path); */
214
                    /* clang-format on */
215
0
                    if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) {
216
0
                        return URI_ERROR_MALLOC;
217
0
                    }
218
0
                    if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, memory)) {
219
0
                        return URI_ERROR_MALLOC;
220
0
                    }
221
                    /* clang-format off */
222
    /* [05/32]     T.query = R.query; */
223
                    /* clang-format on */
224
0
                    absDest->query = relSource->query;
225
                    /* clang-format off */
226
    /* [06/32] else */
227
                    /* clang-format on */
228
0
                } else {
229
                    /* clang-format off */
230
    /* [07/32]     if defined(R.authority) then */
231
                    /* clang-format on */
232
0
                    if (URI_FUNC(HasHost)(relSource)) {
233
                        /* clang-format off */
234
    /* [08/32]         T.authority = R.authority; */
235
                        /* clang-format on */
236
0
                        if (!URI_FUNC(CopyAuthority)(absDest, relSource, memory)) {
237
0
                            return URI_ERROR_MALLOC;
238
0
                        }
239
                        /* clang-format off */
240
    /* [09/32]         T.path = remove_dot_segments(R.path); */
241
                        /* clang-format on */
242
0
                        if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) {
243
0
                            return URI_ERROR_MALLOC;
244
0
                        }
245
0
                        if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, memory)) {
246
0
                            return URI_ERROR_MALLOC;
247
0
                        }
248
                        /* clang-format off */
249
    /* [10/32]         T.query = R.query; */
250
                        /* clang-format on */
251
0
                        absDest->query = relSource->query;
252
                        /* clang-format off */
253
    /* [11/32]     else */
254
                        /* clang-format on */
255
0
                    } else {
256
                        /* clang-format off */
257
    /* [28/32]         T.authority = Base.authority; */
258
                        /* clang-format on */
259
0
                        if (!URI_FUNC(CopyAuthority)(absDest, absBase, memory)) {
260
0
                            return URI_ERROR_MALLOC;
261
0
                        }
262
                        /* clang-format off */
263
    /* [12/32]         if (R.path == "") then */
264
                        /* clang-format on */
265
0
                        if (relSource->pathHead == NULL && !relSource->absolutePath) {
266
                            /* clang-format off */
267
    /* [13/32]             T.path = Base.path; */
268
                            /* clang-format on */
269
0
                            if (!URI_FUNC(CopyPath)(absDest, absBase, memory)) {
270
0
                                return URI_ERROR_MALLOC;
271
0
                            }
272
                            /* clang-format off */
273
    /* [14/32]             if defined(R.query) then */
274
                            /* clang-format on */
275
0
                            if (relSource->query.first != NULL) {
276
                                /* clang-format off */
277
    /* [15/32]                 T.query = R.query; */
278
                                /* clang-format on */
279
0
                                absDest->query = relSource->query;
280
                                /* clang-format off */
281
    /* [16/32]             else */
282
                                /* clang-format on */
283
0
                            } else {
284
                                /* clang-format off */
285
    /* [17/32]                 T.query = Base.query; */
286
                                /* clang-format on */
287
0
                                absDest->query = absBase->query;
288
                                /* clang-format off */
289
    /* [18/32]             endif; */
290
                                /* clang-format on */
291
0
                            }
292
                            /* clang-format off */
293
    /* [19/32]         else */
294
                            /* clang-format on */
295
0
                        } else {
296
                            /* clang-format off */
297
    /* [20/32]             if (R.path starts-with "/") then */
298
                            /* clang-format on */
299
0
                            if (relSource->absolutePath) {
300
0
                                int res;
301
                                /* clang-format off */
302
    /* [21/32]                 T.path = remove_dot_segments(R.path); */
303
                                /* clang-format on */
304
0
                                if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) {
305
0
                                    return URI_ERROR_MALLOC;
306
0
                                }
307
0
                                res = URI_FUNC(ResolveAbsolutePathFlag)(absDest, memory);
308
0
                                if (res != URI_SUCCESS) {
309
0
                                    return res;
310
0
                                }
311
0
                                if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest,
312
0
                                                                         memory)) {
313
0
                                    return URI_ERROR_MALLOC;
314
0
                                }
315
                                /* clang-format off */
316
    /* [22/32]             else */
317
                                /* clang-format on */
318
0
                            } else {
319
                                /* clang-format off */
320
    /* [23/32]                 T.path = merge(Base.path, R.path); */
321
                                /* clang-format on */
322
0
                                if (!URI_FUNC(CopyPath)(absDest, absBase, memory)) {
323
0
                                    return URI_ERROR_MALLOC;
324
0
                                }
325
0
                                if (!URI_FUNC(MergePath)(absDest, relSource, memory)) {
326
0
                                    return URI_ERROR_MALLOC;
327
0
                                }
328
                                /* clang-format off */
329
    /* [24/32]                 T.path = remove_dot_segments(T.path); */
330
                                /* clang-format on */
331
0
                                if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest,
332
0
                                                                         memory)) {
333
0
                                    return URI_ERROR_MALLOC;
334
0
                                }
335
336
0
                                if (!URI_FUNC(FixAmbiguity)(absDest, memory)) {
337
0
                                    return URI_ERROR_MALLOC;
338
0
                                }
339
                                /* clang-format off */
340
    /* [25/32]             endif; */
341
0
                            }
342
    /* clang-format off */
343
    /* [26/32]             T.query = R.query; */
344
                            /* clang-format on */
345
0
                            absDest->query = relSource->query;
346
                            /* clang-format off */
347
    /* [27/32]         endif; */
348
                            /* clang-format on */
349
0
                        }
350
0
                        URI_FUNC(FixEmptyTrailSegment)(absDest, memory);
351
                        /* clang-format off */
352
    /* [29/32]     endif; */
353
                        /* clang-format on */
354
0
                    }
355
                    /* clang-format off */
356
    /* [30/32]     T.scheme = Base.scheme; */
357
                    /* clang-format on */
358
0
                    absDest->scheme = absBase->scheme;
359
                    /* clang-format off */
360
    /* [31/32] endif; */
361
                    /* clang-format on */
362
0
                }
363
                /* clang-format off */
364
    /* [32/32] T.fragment = R.fragment; */
365
                /* clang-format on */
366
0
                absDest->fragment = relSource->fragment;
367
0
            }
368
0
        }
369
0
    }
370
0
    return URI_SUCCESS;
371
0
}
Unexecuted instantiation: UriResolve.c:uriAddBaseUriImplA
Unexecuted instantiation: UriResolve.c:uriAddBaseUriImplW
372
373
int URI_FUNC(AddBaseUri)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource,
374
0
                         const URI_TYPE(Uri) * absBase) {
375
0
    const UriResolutionOptions options = URI_RESOLVE_STRICTLY;
376
0
    return URI_FUNC(AddBaseUriEx)(absDest, relSource, absBase, options);
377
0
}
Unexecuted instantiation: uriAddBaseUriA
Unexecuted instantiation: uriAddBaseUriW
378
379
int URI_FUNC(AddBaseUriEx)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource,
380
0
                           const URI_TYPE(Uri) * absBase, UriResolutionOptions options) {
381
0
    return URI_FUNC(AddBaseUriExMm)(absDest, relSource, absBase, options, NULL);
382
0
}
Unexecuted instantiation: uriAddBaseUriExA
Unexecuted instantiation: uriAddBaseUriExW
383
384
int URI_FUNC(AddBaseUriExMm)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource,
385
                             const URI_TYPE(Uri) * absBase, UriResolutionOptions options,
386
0
                             UriMemoryManager * memory) {
387
0
    int res;
388
389
0
    URI_CHECK_MEMORY_MANAGER(memory); /* may return */
390
391
0
    res = URI_FUNC(AddBaseUriImpl)(absDest, relSource, absBase, options, memory);
392
0
    if ((res != URI_SUCCESS) && (absDest != NULL)) {
393
0
        URI_FUNC(FreeUriMembersMm)(absDest, memory);
394
0
    }
395
0
    return res;
396
0
}
Unexecuted instantiation: uriAddBaseUriExMmA
Unexecuted instantiation: uriAddBaseUriExMmW
397
398
#endif