|
5 | 5 | #include "Define.h" |
6 | 6 | #include "Utility.h" |
7 | 7 | #include "StringHelper.h" |
8 | | -#include "RapidJsonHandler.h" |
| 8 | +#include "RapidJsonHandler" |
9 | 9 | #include "ScintillaEditor.h" |
10 | 10 | #include "Profile.h" |
11 | 11 |
|
@@ -177,55 +177,62 @@ bool JsonViewDlg::CheckForTokenUndefined(eMethod method, std::string selectedTex |
177 | 177 | { |
178 | 178 | auto [le, lf, indentChar, indentLen] = GetFormatSetting(); |
179 | 179 |
|
180 | | - if (m_pSetting->parseOptions.bReplaceUndefined) |
| 180 | + // ENHANCEMENT: Add null check and error handling |
| 181 | + if (m_pSetting == nullptr) |
181 | 182 | { |
182 | | - auto text = selectedText.substr(res.error_pos, 9); |
183 | | - StringHelper::ToLower(text); |
| 183 | + return res; // Return empty result |
| 184 | + } |
184 | 185 |
|
185 | | - if (text == "undefined") |
| 186 | + if (m_pSetting->parseOptions.bReplaceUndefined) |
| 187 | + { |
| 188 | + // Validate error position is within bounds |
| 189 | + if (res.error_pos >= 0 && res.error_pos + 9 <= static_cast<int>(selectedText.size())) |
186 | 190 | { |
187 | | - try |
| 191 | + auto text = selectedText.substr(res.error_pos, 9); |
| 192 | + StringHelper::ToLower(text); |
| 193 | + |
| 194 | + if (text == "undefined") |
188 | 195 | { |
189 | | - std::regex regex("([:\\[,])([\\s]*?)undefined([\\s,}]*?)", std::regex_constants::icase); |
190 | | - text = std::regex_replace(selectedText, regex, "$1$2null"); |
191 | | - switch (method) |
192 | | - { |
193 | | - case eMethod::FormatJson: |
194 | | - res = JsonHandler(m_pSetting->parseOptions).FormatJson(text, le, lf, indentChar, indentLen); |
195 | | - break; |
196 | | - case eMethod::GetCompressedJson: |
197 | | - res = JsonHandler(m_pSetting->parseOptions).GetCompressedJson(text); |
198 | | - break; |
199 | | - case eMethod::ParseJson: |
| 196 | + try |
200 | 197 | { |
201 | | - RapidJsonHandler handler(this, tree_root); |
202 | | - rapidjson::StringBuffer sb; |
203 | | - res = JsonHandler(m_pSetting->parseOptions).ParseJson<flgBaseReader>(text, sb, handler); |
204 | | - break; |
| 198 | + // ENHANCEMENT: Use ReplaceLiteral for safer replacement |
| 199 | + std::string processedText = StringHelper::ReplaceLiteral( |
| 200 | + selectedText, |
| 201 | + "undefined", |
| 202 | + "null" |
| 203 | + ); |
| 204 | + |
| 205 | + switch (method) |
| 206 | + { |
| 207 | + case eMethod::FormatJson: |
| 208 | + res = JsonHandler(m_pSetting->parseOptions).FormatJson( |
| 209 | + processedText, le, lf, indentChar, indentLen |
| 210 | + ); |
| 211 | + break; |
| 212 | + case eMethod::GetCompressedJson: |
| 213 | + res = JsonHandler(m_pSetting->parseOptions).GetCompressedJson(processedText); |
| 214 | + break; |
| 215 | + case eMethod::ParseJson: |
| 216 | + { |
| 217 | + RapidJsonHandler handler(this, tree_root); |
| 218 | + rapidjson::StringBuffer sb; |
| 219 | + res = JsonHandler(m_pSetting->parseOptions).ParseJson<flgBaseReader>(text, sb, handler); |
| 220 | + break; |
| 221 | + } |
| 222 | + case eMethod::ValidateJson: |
| 223 | + res = JsonHandler(m_pSetting->parseOptions).ValidateJson(text); |
| 224 | + break; |
| 225 | + case eMethod::SortJsonByKey: |
| 226 | + res = JsonHandler(m_pSetting->parseOptions).SortJsonByKey(text, le, lf, indentChar, indentLen); |
| 227 | + break; |
| 228 | + } |
205 | 229 | } |
206 | | - case eMethod::ValidateJson: |
207 | | - res = JsonHandler(m_pSetting->parseOptions).ValidateJson(text); |
208 | | - break; |
209 | | - case eMethod::SortJsonByKey: |
210 | | - res = JsonHandler(m_pSetting->parseOptions).SortJsonByKey(text, le, lf, indentChar, indentLen); |
211 | | - break; |
212 | | - } |
213 | | - if (res.success) |
| 230 | + catch (const std::exception& ex) |
214 | 231 | { |
215 | | - bool bShouldReplace = method == eMethod::ParseJson || method == eMethod::ValidateJson || method == eMethod::SortJsonByKey; |
216 | | - m_pEditor->ReplaceSelection(bShouldReplace ? text : res.response); |
217 | | - HighlightAsJson(); |
218 | | - return true; |
| 232 | + // Log exception and return error result |
| 233 | + res.success = false; |
| 234 | + res.error_str = "Exception processing undefined replacement: " + std::string(ex.what()); |
219 | 235 | } |
220 | | - else |
221 | | - { |
222 | | - m_pEditor->ReplaceSelection(text); |
223 | | - m_pEditor->MakeSelection(m_pEditor->GetSelectionStart(), text.length()); |
224 | | - m_pEditor->RefreshSelectionPos(); |
225 | | - } |
226 | | - } |
227 | | - catch (const std::exception&) |
228 | | - { |
229 | 236 | } |
230 | 237 | } |
231 | 238 | } |
@@ -261,6 +268,14 @@ void JsonViewDlg::ProcessScintillaData(const ScintillaData& scintillaData, std:: |
261 | 268 | text.clear(); |
262 | 269 | code = ScintillaCode::Unknown; |
263 | 270 |
|
| 271 | + // ENHANCEMENT: Add null pointer guard |
| 272 | + if (!std::holds_alternative<std::string>(scintillaData) && |
| 273 | + !std::holds_alternative<ScintillaCode>(scintillaData)) |
| 274 | + { |
| 275 | + return; |
| 276 | + } |
| 277 | + |
| 278 | + // Use visitor pattern safely with type checking |
264 | 279 | std::visit( |
265 | 280 | [&text, &code](auto&& arg) |
266 | 281 | { |
|
0 commit comments