Bug 1874684 - Part 6: Limit day length calculations to safe integers. r=mgaudet
[gecko.git] / dom / quota / ScopedLogExtraInfo.cpp
blob1c38c58f88522b891a5b967439b3f93835bb6751
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ScopedLogExtraInfo.h"
9 namespace mozilla::dom::quota {
11 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
12 MOZ_THREAD_LOCAL(const Tainted<nsCString>*)
13 ScopedLogExtraInfo::sQueryValueTainted;
14 MOZ_THREAD_LOCAL(const Tainted<nsCString>*)
15 ScopedLogExtraInfo::sContextValueTainted;
16 MOZ_THREAD_LOCAL(const Tainted<nsCString>*)
17 ScopedLogExtraInfo::sStorageOriginValueTainted;
19 /* static */
20 auto ScopedLogExtraInfo::FindSlot(const char* aTag) {
21 // XXX For now, don't use a real map but just allow the known tag values.
23 if (aTag == kTagQueryTainted) {
24 return &sQueryValueTainted;
27 if (aTag == kTagContextTainted) {
28 return &sContextValueTainted;
31 if (aTag == kTagStorageOriginTainted) {
32 return &sStorageOriginValueTainted;
35 MOZ_CRASH("Unknown tag!");
38 ScopedLogExtraInfo::~ScopedLogExtraInfo() {
39 if (mTag) {
40 MOZ_ASSERT(&mCurrentValue == FindSlot(mTag)->get(),
41 "Bad scoping of ScopedLogExtraInfo, must not be interleaved!");
43 FindSlot(mTag)->set(mPreviousValue);
47 ScopedLogExtraInfo::ScopedLogExtraInfo(ScopedLogExtraInfo&& aOther) noexcept
48 : mTag(aOther.mTag),
49 mPreviousValue(aOther.mPreviousValue),
50 mCurrentValue(std::move(aOther.mCurrentValue)) {
51 aOther.mTag = nullptr;
52 FindSlot(mTag)->set(&mCurrentValue);
55 /* static */ ScopedLogExtraInfo::ScopedLogExtraInfoMap
56 ScopedLogExtraInfo::GetExtraInfoMap() {
57 // This could be done in a cheaper way, but this is never called on a hot
58 // path, so we anticipate using a real map inside here to make use simpler for
59 // the caller(s).
61 ScopedLogExtraInfoMap map;
62 if (sQueryValueTainted.get()) {
63 map.emplace(kTagQueryTainted, sQueryValueTainted.get());
66 if (sContextValueTainted.get()) {
67 map.emplace(kTagContextTainted, sContextValueTainted.get());
70 if (sStorageOriginValueTainted.get()) {
71 map.emplace(kTagStorageOriginTainted, sStorageOriginValueTainted.get());
74 return map;
77 /* static */ void ScopedLogExtraInfo::Initialize() {
78 MOZ_ALWAYS_TRUE(sQueryValueTainted.init());
79 MOZ_ALWAYS_TRUE(sContextValueTainted.init());
80 MOZ_ALWAYS_TRUE(sStorageOriginValueTainted.init());
83 void ScopedLogExtraInfo::AddInfo() {
84 auto* slot = FindSlot(mTag);
85 MOZ_ASSERT(slot);
86 mPreviousValue = slot->get();
88 slot->set(&mCurrentValue);
90 #endif
92 } // namespace mozilla::dom::quota