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

bblanchon / ArduinoJson / 11857529172

15 Nov 2024 01:55PM CUT coverage: 99.327%. First build
11857529172

push

github

bblanchon
Make `ElementProxy` and `MemberProxy` non-copyable

22 of 22 new or added lines in 5 files covered. (100.0%)

3982 of 4009 relevant lines covered (99.33%)

10805.14 hits per line

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

96.15
/src/ArduinoJson/Memory/StringNode.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2024, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Memory/Allocator.hpp>
8
#include <ArduinoJson/Namespace.hpp>
9
#include <ArduinoJson/Polyfills/assert.hpp>
10
#include <ArduinoJson/Polyfills/integer.hpp>
11
#include <ArduinoJson/Polyfills/limits.hpp>
12

13
#include <stddef.h>  // offsetof
14

15
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
16

17
struct StringNode {
18
  // Use the same type as SlotId to store the reference count
19
  // (there can never be more references than slots)
20
  using references_type = uint_t<ARDUINOJSON_SLOT_ID_SIZE * 8>;
21

22
  using length_type = uint_t<ARDUINOJSON_STRING_LENGTH_SIZE * 8>;
23

24
  struct StringNode* next;
25
  references_type references;
26
  length_type length;
27
  char data[1];
28

29
  static constexpr size_t maxLength = numeric_limits<length_type>::highest();
30

31
  static constexpr size_t sizeForLength(size_t n) {
5,302✔
32
    return n + 1 + offsetof(StringNode, data);
5,302✔
33
  }
34

35
  static StringNode* create(size_t length, Allocator* allocator) {
1,977✔
36
    if (length > maxLength)
1,977✔
37
      return nullptr;
15✔
38
    auto size = sizeForLength(length);
1,962✔
39
    if (size < length)  // integer overflow
1,962✔
40
      return nullptr;   // (not testable on 64-bit)
×
41
    auto node = reinterpret_cast<StringNode*>(allocator->allocate(size));
1,962✔
42
    if (node) {
1,962✔
43
      node->length = length_type(length);
1,930✔
44
      node->references = 1;
1,930✔
45
    }
46
    return node;
1,962✔
47
  }
48

49
  static StringNode* resize(StringNode* node, size_t length,
1,320✔
50
                            Allocator* allocator) {
51
    ARDUINOJSON_ASSERT(node != nullptr);
52
    StringNode* newNode;
53
    if (length <= maxLength)
1,320✔
54
      newNode = reinterpret_cast<StringNode*>(
55
          allocator->reallocate(node, sizeForLength(length)));
1,317✔
56
    else
57
      newNode = nullptr;
3✔
58
    if (newNode)
1,320✔
59
      newNode->length = length_type(length);
1,314✔
60
    else
61
      allocator->deallocate(node);
6✔
62
    return newNode;
1,320✔
63
  }
64

65
  static void destroy(StringNode* node, Allocator* allocator) {
1,924✔
66
    allocator->deallocate(node);
1,924✔
67
  }
1,924✔
68
};
69

70
// Returns the size (in bytes) of an string with n characters.
71
constexpr size_t sizeofString(size_t n) {
2,023✔
72
  return StringNode::sizeForLength(n);
2,023✔
73
}
74

75
ARDUINOJSON_END_PRIVATE_NAMESPACE
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

© 2025 Coveralls, Inc