* @brief Returns true if the JSON byte stream nests deeper than the given limit. */
| 78 | * @brief Returns true if the JSON byte stream nests deeper than the given limit. |
| 79 | */ |
| 80 | bool exceedsJsonDepthLimit(const QByteArray& data, int maxDepth) |
| 81 | { |
| 82 | Q_ASSERT(!data.isEmpty()); |
| 83 | Q_ASSERT(maxDepth > 0); |
| 84 | |
| 85 | int depth = 0; |
| 86 | bool inString = false; |
| 87 | bool escaped = false; |
| 88 | |
| 89 | for (const auto byte : data) { |
| 90 | const char ch = static_cast<char>(byte); |
| 91 | |
| 92 | if (inString) { |
| 93 | if (escaped) { |
| 94 | escaped = false; |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | if (ch == '\\') { |
| 99 | escaped = true; |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | if (ch == '"') |
| 104 | inString = false; |
| 105 | |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | if (ch == '"') { |
| 110 | inString = true; |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | if (ch == '{' || ch == '[') { |
| 115 | ++depth; |
| 116 | if (depth > maxDepth) |
| 117 | return true; |
| 118 | |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | if ((ch == '}' || ch == ']') && depth > 0) |
| 123 | --depth; |
| 124 | } |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | //-------------------------------------------------------------------------------------------------- |
| 130 | // ServerWorker implementation |
no test coverage detected