• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / 1816

04 Nov 2023 12:29AM UTC coverage: 91.648% (-0.01%) from 91.66%
1816

push

Evergreen

web-flow
Use a single write transaction for DiscardLocal client resets on FLX realms (#7110)

Updating the subscription store in a separate write transaction from the
recovery means that we temporarily commit an invalid state. If the application
crashes between committing the client reset diff and updating the subscription
store, the next launch of the application would try to use the now-invalid
pending subscriptions that should have been discarded.

92128 of 168844 branches covered (0.0%)

141 of 146 new or added lines in 7 files covered. (96.58%)

84 existing lines in 15 files now uncovered.

230681 of 251702 relevant lines covered (91.65%)

6383138.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.06
/src/realm/uuid.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2020 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include <realm/uuid.hpp>
20
#include <realm/string_data.hpp>
21
#include <realm/util/assert.hpp>
22
#include <realm/util/base64.hpp>
23

24
#include <atomic>
25
#include <cctype>
26

27
namespace {
28
constexpr char hex_digits[] = "0123456789abcdef";
29
constexpr size_t size_of_uuid_string = 36;
30
constexpr char null_uuid_string[] = "00000000-0000-0000-0000-000000000000";
31
static_assert(sizeof(realm::UUID::UUIDBytes) == 16, "A change to the size of UUID is a file format breaking change");
32
static_assert(realm::UUID::num_bytes == 16, "A change to the size of UUID is a file format breaking change");
33
static_assert(sizeof(null_uuid_string) - 1 == (sizeof(realm::UUID::UUIDBytes) * 2) + 4,
34
              "size mismatch on uuid content and it's string representation");
35
static_assert(sizeof(null_uuid_string) - 1 == size_of_uuid_string,
36
              "size mismatch on uuid content and it's string representation");
37
constexpr size_t hyphen_pos_0 = 8;
38
constexpr size_t hyphen_pos_1 = 13;
39
constexpr size_t hyphen_pos_2 = 18;
40
constexpr size_t hyphen_pos_3 = 23;
41
constexpr char hyphen = '-';
42

43
bool is_hex_digit(unsigned char c)
44
{
4,244,742✔
45
    return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
4,244,742✔
46
}
4,244,742✔
47

48
char parse_xdigit(char ch)
49
{
4,242,048✔
50
    if (ch >= '0' && ch <= '9')
4,242,048✔
51
        return ch - '0';
2,677,020✔
52
    if (ch >= 'a' && ch <= 'f')
1,565,028✔
53
        return ch - 'a' + 10;
1,564,236✔
54
    if (ch >= 'A' && ch <= 'F')
792✔
55
        return ch - 'A' + 10;
792✔
UNCOV
56
    return char(-1);
×
UNCOV
57
}
×
58
} // anonymous namespace
59

60
namespace realm {
61

62
bool UUID::is_valid_string(StringData str) noexcept
63
{
132,720✔
64
    if (str.size() != size_of_uuid_string) {
132,720✔
65
        return false;
48✔
66
    }
48✔
67
    if (str[hyphen_pos_0] != hyphen || str[hyphen_pos_1] != hyphen || str[hyphen_pos_2] != hyphen ||
132,672✔
68
        str[hyphen_pos_3] != hyphen) {
132,663✔
69
        return false;
18✔
70
    }
18✔
71
    for (size_t i = 0; i < size_of_uuid_string; ++i) {
4,377,390✔
72
        if (i == hyphen_pos_0 || i == hyphen_pos_1 || i == hyphen_pos_2 || i == hyphen_pos_3) {
4,244,742✔
73
            ++i;
530,592✔
74
        }
530,592✔
75
        if (!::is_hex_digit(str[i])) {
4,244,742✔
76
            return false;
6✔
77
        }
6✔
78
    }
4,244,742✔
79
    return true;
132,651✔
80
}
132,654✔
81

82
UUID::UUID(StringData init)
83
    : m_bytes{}
84
{
132,570✔
85
    if (!is_valid_string(init)) {
132,570✔
86
        throw InvalidUUIDString{
6✔
87
            util::format("Invalid string format encountered when constructing a UUID: '%1'.", init)};
6✔
88
    }
6✔
89

66,345✔
90
    size_t j = 0;
132,564✔
91
    for (size_t i = 0; i < m_bytes.size(); i++) {
2,253,588✔
92
        if (j == hyphen_pos_0 || j == hyphen_pos_1 || j == hyphen_pos_2 || j == hyphen_pos_3) {
2,121,024✔
93
            j++;
530,256✔
94
        }
530,256✔
95
        m_bytes[i] = parse_xdigit(init[j++]) << 4;
2,121,024✔
96
        m_bytes[i] += parse_xdigit(init[j++]);
2,121,024✔
97
    }
2,121,024✔
98
}
132,564✔
99

100
std::string UUID::to_string() const
101
{
2,199✔
102
    std::string ret(null_uuid_string);
2,199✔
103
    size_t mod_ndx = 0;
2,199✔
104
    for (size_t i = 0; i < m_bytes.size(); i++) {
37,383✔
105
        ret[mod_ndx++] = hex_digits[m_bytes[i] >> 4];
35,184✔
106
        ret[mod_ndx++] = hex_digits[m_bytes[i] & 0xf];
35,184✔
107
        if (mod_ndx == hyphen_pos_0 || mod_ndx == hyphen_pos_1 || mod_ndx == hyphen_pos_2 ||
35,184✔
108
            mod_ndx == hyphen_pos_3) {
31,863✔
109
            ++mod_ndx;
8,796✔
110
        }
8,796✔
111
    }
35,184✔
112
    return ret;
2,199✔
113
}
2,199✔
114

115
std::string UUID::to_base64() const
116
{
246✔
117
    char bytes[UUID::num_bytes];
246✔
118
    std::copy(std::begin(m_bytes), std::end(m_bytes), std::begin(bytes));
246✔
119
    char* bytes_ptr = &bytes[0];
246✔
120

123✔
121
    std::string encode_buffer;
246✔
122
    encode_buffer.resize(util::base64_encoded_size(UUID::num_bytes));
246✔
123
    util::base64_encode(bytes_ptr, UUID::num_bytes, encode_buffer.data(), encode_buffer.size());
246✔
124
    return encode_buffer;
246✔
125
}
246✔
126

127
} // namespace realm
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc