Bug Summary

File:Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore/Modules/indexeddb/IDBIndex.cpp
Warning:line 422, column 15
Local variable 'context' is uncounted and unsafe

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple arm64-apple-ios26.2.0-simulator -O3 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name UnifiedSource51.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=osx -analyzer-checker=security.insecureAPI.decodeValueOfObjCType -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -target-sdk-version=26.2 -fdepfile-entry=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/SDKSettings.json -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -fno-modulemap-allow-subdirectory-search -fdefine-target-os-macros -enable-tlsdesc -target-cpu apple-m1 -target-feature +v8.4a -target-feature +aes -target-feature +altnzcv -target-feature +ccdp -target-feature +ccpp -target-feature +complxnum -target-feature +crc -target-feature +dotprod -target-feature +flagm -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +fptoint -target-feature +fullfp16 -target-feature +jsconv -target-feature +lse -target-feature +neon -target-feature +pauth -target-feature +perfmon -target-feature +predres -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sb -target-feature +sha2 -target-feature +sha3 -target-feature +specrestrict -target-feature +ssbs -target-abi darwinpcs -dwarf-ext-refs -fmodule-format=obj -debug-info-kind=standalone -dwarf-version=5 -debugger-tuning=lldb -fdebug-compilation-dir=/Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore -target-linker-version 1230.1 -fcoverage-compilation-dir=/Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore -resource-dir /Users/buildbot/Library/Developer/Toolchains/swift-webkit.xctoolchain/usr/lib/clang/21 -dependency-file /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/scan-build-output/StaticAnalyzer/WebCore/WebCore/normal/arm64/UnifiedSource51.d -skip-unused-modulemap-deps -MT dependencies -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk -iquote /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/WebCore-generated-files.hmap -iquote /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/WebCore-project-headers.hmap -isystem /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore/PAL/ThirdParty/dav1d/include -isystem /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/SDKAdditions/usr/include -iframework /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/SDKAdditions/System/Library/Frameworks -iframework /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitLibraries/SDKs/iphonesimulator26.0-additions.sdk/System/Library/PrivateFrameworks -iframework /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/System/Library/Frameworks -include-pch /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/PrecompiledHeaders/SharedPrecompiledHeaders/11721353671419897710/WebCorePrefix.h.gch -D _LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE -D NDEBUG -D BUILDING_WEBKIT -D GL_SILENCE_DEPRECATION=1 -D GLES_SILENCE_DEPRECATION=1 -D __clang_analyzer__ -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/WebCore-own-target-headers.hmap -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/WebCore-all-target-headers.hmap -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/include -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore/PAL -I /usr/include/libxslt -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/include/libxml2 -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/DerivedSources/WebCore -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include/WebKitAdditions -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include/WebKitAdditions -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitLibraries/SDKs/iphonesimulator26.0-additions.sdk/usr/local/include -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include/webrtc -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include/webrtc -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/usr/local/include/webm -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include/webm -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/PAL.build/PAL.build/DerivedSources -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/PAL.build/Release-iphonesimulator/PAL.build/DerivedSources -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/DerivedSources-normal/arm64 -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/DerivedSources/arm64 -I /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/WebCore.build/Release-iphonesimulator/WebCore.build/DerivedSources -F/Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator -F/Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator -I/usr/local/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/local/include -internal-isystem /Users/buildbot/Library/Developer/Toolchains/swift-webkit.xctoolchain/usr/lib/clang/21/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/usr/include -internal-iframework /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/System/Library/Frameworks -internal-iframework /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/System/Library/SubFrameworks -internal-iframework /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator26.2.sdk/Library/Frameworks -Wno-trigraphs -Werror -Wno-missing-field-initializers -Wmissing-prototypes -Wunreachable-code -Wnon-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion -Wnon-literal-null-conversion -Wobjc-literal-conversion -Wsign-compare -Wno-shorten-64-to-32 -Wnewline-eof -Wno-c++11-extensions -Wno-implicit-fallthrough -Wdeprecated-declarations -Winvalid-offsetof -Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing -Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -Wall -Wc99-designator -Wconditional-uninitialized -Wextra -Wdeprecated-enum-enum-conversion -Wdeprecated-enum-float-conversion -Wenum-float-conversion -Wfinal-dtor-non-final-class -Wformat=2 -Wmisleading-indentation -Wpointer-to-int-cast -Wreorder-init-list -Wundef -Wunused-but-set-variable -Wvla -Werror=unguarded-availability -Wno-elaborated-enum-base -Wcast-qual -Wchar-subscripts -Wextra-tokens -Winit-self -Wmissing-noreturn -Wpacked -Wpointer-arith -Wredundant-decls -Wwrite-strings -Wexit-time-destructors -Wglobal-constructors -Wtautological-compare -Wimplicit-fallthrough -Wvla -Wno-unknown-warning-option -Wliteral-conversion -Wthread-safety -Wno-profile-instr-out-of-date -Wno-profile-instr-unprofiled -Wno-backend-plugin -Wunsafe-buffer-usage -std=c++2b -fdeprecated-macro -ferror-limit 19 -fmacro-backtrace-limit=0 -fvisibility=hidden -fvisibility-inlines-hidden -stack-protector 1 -fblocks -fencode-extended-block-signature -fno-rtti -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fno-implicit-modules -fskip-odr-check-in-gmf -fpascal-strings -fmax-type-align=16 -fdiagnostics-show-note-include-stack -vectorize-loops -vectorize-slp -fsafe-buffer-usage-suggestions -analyzer-output=html -analyzer-config report-in-main-source-file=true -analyzer-config nullability:NoDiagnoseCallsToSystemHeaders=true -analyzer-checker optin.osx.cocoa.localizability.NonLocalizedStringChecker -analyzer-checker security.insecureAPI.UncheckedReturn -analyzer-checker security.insecureAPI.getpw -analyzer-checker security.insecureAPI.gets -analyzer-checker security.insecureAPI.mkstemp -analyzer-checker security.insecureAPI.mktemp -analyzer-disable-checker security.insecureAPI.rand -analyzer-disable-checker security.insecureAPI.strcpy -analyzer-checker security.insecureAPI.vfork -analyzer-disable-checker alpha,apiModeling,core,cplusplus,deadcode,debug,fuchsia,nullability,optin,osx,security,unix,webkit -analyzer-checker alpha.webkit.ForwardDeclChecker,alpha.webkit.MemoryUnsafeCastChecker,alpha.webkit.NoDeleteChecker,alpha.webkit.NoUncheckedPtrMemberChecker,alpha.webkit.NoUnretainedMemberChecker,alpha.webkit.RetainPtrCtorAdoptChecker,alpha.webkit.UncheckedCallArgsChecker,alpha.webkit.UncheckedLocalVarsChecker,alpha.webkit.UncountedCallArgsChecker,alpha.webkit.UncountedLocalVarsChecker,alpha.webkit.UnretainedCallArgsChecker,alpha.webkit.UnretainedLambdaCapturesChecker,alpha.webkit.UnretainedLocalVarsChecker,webkit.NoUncountedMemberChecker,webkit.RefCntblBaseVirtualDtor,webkit.UncountedLambdaCapturesChecker -analyzer-config max-nodes=10000000 -analyzer-config verbose-report-filename=true -fdwarf2-cfi-asm -o /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/scan-build-output/StaticAnalyzer/WebCore/WebCore/normal/arm64/UnifiedSource51.plist -x c++ /Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/WebKitBuild/Release-iphonesimulator/DerivedSources/WebCore/unified-sources/UnifiedSource51.cpp
1/*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "IDBIndex.h"
28
29#include "ContextDestructionObserverInlines.h"
30#include "ExceptionOr.h"
31#include "IDBBindingUtilities.h"
32#include "IDBCursor.h"
33#include "IDBCursorDirection.h"
34#include "IDBDatabase.h"
35#include "IDBGetAllOptions.h"
36#include "IDBKeyRangeData.h"
37#include "IDBObjectStore.h"
38#include "IDBRequest.h"
39#include "IDBTransaction.h"
40#include "JSIDBGetAllOptions.h"
41#include "JSIDBKeyRange.h"
42#include "Logging.h"
43#include "Settings.h"
44#include "WebCoreOpaqueRoot.h"
45#include <JavaScriptCore/HeapInlines.h>
46#include <wtf/TZoneMallocInlines.h>
47#include <wtf/text/MakeString.h>
48
49namespace WebCore {
50using namespace JSC;
51
52WTF_MAKE_TZONE_ALLOCATED_IMPL(IDBIndex)::bmalloc::api::HeapRef IDBIndex::s_heapRef; const TZoneSpecification
IDBIndex::s_heapSpec = { &IDBIndex::s_heapRef, TZoneSpecification
::encodeSize<IDBIndex>(), TZoneSpecification::encodeAlignment
<IDBIndex>(), TZoneSpecification::encodeCategory<IDBIndex
>(), ::bmalloc::api::compactAllocationMode<IDBIndex>
(), TZoneSpecification::encodeDescriptor<IDBIndex>(), }
; void* IDBIndex::operatorNewSlow(size_t size) { if constexpr
(::bmalloc::api::compactAllocationMode<IDBIndex>() == CompactAllocationMode
::Compact) return ::bmalloc::api::tzoneAllocateCompactSlow(size
, s_heapSpec); return ::bmalloc::api::tzoneAllocateNonCompactSlow
(size, s_heapSpec); } using __makeBtzoneMallocedInlineMacroSemicolonifier
__attribute__((unused)) = int
;
53
54UniqueRef<IDBIndex> IDBIndex::create(ScriptExecutionContext& context, const IDBIndexInfo& info, IDBObjectStore& objectStore)
55{
56 auto result = UniqueRef(*new IDBIndex(context, info, objectStore));
57 result->suspendIfNeeded();
58 return result;
59}
60
61IDBIndex::IDBIndex(ScriptExecutionContext& context, const IDBIndexInfo& info, IDBObjectStore& objectStore)
62 : ActiveDOMObject(&context)
63 , m_info(info)
64 , m_originalInfo(info)
65 , m_objectStore(objectStore)
66{
67 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
68}
69
70IDBIndex::~IDBIndex()
71{
72 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
73}
74
75bool IDBIndex::virtualHasPendingActivity() const
76{
77 return m_objectStore->hasPendingActivity();
78}
79
80const String& IDBIndex::name() const
81{
82 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
83 return m_info.name();
84}
85
86ExceptionOr<void> IDBIndex::setName(const String& name)
87{
88 Ref transaction = m_objectStore->transaction();
89 ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0);
90
91 if (m_deleted)
92 return Exception { ExceptionCode::InvalidStateError, "Failed set property 'name' on 'IDBIndex': The index has been deleted."_s };
93
94 if (m_objectStore->isDeleted())
95 return Exception { ExceptionCode::InvalidStateError, "Failed set property 'name' on 'IDBIndex': The index's object store has been deleted."_s };
96
97 if (!transaction->isVersionChange())
98 return Exception { ExceptionCode::InvalidStateError, "Failed set property 'name' on 'IDBIndex': The index's transaction is not a version change transaction."_s };
99
100 if (!transaction->isActive())
101 return Exception { ExceptionCode::TransactionInactiveError, "Failed set property 'name' on 'IDBIndex': The index's transaction is not active."_s };
102
103 if (m_info.name() == name)
104 return { };
105
106 if (m_objectStore->info().hasIndex(name))
107 return Exception { ExceptionCode::ConstraintError, makeString("Failed set property 'name' on 'IDBIndex': The owning object store already has an index named '"_s, name, "'."_s) };
108
109 transaction->database().renameIndex(*this, name);
110 m_info.rename(name);
111
112 return { };
113}
114
115IDBObjectStore& IDBIndex::objectStore()
116{
117 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
118 return m_objectStore;
119}
120
121const IDBKeyPath& IDBIndex::keyPath() const
122{
123 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
124 return m_info.keyPath();
125}
126
127bool IDBIndex::unique() const
128{
129 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
130 return m_info.unique();
131}
132
133bool IDBIndex::multiEntry() const
134{
135 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
136 return m_info.multiEntry();
137}
138
139void IDBIndex::rollbackInfoForVersionChangeAbort()
140{
141 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
142
143 // Only rollback to the original info if this index still exists in the rolled-back database info.
144 auto* objectStoreInfo = m_objectStore->transaction().database().info().infoForExistingObjectStore(m_objectStore->info().identifier());
145 if (!objectStoreInfo)
146 return;
147
148 if (!objectStoreInfo->hasIndex(m_info.identifier())) {
149 m_deleted = true;
150 return;
151 }
152
153 m_info = m_originalInfo;
154 m_deleted = false;
155}
156
157ExceptionOr<Ref<IDBRequest>> IDBIndex::doOpenCursor(IDBCursorDirection direction, Function<ExceptionOr<RefPtr<IDBKeyRange>>()>&& function)
158{
159 LOG(IndexedDB, "IDBIndex::openCursor")((void)0);
160 Ref transaction = m_objectStore->transaction();
161 ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0);
162
163 if (m_deleted || m_objectStore->isDeleted())
164 return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'openCursor' on 'IDBIndex': The index or its object store has been deleted."_s };
165
166 if (!transaction->isActive())
167 return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'openCursor' on 'IDBIndex': The transaction is inactive or finished."_s };
168
169 auto keyRange = function();
170 if (keyRange.hasException())
171 return keyRange.releaseException();
172
173 IDBKeyRangeData rangeData = keyRange.returnValue().get();
174 if (rangeData.lowerKey.isNull())
175 rangeData.lowerKey = IDBKeyData::minimum();
176 if (rangeData.upperKey.isNull())
177 rangeData.upperKey = IDBKeyData::maximum();
178
179 auto info = IDBCursorInfo::indexCursor(transaction, m_objectStore->info().identifier(), m_info.identifier(), rangeData, direction, IndexedDB::CursorType::KeyAndValue);
180 return transaction->requestOpenCursor(*this, info);
181}
182
183ExceptionOr<Ref<IDBRequest>> IDBIndex::openCursor(RefPtr<IDBKeyRange>&& range, IDBCursorDirection direction)
184{
185 return doOpenCursor(direction, [range = WTF::move(range)]() {
186 return range;
187 });
188}
189
190ExceptionOr<Ref<IDBRequest>> IDBIndex::openCursor(JSGlobalObject& execState, JSValue key, IDBCursorDirection direction)
191{
192 return doOpenCursor(direction, [state = &execState, key]() {
193 auto onlyResult = IDBKeyRange::only(*state, key);
194 if (onlyResult.hasException())
195 return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'openCursor' on 'IDBIndex': The parameter is not a valid key."_s) };
196
197 return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() };
198 });
199}
200
201ExceptionOr<Ref<IDBRequest>> IDBIndex::doOpenKeyCursor(IDBCursorDirection direction, Function<ExceptionOr<RefPtr<IDBKeyRange>>()>&& function)
202{
203 LOG(IndexedDB, "IDBIndex::openKeyCursor")((void)0);
204 Ref transaction = m_objectStore->transaction();
205 ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0);
206
207 if (m_deleted || m_objectStore->isDeleted())
208 return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'openKeyCursor' on 'IDBIndex': The index or its object store has been deleted."_s };
209
210 if (!transaction->isActive())
211 return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'openKeyCursor' on 'IDBIndex': The transaction is inactive or finished."_s };
212
213 auto keyRange = function();
214 if (keyRange.hasException())
215 return keyRange.releaseException();
216
217 RefPtr keyRangePointer = keyRange.returnValue().get();
218 auto info = IDBCursorInfo::indexCursor(transaction, m_objectStore->info().identifier(), m_info.identifier(), keyRangePointer.get(), direction, IndexedDB::CursorType::KeyOnly);
219 return transaction->requestOpenCursor(*this, info);
220}
221
222ExceptionOr<Ref<IDBRequest>> IDBIndex::openKeyCursor(RefPtr<IDBKeyRange>&& range, IDBCursorDirection direction)
223{
224 return doOpenKeyCursor(direction, [range = WTF::move(range)]() {
225 return range;
226 });
227}
228
229ExceptionOr<Ref<IDBRequest>> IDBIndex::openKeyCursor(JSGlobalObject& execState, JSValue key, IDBCursorDirection direction)
230{
231 return doOpenKeyCursor(direction, [state = &execState, key]() {
232 auto onlyResult = IDBKeyRange::only(*state, key);
233 if (onlyResult.hasException())
234 return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'openKeyCursor' on 'IDBIndex': The parameter is not a valid key."_s) };
235
236 return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() };
237 });
238}
239
240ExceptionOr<Ref<IDBRequest>> IDBIndex::count(IDBKeyRange* range)
241{
242 LOG(IndexedDB, "IDBIndex::count")((void)0);
243
244 return doCount(range ? IDBKeyRangeData(range) : IDBKeyRangeData::allKeys());
245}
246
247ExceptionOr<Ref<IDBRequest>> IDBIndex::count(JSGlobalObject& execState, JSValue key)
248{
249 LOG(IndexedDB, "IDBIndex::count")((void)0);
250
251 auto idbKey = scriptValueToIDBKey(execState, key);
252 RefPtr idbKeyPointer = idbKey->isValid() ? idbKey.ptr() : nullptr;
253
254 return doCount(IDBKeyRangeData(idbKeyPointer.get()));
255}
256
257ExceptionOr<Ref<IDBRequest>> IDBIndex::doCount(const IDBKeyRangeData& range)
258{
259 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
260
261 if (m_deleted || m_objectStore->isDeleted())
262 return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'count' on 'IDBIndex': The index or its object store has been deleted."_s };
263
264 Ref transaction = m_objectStore->transaction();
265 if (!transaction->isActive())
266 return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'count' on 'IDBIndex': The transaction is inactive or finished."_s };
267
268 if (!range.isValid())
269 return Exception { ExceptionCode::DataError, "Failed to execute 'count' on 'IDBIndex': The parameter is not a valid key."_s };
270
271 return transaction->requestCount(*this, range);
272}
273
274ExceptionOr<Ref<IDBRequest>> IDBIndex::get(IDBKeyRange* range)
275{
276 LOG(IndexedDB, "IDBIndex::get")((void)0);
277
278 return doGet(IDBKeyRangeData(range));
279}
280
281ExceptionOr<Ref<IDBRequest>> IDBIndex::get(JSGlobalObject& execState, JSValue key)
282{
283 LOG(IndexedDB, "IDBIndex::get")((void)0);
284
285 auto idbKey = scriptValueToIDBKey(execState, key);
286 if (!idbKey->isValid())
287 return doGet(Exception(ExceptionCode::DataError, "Failed to execute 'get' on 'IDBIndex': The parameter is not a valid key."_s));
288
289 return doGet(IDBKeyRangeData(idbKey.ptr()));
290}
291
292ExceptionOr<Ref<IDBRequest>> IDBIndex::doGet(ExceptionOr<IDBKeyRangeData> range)
293{
294 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
295
296 if (m_deleted || m_objectStore->isDeleted())
297 return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'get' on 'IDBIndex': The index or its object store has been deleted."_s };
298
299 Ref transaction = m_objectStore->transaction();
300 if (!transaction->isActive())
301 return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'get' on 'IDBIndex': The transaction is inactive or finished."_s };
302
303 if (range.hasException())
304 return range.releaseException();
305 auto keyRange = range.releaseReturnValue();
306
307 if (keyRange.isNull())
308 return Exception { ExceptionCode::DataError };
309
310 return transaction->requestGetValue(*this, keyRange);
311}
312
313ExceptionOr<Ref<IDBRequest>> IDBIndex::getKey(IDBKeyRange* range)
314{
315 LOG(IndexedDB, "IDBIndex::getKey")((void)0);
316
317 return doGetKey(IDBKeyRangeData(range));
318}
319
320ExceptionOr<Ref<IDBRequest>> IDBIndex::getKey(JSGlobalObject& execState, JSValue key)
321{
322 LOG(IndexedDB, "IDBIndex::getKey")((void)0);
323
324 auto idbKey = scriptValueToIDBKey(execState, key);
325 if (!idbKey->isValid())
326 return doGetKey(Exception(ExceptionCode::DataError, "Failed to execute 'getKey' on 'IDBIndex': The parameter is not a valid key."_s));
327
328 return doGetKey(IDBKeyRangeData(idbKey.ptr()));
329}
330
331ExceptionOr<Ref<IDBRequest>> IDBIndex::doGetKey(ExceptionOr<IDBKeyRangeData> range)
332{
333 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
334
335 if (m_deleted || m_objectStore->isDeleted())
336 return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'getKey' on 'IDBIndex': The index or its object store has been deleted."_s };
337
338 Ref transaction = m_objectStore->transaction();
339 if (!transaction->isActive())
340 return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'getKey' on 'IDBIndex': The transaction is inactive or finished."_s };
341
342 if (range.hasException())
343 return range.releaseException();
344 auto keyRange = range.releaseReturnValue();
345
346 if (keyRange.isNull())
347 return Exception { ExceptionCode::DataError };
348
349 return transaction->requestGetKey(*this, keyRange);
350}
351
352ExceptionOr<Ref<IDBRequest>> IDBIndex::doGetAll(std::optional<uint32_t> count, Function<ExceptionOr<RefPtr<IDBKeyRange>>()>&& function)
353{
354 LOG(IndexedDB, "IDBIndex::getAll")((void)0);
355 Ref transaction = m_objectStore->transaction();
356 ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0);
357
358 if (m_deleted || m_objectStore->isDeleted())
359 return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'getAll' on 'IDBIndex': The index or its object store has been deleted."_s };
360
361 if (!transaction->isActive())
362 return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'getAll' on 'IDBIndex': The transaction is inactive or finished."_s };
363
364 auto keyRange = function();
365 if (keyRange.hasException())
366 return keyRange.releaseException();
367
368 RefPtr keyRangePointer = keyRange.returnValue().get();
369 return transaction->requestGetAllIndexRecords(*this, keyRangePointer.get(), IndexedDB::GetAllType::Values, count, IDBCursorDirection::Next);
370}
371
372ExceptionOr<Ref<IDBRequest>> IDBIndex::getAll(RefPtr<IDBKeyRange>&& range, std::optional<uint32_t> count)
373{
374 return doGetAll(count, [range = WTF::move(range)]() {
375 return range;
376 });
377}
378
379ExceptionOr<Ref<IDBRequest>> IDBIndex::getAll(JSGlobalObject& execState, JSValue key, std::optional<uint32_t> count)
380{
381 return doGetAll(count, [state = &execState, key]() {
382 auto onlyResult = IDBKeyRange::only(*state, key);
383 if (onlyResult.hasException())
384 return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'getAll' on 'IDBIndex': The parameter is not a valid key."_s) };
385
386 return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() };
387 });
388}
389
390ExceptionOr<Ref<IDBRequest>> IDBIndex::doGetAllKeys(std::optional<uint32_t> count, IDBCursorDirection cursorDirection, Function<ExceptionOr<RefPtr<IDBKeyRange>>()>&& function)
391{
392 LOG(IndexedDB, "IDBIndex::getAllKeys")((void)0);
393 Ref transaction = m_objectStore->transaction();
394 ASSERT(canCurrentThreadAccessThreadLocalData(transaction->database().originThread()))((void)0);
395
396 if (m_deleted || m_objectStore->isDeleted())
397 return Exception { ExceptionCode::InvalidStateError, "Failed to execute 'getAllKeys' on 'IDBIndex': The index or its object store has been deleted."_s };
398
399 if (!transaction->isActive())
400 return Exception { ExceptionCode::TransactionInactiveError, "Failed to execute 'getAllKeys' on 'IDBIndex': The transaction is inactive or finished."_s };
401
402 auto keyRange = function();
403 if (keyRange.hasException())
404 return keyRange.releaseException();
405
406 RefPtr keyRangePointer = keyRange.returnValue().get();
407 return transaction->requestGetAllIndexRecords(*this, keyRangePointer.get(), IndexedDB::GetAllType::Keys, count, cursorDirection);
408}
409
410ExceptionOr<Ref<IDBRequest>> IDBIndex::getAllKeys(RefPtr<IDBKeyRange>&& range, std::optional<uint32_t> count)
411{
412 return doGetAllKeys(count, IDBCursorDirection::Next, [range = WTF::move(range)]() {
413 return range;
414 });
415}
416
417ExceptionOr<Ref<IDBRequest>> IDBIndex::getAllKeys(JSGlobalObject& execState, JSValue key, std::optional<uint32_t> count)
418{
419 // Spec §2.9: For objects, check "is a potentially valid key range" before
420 // attempting IDBGetAllOptions dictionary conversion.
421 if (key.isObject() && !key.isUndefinedOrNull()) {
422 auto* context = scriptExecutionContext();
Local variable 'context' is uncounted and unsafe
423 if (context && context->settingsValues().indexedDBGetAllRecordsEnabled) {
424 auto& vm = execState.vm();
425 auto throwScope = DECLARE_THROW_SCOPE(vm)JSC::ThrowScope((vm));
426 auto idbKey = scriptValueToIDBKey(execState, key);
427
428 // If key conversion threw (e.g., throwing getter), let it propagate.
429 // The binding's throwScope will see the pending JS exception.
430 if (throwScope.exception())
431 return Exception { ExceptionCode::DataError };
432
433 if (!idbKey->isValid()) {
434 // Not a valid key — treat as IDBGetAllOptions dictionary.
435 auto optionsResult = convertDictionary<IDBGetAllOptions>(execState, key);
436 if (throwScope.exception())
437 return Exception { ExceptionCode::DataError };
438 auto options = optionsResult.releaseReturnValue();
439 return doGetAllKeys(options.count, options.direction,
440 [state = &execState, query = WTF::move(options.query)]()
441 -> ExceptionOr<RefPtr<IDBKeyRange>> {
442 if (query.isUndefinedOrNull())
443 return { nullptr };
444 if (auto* keyRange = JSIDBKeyRange::toWrapped(state->vm(), query))
445 return { RefPtr<IDBKeyRange>(keyRange) };
446 auto onlyResult = IDBKeyRange::only(*state, query);
447 if (onlyResult.hasException())
448 return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'getAllKeys' on 'IDBIndex': The parameter is not a valid key."_s) };
449 return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() };
450 });
451 }
452 // else: valid key — fall through to normal key path
453 }
454 }
455
456 // Normal key conversion path.
457 return doGetAllKeys(count, IDBCursorDirection::Next, [state = &execState, key]() {
458 auto onlyResult = IDBKeyRange::only(*state, key);
459 if (onlyResult.hasException())
460 return ExceptionOr<RefPtr<IDBKeyRange>> { Exception(ExceptionCode::DataError, "Failed to execute 'getAllKeys' on 'IDBIndex': The parameter is not a valid key."_s) };
461
462 return ExceptionOr<RefPtr<IDBKeyRange>> { onlyResult.releaseReturnValue() };
463 });
464}
465
466ExceptionOr<Ref<IDBRequest>> IDBIndex::getAllRecords(JSGlobalObject&, IDBGetAllOptions&&)
467{
468 return Exception { ExceptionCode::NotSupportedError, "getAllRecords is not yet supported"_s };
469}
470
471void IDBIndex::markAsDeleted()
472{
473 ASSERT(canCurrentThreadAccessThreadLocalData(m_objectStore->transaction().database().originThread()))((void)0);
474
475 ASSERT(!m_deleted)((void)0);
476 m_deleted = true;
477}
478
479void IDBIndex::ref() const
480{
481 m_objectStore->ref();
482}
483
484void IDBIndex::deref() const
485{
486 m_objectStore->deref();
487}
488
489WebCoreOpaqueRoot IDBIndex::opaqueRoot()
490{
491 return WebCoreOpaqueRoot { m_objectStore.ptr() };
492}
493
494WebCoreOpaqueRoot root(IDBIndex* index)
495{
496 return index ? index->opaqueRoot() : nullptr;
497}
498
499} // namespace WebCore