Coverage Report

Created: 2025-12-14 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/uri/uriparser/src/UriSetFragment.c
Line
Count
Source
1
/*
2
 * uriparser - RFC 3986 URI parsing library
3
 *
4
 * Copyright (C) 2025, Sebastian Pipping <sebastian@pipping.org>
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source  and binary forms, with or without
8
 * modification, are permitted provided  that the following conditions
9
 * are met:
10
 *
11
 *     1. Redistributions  of  source  code   must  retain  the  above
12
 *        copyright notice, this list  of conditions and the following
13
 *        disclaimer.
14
 *
15
 *     2. Redistributions  in binary  form  must  reproduce the  above
16
 *        copyright notice, this list  of conditions and the following
17
 *        disclaimer  in  the  documentation  and/or  other  materials
18
 *        provided with the distribution.
19
 *
20
 *     3. Neither the  name of the  copyright holder nor the  names of
21
 *        its contributors may be used  to endorse or promote products
22
 *        derived from  this software  without specific  prior written
23
 *        permission.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 * "AS IS" AND  ANY EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT
27
 * LIMITED TO,  THE IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS
28
 * FOR  A  PARTICULAR  PURPOSE  ARE  DISCLAIMED.  IN  NO  EVENT  SHALL
29
 * THE  COPYRIGHT HOLDER  OR CONTRIBUTORS  BE LIABLE  FOR ANY  DIRECT,
30
 * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31
 * (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE GOODS OR
32
 * SERVICES; LOSS OF USE, DATA,  OR PROFITS; OR BUSINESS INTERRUPTION)
33
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34
 * STRICT  LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE  OR  OTHERWISE)
35
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36
 * OF THE POSSIBILITY OF SUCH DAMAGE.
37
 */
38
39
/* What encodings are enabled? */
40
#include <uriparser/UriDefsConfig.h>
41
#if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE))
42
/* Include SELF twice */
43
#  ifdef URI_ENABLE_ANSI
44
#    define URI_PASS_ANSI 1
45
#    include "UriSetFragment.c"
46
#    undef URI_PASS_ANSI
47
#  endif
48
#  ifdef URI_ENABLE_UNICODE
49
#    define URI_PASS_UNICODE 1
50
#    include "UriSetFragment.c"
51
#    undef URI_PASS_UNICODE
52
#  endif
53
#else
54
#  ifdef URI_PASS_ANSI
55
#    include <uriparser/UriDefsAnsi.h>
56
#  else
57
#    include <uriparser/UriDefsUnicode.h>
58
#    include <wchar.h>
59
#  endif
60
61
#  ifndef URI_DOXYGEN
62
#    include <uriparser/Uri.h>
63
#    include "UriCommon.h"
64
#    include "UriMemory.h"
65
#    include "UriSets.h"
66
#  endif
67
68
#  include <assert.h>
69
70
UriBool URI_FUNC(IsWellFormedFragment)(const URI_CHAR * first,
71
0
                                       const URI_CHAR * afterLast) {
72
0
    if ((first == NULL) || (afterLast == NULL)) {
73
0
        return URI_FALSE;
74
0
    }
75
76
    /* The related part of the grammar in RFC 3986 reads:
77
     *
78
     *   fragment      = *( pchar / "/" / "?" )
79
     *   pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
80
     */
81
0
    while (first < afterLast) {
82
0
        switch (first[0]) {
83
0
        case URI_SET_PCHAR_WITHOUT_PERCENT(_UT):
84
0
            break;
85
86
        /* pct-encoded */
87
0
        case _UT('%'):
88
0
            if (afterLast - first < 3) {
89
0
                return URI_FALSE;
90
0
            }
91
0
            switch (first[1]) {
92
0
            case URI_SET_HEXDIG(_UT):
93
0
                break;
94
0
            default:
95
0
                return URI_FALSE;
96
0
            }
97
0
            switch (first[2]) {
98
0
            case URI_SET_HEXDIG(_UT):
99
0
                break;
100
0
            default:
101
0
                return URI_FALSE;
102
0
            }
103
0
            first += 2;
104
0
            break;
105
106
0
        case _UT('/'):
107
0
        case _UT('?'):
108
0
            break;
109
110
0
        default:
111
0
            return URI_FALSE;
112
0
        }
113
114
0
        first++;
115
0
    }
116
0
    return URI_TRUE;
117
0
}
Unexecuted instantiation: uriIsWellFormedFragmentA
Unexecuted instantiation: uriIsWellFormedFragmentW
118
119
int URI_FUNC(SetFragmentMm)(URI_TYPE(Uri) * uri, const URI_CHAR * first,
120
0
                            const URI_CHAR * afterLast, UriMemoryManager * memory) {
121
    /* Input validation (before making any changes) */
122
0
    if ((uri == NULL) || ((first == NULL) != (afterLast == NULL))) {
123
0
        return URI_ERROR_NULL;
124
0
    }
125
126
0
    URI_CHECK_MEMORY_MANAGER(memory); /* may return */
127
128
0
    if ((first != NULL)
129
0
        && (URI_FUNC(IsWellFormedFragment)(first, afterLast) == URI_FALSE)) {
130
0
        return URI_ERROR_SYNTAX;
131
0
    }
132
133
    /* Clear old value */
134
0
    if ((uri->owner == URI_TRUE) && (uri->fragment.first != uri->fragment.afterLast)) {
135
0
        memory->free(memory, (URI_CHAR *)uri->fragment.first);
136
0
    }
137
0
    uri->fragment.first = NULL;
138
0
    uri->fragment.afterLast = NULL;
139
140
    /* Already done? */
141
0
    if (first == NULL) {
142
0
        return URI_SUCCESS;
143
0
    }
144
145
0
    assert(first != NULL);
146
147
    /* Ensure owned */
148
0
    if (uri->owner == URI_FALSE) {
149
0
        const int res = URI_FUNC(MakeOwnerMm)(uri, memory);
150
0
        if (res != URI_SUCCESS) {
151
0
            return res;
152
0
        }
153
0
    }
154
155
0
    assert(uri->owner == URI_TRUE);
156
157
    /* Apply new value */
158
0
    URI_TYPE(TextRange) sourceRange;
159
0
    sourceRange.first = first;
160
0
    sourceRange.afterLast = afterLast;
161
162
0
    if (URI_FUNC(CopyRangeAsNeeded)(&uri->fragment, &sourceRange, memory) == URI_FALSE) {
163
0
        return URI_ERROR_MALLOC;
164
0
    }
165
166
0
    return URI_SUCCESS;
167
0
}
Unexecuted instantiation: uriSetFragmentMmA
Unexecuted instantiation: uriSetFragmentMmW
168
169
int URI_FUNC(SetFragment)(URI_TYPE(Uri) * uri, const URI_CHAR * first,
170
0
                          const URI_CHAR * afterLast) {
171
0
    return URI_FUNC(SetFragmentMm)(uri, first, afterLast, NULL);
172
0
}
Unexecuted instantiation: uriSetFragmentA
Unexecuted instantiation: uriSetFragmentW
173
174
#endif