File: | var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp |
Warning: | line 505, column 3 Value stored to 'result' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | |
7 | #include "Location.h" |
8 | #include "nsIScriptObjectPrincipal.h" |
9 | #include "nsIScriptContext.h" |
10 | #include "nsDocShellLoadState.h" |
11 | #include "nsIWebNavigation.h" |
12 | #include "nsIOService.h" |
13 | #include "nsIURL.h" |
14 | #include "nsIJARURI.h" |
15 | #include "nsIURIMutator.h" |
16 | #include "nsNetUtil.h" |
17 | #include "nsCOMPtr.h" |
18 | #include "nsEscape.h" |
19 | #include "nsPresContext.h" |
20 | #include "nsError.h" |
21 | #include "nsReadableUtils.h" |
22 | #include "nsJSUtils.h" |
23 | #include "nsContentUtils.h" |
24 | #include "nsDocShell.h" |
25 | #include "nsGlobalWindowOuter.h" |
26 | #include "nsPIDOMWindowInlines.h" |
27 | #include "mozilla/Likely.h" |
28 | #include "nsCycleCollectionParticipant.h" |
29 | #include "mozilla/BasePrincipal.h" |
30 | #include "mozilla/Components.h" |
31 | #include "mozilla/NullPrincipal.h" |
32 | #include "mozilla/ServoStyleConsts.h" |
33 | #include "mozilla/StaticPrefs_dom.h" |
34 | #include "mozilla/Unused.h" |
35 | #include "mozilla/dom/Document.h" |
36 | #include "mozilla/dom/DocumentInlines.h" |
37 | #include "mozilla/dom/FragmentDirective.h" |
38 | #include "mozilla/dom/LocationBinding.h" |
39 | #include "mozilla/dom/ScriptSettings.h" |
40 | #include "ReferrerInfo.h" |
41 | |
42 | namespace mozilla::dom { |
43 | |
44 | Location::Location(nsPIDOMWindowInner* aWindow) |
45 | : mCachedHash(VoidString()), mInnerWindow(aWindow) { |
46 | BrowsingContext* bc = GetBrowsingContext(); |
47 | if (bc) { |
48 | bc->LocationCreated(this); |
49 | } |
50 | } |
51 | |
52 | Location::~Location() { |
53 | if (isInList()) { |
54 | remove(); |
55 | } |
56 | } |
57 | |
58 | // QueryInterface implementation for Location |
59 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Location)nsresult Location::QueryInterface(const nsIID& aIID, void ** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak( NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!" , "aInstancePtr", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 59); MOZ_PretendNoReturn(); } } while (0); nsISupports* foundInterface ; if (TopThreeWordsEquals( aIID, (nsXPCOMCycleCollectionParticipant ::COMTypeInfo<nsXPCOMCycleCollectionParticipant, void>:: kIID), (nsCycleCollectionISupports::COMTypeInfo<nsCycleCollectionISupports , void>::kIID)) && (LowWordEquals(aIID, (nsXPCOMCycleCollectionParticipant ::COMTypeInfo<nsXPCOMCycleCollectionParticipant, void>:: kIID)) || LowWordEquals(aIID, (nsCycleCollectionISupports::COMTypeInfo <nsCycleCollectionISupports, void>::kIID)))) { if (LowWordEquals (aIID, (nsXPCOMCycleCollectionParticipant::COMTypeInfo<nsXPCOMCycleCollectionParticipant , void>::kIID))) { *aInstancePtr = Location::cycleCollection ::GetParticipant(); return NS_OK; } if (LowWordEquals(aIID, ( nsCycleCollectionISupports::COMTypeInfo<nsCycleCollectionISupports , void>::kIID))) { *aInstancePtr = Location::cycleCollection ::Upcast(this); return NS_OK; } foundInterface = nullptr; } else |
60 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRYif (aIID.Equals((nsWrapperCache::COMTypeInfo<nsWrapperCache , void>::kIID))) { *aInstancePtr = static_cast<nsWrapperCache *>(this); return NS_OK; } else |
61 | NS_INTERFACE_MAP_ENTRY(nsISupports)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t <decltype(*this)>, nsISupports>)) foundInterface = static_cast <nsISupports*>(this); else |
62 | NS_INTERFACE_MAP_ENDfoundInterface = 0; nsresult status; if (!foundInterface) { do { static_assert( mozilla::detail::AssertionConditionType< decltype(!aIID.Equals((nsISupports::COMTypeInfo<nsISupports , void>::kIID)))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!aIID.Equals((nsISupports::COMTypeInfo <nsISupports, void>::kIID))))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))" , "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 62); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))" ")"); do { *((volatile int*)__null) = 62; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); status = NS_NOINTERFACE ; } else { (foundInterface)->AddRef(); status = NS_OK; } * aInstancePtr = foundInterface; return status; } |
63 | |
64 | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Location, mInnerWindow)static_assert(std::is_base_of<nsWrapperCache, Location> ::value, "Class should inherit nsWrapperCache"); Location::cycleCollection Location::_cycleCollectorGlobal( nsCycleCollectionParticipant ::FlagMaybeSingleZoneJSHolder); void Location::cycleCollection ::Trace( void* p, const TraceCallbacks& aCallbacks, void* aClosure) { Location* tmp = DowncastCCParticipant<Location >(p); TraceWrapper(p, aCallbacks, aClosure); (void)tmp; } void Location::cycleCollection::TraceWrapper( void* p, const TraceCallbacks & aCallbacks, void* aClosure) { Location* tmp = DowncastCCParticipant <Location>(p); tmp->TraceWrapper(aCallbacks, aClosure ); } void Location::cycleCollection::Unlink(void* p) { Location * tmp = DowncastCCParticipant<Location>(p); ImplCycleCollectionUnlink (tmp->mInnerWindow); tmp->ReleaseWrapper(p); (void)tmp; } nsresult Location::cycleCollection::TraverseNative( void* p , nsCycleCollectionTraversalCallback& cb) { Location* tmp = DowncastCCParticipant<Location>(p); cb.DescribeRefCountedNode (tmp->mRefCnt.get(), "Location"); ImplCycleCollectionTraverse (cb, tmp->mInnerWindow, "mInnerWindow", 0); (void)tmp; return NS_OK; } |
65 | |
66 | NS_IMPL_CYCLE_COLLECTING_ADDREF(Location)MozExternalRefCountType Location::AddRef(void) { static_assert (!std::is_destructible_v<Location>, "Reference-counted class " "Location" " should not have a public destructor. " "Make this class's destructor non-public" ); do { static_assert( mozilla::detail::AssertionConditionType <decltype(int32_t(mRefCnt) >= 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) >= 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) >= 0" " (" "illegal refcnt" ")", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 66); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) >= 0" ") (" "illegal refcnt" ")"); do { *((volatile int*)__null) = 66; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); _mOwningThread.AssertOwnership("Location" " not thread-safe" ); nsISupports* base = Location::cycleCollection::Upcast(this ); nsrefcnt count = mRefCnt.incr(base); NS_LogAddRef((this), ( count), ("Location"), (uint32_t)(sizeof(*this))); return count ; } |
67 | NS_IMPL_CYCLE_COLLECTING_RELEASE(Location)MozExternalRefCountType Location::Release(void) { do { static_assert ( mozilla::detail::AssertionConditionType<decltype(int32_t (mRefCnt) > 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) > 0))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) > 0" " (" "dup release" ")", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 67); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) > 0" ") (" "dup release" ")"); do { *((volatile int*)__null) = 67 ; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); _mOwningThread.AssertOwnership("Location" " not thread-safe" ); nsISupports* base = Location::cycleCollection::Upcast(this ); nsrefcnt count = mRefCnt.decr(base); NS_LogRelease((this), (count), ("Location")); return count; } void Location::DeleteCycleCollectable (void) { delete (this); } |
68 | |
69 | BrowsingContext* Location::GetBrowsingContext() { |
70 | return mInnerWindow ? mInnerWindow->GetBrowsingContext() : nullptr; |
71 | } |
72 | |
73 | nsIDocShell* Location::GetDocShell() { |
74 | if (BrowsingContext* bc = GetBrowsingContext()) { |
75 | return bc->GetDocShell(); |
76 | } |
77 | return nullptr; |
78 | } |
79 | |
80 | nsresult Location::GetURI(nsIURI** aURI, bool aGetInnermostURI) { |
81 | *aURI = nullptr; |
82 | |
83 | nsIDocShell* docShell = GetDocShell(); |
84 | if (!docShell) { |
85 | return NS_OK; |
86 | } |
87 | |
88 | nsIWebNavigation* webNav = nsDocShell::Cast(docShell); |
89 | |
90 | nsCOMPtr<nsIURI> uri; |
91 | nsresult rv = webNav->GetCurrentURI(getter_AddRefs(uri)); |
92 | NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl (__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName (__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with " "result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t >(__rv), name ? " (" : "", name ? name : "", name ? ")" : "" ); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 92); return rv; } } while (false); |
93 | |
94 | // It is valid for docshell to return a null URI. Don't try to fixup |
95 | // if this happens. |
96 | if (!uri) { |
97 | return NS_OK; |
98 | } |
99 | |
100 | if (aGetInnermostURI) { |
101 | nsCOMPtr<nsIJARURI> jarURI(do_QueryInterface(uri)); |
102 | while (jarURI) { |
103 | jarURI->GetJARFile(getter_AddRefs(uri)); |
104 | jarURI = do_QueryInterface(uri); |
105 | } |
106 | } |
107 | |
108 | NS_ASSERTION(uri, "nsJARURI screwed up?")do { if (!(uri)) { NS_DebugBreak(NS_DEBUG_ASSERTION, "nsJARURI screwed up?" , "uri", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 108); MOZ_PretendNoReturn(); } } while (0); |
109 | |
110 | // Remove the fragment directive from the url hash. |
111 | FragmentDirective::ParseAndRemoveFragmentDirectiveFromFragment(uri); |
112 | nsCOMPtr<nsIURI> exposableURI = net::nsIOService::CreateExposableURI(uri); |
113 | exposableURI.forget(aURI); |
114 | return NS_OK; |
115 | } |
116 | |
117 | void Location::GetHash(nsAString& aHash, nsIPrincipal& aSubjectPrincipal, |
118 | ErrorResult& aRv) { |
119 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
120 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
121 | return; |
122 | } |
123 | |
124 | if (!mCachedHash.IsVoid()) { |
125 | aHash = mCachedHash; |
126 | return; |
127 | } |
128 | |
129 | aHash.SetLength(0); |
130 | |
131 | nsCOMPtr<nsIURI> uri; |
132 | aRv = GetURI(getter_AddRefs(uri)); |
133 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 133) || !uri) { |
134 | return; |
135 | } |
136 | |
137 | nsAutoCString ref; |
138 | |
139 | aRv = uri->GetRef(ref); |
140 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 140)) { |
141 | return; |
142 | } |
143 | |
144 | if (!ref.IsEmpty()) { |
145 | aHash.Assign(char16_t('#')); |
146 | AppendUTF8toUTF16(ref, aHash); |
147 | } |
148 | |
149 | mCachedHash = aHash; |
150 | } |
151 | |
152 | void Location::SetHash(const nsAString& aHash, nsIPrincipal& aSubjectPrincipal, |
153 | ErrorResult& aRv) { |
154 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
155 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
156 | return; |
157 | } |
158 | |
159 | NS_ConvertUTF16toUTF8 hash(aHash); |
160 | if (hash.IsEmpty() || hash.First() != char16_t('#')) { |
161 | hash.Insert(char16_t('#'), 0); |
162 | } |
163 | |
164 | nsCOMPtr<nsIURI> uri; |
165 | aRv = GetURI(getter_AddRefs(uri)); |
166 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 166) || !uri) { |
167 | return; |
168 | } |
169 | |
170 | aRv = NS_MutateURI(uri).SetRef(hash).Finalize(uri); |
171 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 171) || !uri) { |
172 | return; |
173 | } |
174 | |
175 | SetURI(uri, aSubjectPrincipal, aRv); |
176 | } |
177 | |
178 | void Location::GetHost(nsAString& aHost, nsIPrincipal& aSubjectPrincipal, |
179 | ErrorResult& aRv) { |
180 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
181 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
182 | return; |
183 | } |
184 | |
185 | aHost.Truncate(); |
186 | |
187 | nsCOMPtr<nsIURI> uri; |
188 | nsresult result; |
189 | |
190 | result = GetURI(getter_AddRefs(uri), true); |
191 | |
192 | if (uri) { |
193 | nsAutoCString hostport; |
194 | |
195 | result = uri->GetHostPort(hostport); |
196 | |
197 | if (NS_SUCCEEDED(result)((bool)(__builtin_expect(!!(!NS_FAILED_impl(result)), 1)))) { |
198 | AppendUTF8toUTF16(hostport, aHost); |
199 | } |
200 | } |
201 | } |
202 | |
203 | void Location::SetHost(const nsAString& aHost, nsIPrincipal& aSubjectPrincipal, |
204 | ErrorResult& aRv) { |
205 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
206 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
207 | return; |
208 | } |
209 | |
210 | nsCOMPtr<nsIURI> uri; |
211 | aRv = GetURI(getter_AddRefs(uri)); |
212 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 212) || !uri) { |
213 | return; |
214 | } |
215 | |
216 | aRv = |
217 | NS_MutateURI(uri).SetHostPort(NS_ConvertUTF16toUTF8(aHost)).Finalize(uri); |
218 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 218)) { |
219 | return; |
220 | } |
221 | |
222 | SetURI(uri, aSubjectPrincipal, aRv); |
223 | } |
224 | |
225 | void Location::GetHostname(nsAString& aHostname, |
226 | nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { |
227 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
228 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
229 | return; |
230 | } |
231 | |
232 | aHostname.Truncate(); |
233 | |
234 | nsCOMPtr<nsIURI> uri; |
235 | GetURI(getter_AddRefs(uri), true); |
236 | if (uri) { |
237 | nsContentUtils::GetHostOrIPv6WithBrackets(uri, aHostname); |
238 | } |
239 | } |
240 | |
241 | void Location::SetHostname(const nsAString& aHostname, |
242 | nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { |
243 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
244 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
245 | return; |
246 | } |
247 | |
248 | nsCOMPtr<nsIURI> uri; |
249 | aRv = GetURI(getter_AddRefs(uri)); |
250 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 250) || !uri) { |
251 | return; |
252 | } |
253 | |
254 | aRv = |
255 | NS_MutateURI(uri).SetHost(NS_ConvertUTF16toUTF8(aHostname)).Finalize(uri); |
256 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 256)) { |
257 | return; |
258 | } |
259 | |
260 | SetURI(uri, aSubjectPrincipal, aRv); |
261 | } |
262 | |
263 | nsresult Location::GetHref(nsAString& aHref) { |
264 | aHref.Truncate(); |
265 | |
266 | nsCOMPtr<nsIURI> uri; |
267 | nsresult rv = GetURI(getter_AddRefs(uri)); |
268 | if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv )), 0))), "NS_FAILED(rv)", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 268) || !uri) { |
269 | return rv; |
270 | } |
271 | |
272 | nsAutoCString uriString; |
273 | rv = uri->GetSpec(uriString); |
274 | if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv )), 0))), "NS_FAILED(rv)", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 274)) { |
275 | return rv; |
276 | } |
277 | |
278 | AppendUTF8toUTF16(uriString, aHref); |
279 | return NS_OK; |
280 | } |
281 | |
282 | void Location::GetOrigin(nsAString& aOrigin, nsIPrincipal& aSubjectPrincipal, |
283 | ErrorResult& aRv) { |
284 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
285 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
286 | return; |
287 | } |
288 | |
289 | aOrigin.Truncate(); |
290 | |
291 | nsCOMPtr<nsIURI> uri; |
292 | aRv = GetURI(getter_AddRefs(uri), true); |
293 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 293) || !uri) { |
294 | return; |
295 | } |
296 | |
297 | nsAutoString origin; |
298 | aRv = nsContentUtils::GetWebExposedOriginSerialization(uri, origin); |
299 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 299)) { |
300 | return; |
301 | } |
302 | |
303 | aOrigin = origin; |
304 | } |
305 | |
306 | void Location::GetPathname(nsAString& aPathname, |
307 | nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { |
308 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
309 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
310 | return; |
311 | } |
312 | |
313 | aPathname.Truncate(); |
314 | |
315 | nsCOMPtr<nsIURI> uri; |
316 | aRv = GetURI(getter_AddRefs(uri)); |
317 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 317) || !uri) { |
318 | return; |
319 | } |
320 | |
321 | nsAutoCString file; |
322 | |
323 | aRv = uri->GetFilePath(file); |
324 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 324)) { |
325 | return; |
326 | } |
327 | |
328 | AppendUTF8toUTF16(file, aPathname); |
329 | } |
330 | |
331 | void Location::SetPathname(const nsAString& aPathname, |
332 | nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { |
333 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
334 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
335 | return; |
336 | } |
337 | |
338 | nsCOMPtr<nsIURI> uri; |
339 | aRv = GetURI(getter_AddRefs(uri)); |
340 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 340) || !uri) { |
341 | return; |
342 | } |
343 | |
344 | nsresult rv = NS_MutateURI(uri) |
345 | .SetFilePath(NS_ConvertUTF16toUTF8(aPathname)) |
346 | .Finalize(uri); |
347 | if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) { |
348 | return; |
349 | } |
350 | |
351 | SetURI(uri, aSubjectPrincipal, aRv); |
352 | } |
353 | |
354 | void Location::GetPort(nsAString& aPort, nsIPrincipal& aSubjectPrincipal, |
355 | ErrorResult& aRv) { |
356 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
357 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
358 | return; |
359 | } |
360 | |
361 | aPort.SetLength(0); |
362 | |
363 | nsCOMPtr<nsIURI> uri; |
364 | aRv = GetURI(getter_AddRefs(uri), true); |
365 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 365) || !uri) { |
366 | return; |
367 | } |
368 | |
369 | int32_t port; |
370 | nsresult result = uri->GetPort(&port); |
371 | |
372 | // Don't propagate this exception to caller |
373 | if (NS_SUCCEEDED(result)((bool)(__builtin_expect(!!(!NS_FAILED_impl(result)), 1))) && -1 != port) { |
374 | nsAutoString portStr; |
375 | portStr.AppendInt(port); |
376 | aPort.Append(portStr); |
377 | } |
378 | } |
379 | |
380 | void Location::SetPort(const nsAString& aPort, nsIPrincipal& aSubjectPrincipal, |
381 | ErrorResult& aRv) { |
382 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
383 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
384 | return; |
385 | } |
386 | |
387 | nsCOMPtr<nsIURI> uri; |
388 | aRv = GetURI(getter_AddRefs(uri)); |
389 | if (NS_WARN_IF(aRv.Failed() || !uri)NS_warn_if_impl(aRv.Failed() || !uri, "aRv.Failed() || !uri", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 389)) { |
390 | return; |
391 | } |
392 | |
393 | // perhaps use nsReadingIterators at some point? |
394 | NS_ConvertUTF16toUTF8 portStr(aPort); |
395 | const char* buf = portStr.get(); |
396 | int32_t port = -1; |
397 | |
398 | if (!portStr.IsEmpty() && buf) { |
399 | if (*buf == ':') { |
400 | port = atol(buf + 1); |
401 | } else { |
402 | port = atol(buf); |
403 | } |
404 | } |
405 | |
406 | aRv = NS_MutateURI(uri).SetPort(port).Finalize(uri); |
407 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 407)) { |
408 | return; |
409 | } |
410 | |
411 | SetURI(uri, aSubjectPrincipal, aRv); |
412 | } |
413 | |
414 | void Location::GetProtocol(nsAString& aProtocol, |
415 | nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { |
416 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
417 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
418 | return; |
419 | } |
420 | |
421 | aProtocol.SetLength(0); |
422 | |
423 | nsCOMPtr<nsIURI> uri; |
424 | aRv = GetURI(getter_AddRefs(uri)); |
425 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 425) || !uri) { |
426 | return; |
427 | } |
428 | |
429 | nsAutoCString protocol; |
430 | |
431 | aRv = uri->GetScheme(protocol); |
432 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 432)) { |
433 | return; |
434 | } |
435 | |
436 | CopyASCIItoUTF16(protocol, aProtocol); |
437 | aProtocol.Append(char16_t(':')); |
438 | } |
439 | |
440 | void Location::SetProtocol(const nsAString& aProtocol, |
441 | nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { |
442 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
443 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
444 | return; |
445 | } |
446 | |
447 | nsCOMPtr<nsIURI> uri; |
448 | aRv = GetURI(getter_AddRefs(uri)); |
449 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 449) || !uri) { |
450 | return; |
451 | } |
452 | |
453 | nsAString::const_iterator start, end; |
454 | aProtocol.BeginReading(start); |
455 | aProtocol.EndReading(end); |
456 | nsAString::const_iterator iter(start); |
457 | Unused << FindCharInReadable(':', iter, end); |
458 | |
459 | nsresult rv = NS_MutateURI(uri) |
460 | .SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter))) |
461 | .Finalize(uri); |
462 | if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv )), 0))), "NS_FAILED(rv)", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 462)) { |
463 | // Oh, I wish nsStandardURL returned NS_ERROR_MALFORMED_URI for _all_ the |
464 | // malformed cases, not just some of them! |
465 | aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); |
466 | return; |
467 | } |
468 | |
469 | nsAutoCString newSpec; |
470 | aRv = uri->GetSpec(newSpec); |
471 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 471)) { |
472 | return; |
473 | } |
474 | // We may want a new URI class for the new URI, so recreate it: |
475 | rv = NS_NewURI(getter_AddRefs(uri), newSpec); |
476 | if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) { |
477 | if (rv == NS_ERROR_MALFORMED_URI) { |
478 | rv = NS_ERROR_DOM_SYNTAX_ERR; |
479 | } |
480 | |
481 | aRv.Throw(rv); |
482 | return; |
483 | } |
484 | |
485 | if (!uri->SchemeIs("http") && !uri->SchemeIs("https")) { |
486 | // No-op, per spec. |
487 | return; |
488 | } |
489 | |
490 | SetURI(uri, aSubjectPrincipal, aRv); |
491 | } |
492 | |
493 | void Location::GetSearch(nsAString& aSearch, nsIPrincipal& aSubjectPrincipal, |
494 | ErrorResult& aRv) { |
495 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
496 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
497 | return; |
498 | } |
499 | |
500 | aSearch.SetLength(0); |
501 | |
502 | nsCOMPtr<nsIURI> uri; |
503 | nsresult result = NS_OK; |
504 | |
505 | result = GetURI(getter_AddRefs(uri)); |
Value stored to 'result' is never read | |
506 | |
507 | nsCOMPtr<nsIURL> url(do_QueryInterface(uri)); |
508 | |
509 | if (url) { |
510 | nsAutoCString search; |
511 | |
512 | result = url->GetQuery(search); |
513 | |
514 | if (NS_SUCCEEDED(result)((bool)(__builtin_expect(!!(!NS_FAILED_impl(result)), 1))) && !search.IsEmpty()) { |
515 | aSearch.Assign(char16_t('?')); |
516 | AppendUTF8toUTF16(search, aSearch); |
517 | } |
518 | } |
519 | } |
520 | |
521 | void Location::SetSearch(const nsAString& aSearch, |
522 | nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) { |
523 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
524 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
525 | return; |
526 | } |
527 | |
528 | nsCOMPtr<nsIURI> uri; |
529 | aRv = GetURI(getter_AddRefs(uri)); |
530 | nsCOMPtr<nsIURL> url(do_QueryInterface(uri)); |
531 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 531) || !url) { |
532 | return; |
533 | } |
534 | |
535 | aRv = |
536 | NS_MutateURI(uri).SetQuery(NS_ConvertUTF16toUTF8(aSearch)).Finalize(uri); |
537 | if (NS_WARN_IF(aRv.Failed())NS_warn_if_impl(aRv.Failed(), "aRv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 537)) { |
538 | return; |
539 | } |
540 | |
541 | SetURI(uri, aSubjectPrincipal, aRv); |
542 | } |
543 | |
544 | void Location::Reload(bool aForceget, nsIPrincipal& aSubjectPrincipal, |
545 | ErrorResult& aRv) { |
546 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
547 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
548 | return; |
549 | } |
550 | |
551 | RefPtr<nsDocShell> docShell(nsDocShell::Cast(GetDocShell())); |
552 | if (!docShell) { |
553 | return aRv.Throw(NS_ERROR_FAILURE); |
554 | } |
555 | |
556 | RefPtr<BrowsingContext> bc = GetBrowsingContext(); |
557 | if (!bc || bc->IsDiscarded()) { |
558 | return; |
559 | } |
560 | |
561 | CallerType callerType = aSubjectPrincipal.IsSystemPrincipal() |
562 | ? CallerType::System |
563 | : CallerType::NonSystem; |
564 | |
565 | nsresult rv = bc->CheckNavigationRateLimit(callerType); |
566 | if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) { |
567 | aRv.Throw(rv); |
568 | return; |
569 | } |
570 | |
571 | uint32_t reloadFlags = nsIWebNavigation::LOAD_FLAGS_NONE; |
572 | |
573 | if (aForceget) { |
574 | reloadFlags = nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | |
575 | nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY; |
576 | } |
577 | |
578 | rv = docShell->Reload(reloadFlags); |
579 | if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) && rv != NS_BINDING_ABORTED) { |
580 | // NS_BINDING_ABORTED is returned when we attempt to reload a POST result |
581 | // and the user says no at the "do you want to reload?" prompt. Don't |
582 | // propagate this one back to callers. |
583 | return aRv.Throw(rv); |
584 | } |
585 | } |
586 | |
587 | void Location::Assign(const nsAString& aUrl, nsIPrincipal& aSubjectPrincipal, |
588 | ErrorResult& aRv) { |
589 | if (!CallerSubsumes(&aSubjectPrincipal)) { |
590 | aRv.Throw(NS_ERROR_DOM_SECURITY_ERR); |
591 | return; |
592 | } |
593 | |
594 | DoSetHref(aUrl, aSubjectPrincipal, false, aRv); |
595 | } |
596 | |
597 | bool Location::CallerSubsumes(nsIPrincipal* aSubjectPrincipal) { |
598 | MOZ_ASSERT(aSubjectPrincipal)do { static_assert( mozilla::detail::AssertionConditionType< decltype(aSubjectPrincipal)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aSubjectPrincipal))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aSubjectPrincipal" , "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 598); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aSubjectPrincipal" ")"); do { *((volatile int*)__null) = 598; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
599 | |
600 | BrowsingContext* bc = GetBrowsingContext(); |
601 | if (MOZ_UNLIKELY(!bc)(__builtin_expect(!!(!bc), 0)) || MOZ_UNLIKELY(bc->IsDiscarded())(__builtin_expect(!!(bc->IsDiscarded()), 0))) { |
602 | // Per spec, operations on a Location object with a discarded BC are no-ops, |
603 | // not security errors, so we need to return true from the access check and |
604 | // let the caller do its own discarded docShell check. |
605 | return true; |
606 | } |
607 | if (MOZ_UNLIKELY(!bc->IsInProcess())(__builtin_expect(!!(!bc->IsInProcess()), 0))) { |
608 | return false; |
609 | } |
610 | |
611 | // Get the principal associated with the location object. Note that this is |
612 | // the principal of the page which will actually be navigated, not the |
613 | // principal of the Location object itself. This is why we need this check |
614 | // even though we only allow limited cross-origin access to Location objects |
615 | // in general. |
616 | nsPIDOMWindowOuter* outer = bc->GetDOMWindow(); |
617 | MOZ_DIAGNOSTIC_ASSERT(outer)do { static_assert( mozilla::detail::AssertionConditionType< decltype(outer)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(outer))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("outer", "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 617); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "outer" ")"); do { *((volatile int*)__null) = 617; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
618 | if (MOZ_UNLIKELY(!outer)(__builtin_expect(!!(!outer), 0))) return false; |
619 | |
620 | nsIScriptObjectPrincipal* sop = nsGlobalWindowOuter::Cast(outer); |
621 | bool subsumes = false; |
622 | nsresult rv = aSubjectPrincipal->SubsumesConsideringDomain( |
623 | sop->GetPrincipal(), &subsumes); |
624 | NS_ENSURE_SUCCESS(rv, false)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl (__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName (__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with " "result 0x%" "X" "%s%s%s", "rv", "false", static_cast<uint32_t >(__rv), name ? " (" : "", name ? name : "", name ? ")" : "" ); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/dom/base/Location.cpp" , 624); return false; } } while (false); |
625 | return subsumes; |
626 | } |
627 | |
628 | JSObject* Location::WrapObject(JSContext* aCx, |
629 | JS::Handle<JSObject*> aGivenProto) { |
630 | return Location_Binding::Wrap(aCx, this, aGivenProto); |
631 | } |
632 | |
633 | void Location::ClearCachedValues() { mCachedHash = VoidString(); } |
634 | |
635 | } // namespace mozilla::dom |