Skip to content

Commit 68da4a4

Browse files
authored
Merge pull request #81 from kapilratnani/bug-68-crash
fix crash
2 parents 96922e7 + 2c7ffdd commit 68da4a4

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

NppJSONViewer/TreeBuilder.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,40 @@ bool TreeBuilder::RawNumber(const char *str, SizeType length, bool copy)
6666

6767
bool TreeBuilder::String(const char* str, SizeType length, bool /*copy*/)
6868
{
69-
// copy and process
70-
TreeNode *parent = this->stack.top();
71-
char *value = NULL;
72-
size_t len = 0;
73-
if (!parent->isArray)
74-
{
75-
len = strlen(this->lastKey) + 3 + length + 1;
76-
value = new char[len];
77-
snprintf(value, len, "%s : %s", this->lastKey, str);
78-
value[len - 1] = '\0';
79-
delete[] this->lastKey;
80-
this->lastKey = NULL;
81-
}
82-
else
83-
{
84-
string strCount = SSTR(parent->counter);
85-
len = strCount.size() + 3 + length + 1;
86-
value = new char[len];
87-
snprintf(value, len, "%s : %s", strCount.c_str(), str);
88-
value[len - 1] = '\0';
89-
parent->counter++;
90-
}
91-
92-
// insert
93-
this->dlg->insertToTree(parent->subRoot, value);
94-
69+
// handle case, when there is only a value in input
70+
char* value = NULL;
71+
size_t len = 0;
72+
if (this->stack.empty()) {
73+
len = length + 1; // 1 for null
74+
value = new char[len];
75+
snprintf(value, len, "%s", str);
76+
value[len - 1] = '\0';
77+
this->dlg->insertToTree(treeRoot, value);
78+
}
79+
else {
80+
TreeNode* parent = this->stack.top();
81+
if (!parent->isArray)
82+
{
83+
len = strlen(this->lastKey) + 3 + length + 1;
84+
value = new char[len];
85+
snprintf(value, len, "%s : %s", this->lastKey, str);
86+
value[len - 1] = '\0';
87+
delete[] this->lastKey;
88+
this->lastKey = NULL;
89+
}
90+
else
91+
{
92+
string strCount = SSTR(parent->counter);
93+
len = strCount.size() + 3 + length + 1;
94+
value = new char[len];
95+
snprintf(value, len, "%s : %s", strCount.c_str(), str);
96+
value[len - 1] = '\0';
97+
parent->counter++;
98+
}
99+
100+
// insert
101+
this->dlg->insertToTree(parent->subRoot, value);
102+
}
95103
//clear
96104
delete[] value;
97105
return true;

0 commit comments

Comments
 (0)