Skip to content
Snippets Groups Projects
Commit 080c52e8 authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

Cleanup and fix potential buffer overflow in LLXMLNode from usage of memcmp instead of strncmp

parent 3f3abeaa
Branches
No related tags found
No related merge requests found
...@@ -623,7 +623,7 @@ bool LLXMLNode::updateNode( ...@@ -623,7 +623,7 @@ bool LLXMLNode::updateNode(
child->getAttributeString("value", nodeName); child->getAttributeString("value", nodeName);
} }
if ((nodeName != "") && (updateName == nodeName)) if ((!nodeName.empty()) && (updateName == nodeName))
{ {
updateNode(child, updateChild); updateNode(child, updateChild);
last_child = child; last_child = child;
...@@ -829,7 +829,7 @@ bool LLXMLNode::getLayeredXMLNode(LLXMLNodePtr& root, ...@@ -829,7 +829,7 @@ bool LLXMLNode::getLayeredXMLNode(LLXMLNodePtr& root,
{ {
if (paths.empty()) return false; if (paths.empty()) return false;
std::string filename = paths.front(); std::string const& filename = paths.front();
if (filename.empty()) if (filename.empty())
{ {
return false; return false;
...@@ -919,7 +919,7 @@ void LLXMLNode::writeToOstream(std::ostream& output_stream, const std::string& i ...@@ -919,7 +919,7 @@ void LLXMLNode::writeToOstream(std::ostream& output_stream, const std::string& i
if (use_type_decorations) if (use_type_decorations)
{ {
// ID // ID
if (mID != "") if (!mID.empty())
{ {
output_stream << indent << " id=\"" << mID << "\"\n"; output_stream << indent << " id=\"" << mID << "\"\n";
} }
...@@ -1024,7 +1024,7 @@ void LLXMLNode::writeToOstream(std::ostream& output_stream, const std::string& i ...@@ -1024,7 +1024,7 @@ void LLXMLNode::writeToOstream(std::ostream& output_stream, const std::string& i
// erase last \n before attaching final > or /> // erase last \n before attaching final > or />
output_stream.seekp(-1, std::ios::cur); output_stream.seekp(-1, std::ios::cur);
if (mChildren.isNull() && mValue == "") if (mChildren.isNull() && mValue.empty())
{ {
output_stream << " />\n"; output_stream << " />\n";
return; return;
...@@ -1299,7 +1299,7 @@ BOOL LLXMLNode::getAttributeU8(const char* name, U8& value ) ...@@ -1299,7 +1299,7 @@ BOOL LLXMLNode::getAttributeU8(const char* name, U8& value )
BOOL LLXMLNode::getAttributeS8(const char* name, S8& value ) BOOL LLXMLNode::getAttributeS8(const char* name, S8& value )
{ {
LLXMLNodePtr node; LLXMLNodePtr node;
S32 val; S32 val = 0;
if (!(getAttribute(name, node) && node->getIntValue(1, &val))) if (!(getAttribute(name, node) && node->getIntValue(1, &val)))
{ {
return false; return false;
...@@ -1311,7 +1311,7 @@ BOOL LLXMLNode::getAttributeS8(const char* name, S8& value ) ...@@ -1311,7 +1311,7 @@ BOOL LLXMLNode::getAttributeS8(const char* name, S8& value )
BOOL LLXMLNode::getAttributeU16(const char* name, U16& value ) BOOL LLXMLNode::getAttributeU16(const char* name, U16& value )
{ {
LLXMLNodePtr node; LLXMLNodePtr node;
U32 val; U32 val = 0;
if (!(getAttribute(name, node) && node->getUnsignedValue(1, &val))) if (!(getAttribute(name, node) && node->getUnsignedValue(1, &val)))
{ {
return false; return false;
...@@ -1323,7 +1323,7 @@ BOOL LLXMLNode::getAttributeU16(const char* name, U16& value ) ...@@ -1323,7 +1323,7 @@ BOOL LLXMLNode::getAttributeU16(const char* name, U16& value )
BOOL LLXMLNode::getAttributeS16(const char* name, S16& value ) BOOL LLXMLNode::getAttributeS16(const char* name, S16& value )
{ {
LLXMLNodePtr node; LLXMLNodePtr node;
S32 val; S32 val = 0;
if (!(getAttribute(name, node) && node->getIntValue(1, &val))) if (!(getAttribute(name, node) && node->getIntValue(1, &val)))
{ {
return false; return false;
...@@ -1542,35 +1542,35 @@ const char *LLXMLNode::parseFloat(const char *str, F64 *dest, U32 precision, Enc ...@@ -1542,35 +1542,35 @@ const char *LLXMLNode::parseFloat(const char *str, F64 *dest, U32 precision, Enc
{ {
str = skipWhitespace(str); str = skipWhitespace(str);
if (memcmp(str, "inf", 3) == 0) if (strncmp(str, "inf", 3) == 0)
{ {
*(U64 *)dest = 0x7FF0000000000000ll; *(U64 *)dest = 0x7FF0000000000000ll;
return str + 3; return str + 3;
} }
if (memcmp(str, "-inf", 4) == 0) if (strncmp(str, "-inf", 4) == 0)
{ {
*(U64 *)dest = 0xFFF0000000000000ll; *(U64 *)dest = 0xFFF0000000000000ll;
return str + 4; return str + 4;
} }
if (memcmp(str, "1.#INF", 6) == 0) if (strncmp(str, "1.#INF", 6) == 0)
{ {
*(U64 *)dest = 0x7FF0000000000000ll; *(U64 *)dest = 0x7FF0000000000000ll;
return str + 6; return str + 6;
} }
if (memcmp(str, "-1.#INF", 7) == 0) if (strncmp(str, "-1.#INF", 7) == 0)
{ {
*(U64 *)dest = 0xFFF0000000000000ll; *(U64 *)dest = 0xFFF0000000000000ll;
return str + 7; return str + 7;
} }
F64 negative = 1.0f; F64 negative = 1.0;
if (str[0] == '+') if (str[0] == '+')
{ {
++str; ++str;
} }
if (str[0] == '-') if (str[0] == '-')
{ {
negative = -1.0f; negative = -1.0;
++str; ++str;
} }
...@@ -1647,7 +1647,7 @@ const char *LLXMLNode::parseFloat(const char *str, F64 *dest, U32 precision, Enc ...@@ -1647,7 +1647,7 @@ const char *LLXMLNode::parseFloat(const char *str, F64 *dest, U32 precision, Enc
F64 ret = F64(int_part) + (F64(f_part)/F64(1LL<<61)); F64 ret = F64(int_part) + (F64(f_part)/F64(1LL<<61));
F64 exponent = 1.f; F64 exponent = 1.0;
if (str[0] == 'e') if (str[0] == 'e')
{ {
// Scientific notation! // Scientific notation!
...@@ -2330,7 +2330,6 @@ void LLXMLNode::setUnsignedValue(U32 length, const U32* array, Encoding encoding ...@@ -2330,7 +2330,6 @@ void LLXMLNode::setUnsignedValue(U32 length, const U32* array, Encoding encoding
new_value.append(llformat("%08X", array[pos])); new_value.append(llformat("%08X", array[pos]));
} }
} }
mValue = new_value;
} }
// TODO -- Handle Base32 // TODO -- Handle Base32
...@@ -2560,7 +2559,7 @@ void LLXMLNode::setNodeRefValue(U32 length, const LLXMLNode **array) ...@@ -2560,7 +2559,7 @@ void LLXMLNode::setNodeRefValue(U32 length, const LLXMLNode **array)
std::string new_value; std::string new_value;
for (U32 pos=0; pos<length; ++pos) for (U32 pos=0; pos<length; ++pos)
{ {
if (array[pos]->mID != "") if (!array[pos]->mID.empty())
{ {
new_value.append(array[pos]->mID); new_value.append(array[pos]->mID);
} }
...@@ -2901,7 +2900,8 @@ void LLXMLNode::createUnitTest(S32 max_num_children) ...@@ -2901,7 +2900,8 @@ void LLXMLNode::createUnitTest(S32 max_num_children)
{ {
random_node_array[value] = get_rand_node(root); random_node_array[value] = get_rand_node(root);
const char *node_name = random_node_array[value]->mName->mString; const char *node_name = random_node_array[value]->mName->mString;
for (U32 pos=0; pos<strlen(node_name); ++pos) /* Flawfinder: ignore */ U32 node_name_size = strlen(node_name);
for (U32 pos=0; pos<node_name_size; ++pos) /* Flawfinder: ignore */
{ {
U32 hash_contrib = U32(node_name[pos]) << ((pos % 4) * 8); U32 hash_contrib = U32(node_name[pos]) << ((pos % 4) * 8);
noderef_checksum ^= hash_contrib; noderef_checksum ^= hash_contrib;
...@@ -3073,7 +3073,8 @@ BOOL LLXMLNode::performUnitTest(std::string &error_buffer) ...@@ -3073,7 +3073,8 @@ BOOL LLXMLNode::performUnitTest(std::string &error_buffer)
for (U32 pos=0; pos<node->mLength; ++pos) for (U32 pos=0; pos<node->mLength; ++pos)
{ {
const char *node_name = node_array[pos]->mName->mString; const char *node_name = node_array[pos]->mName->mString;
for (U32 pos2=0; pos2<strlen(node_name); ++pos2) /* Flawfinder: ignore */ U32 node_name_size = strlen(node_name);
for (U32 pos2=0; pos2<node_name_size; ++pos2) /* Flawfinder: ignore */
{ {
U32 hash_contrib = U32(node_name[pos2]) << ((pos2 % 4) * 8); U32 hash_contrib = U32(node_name[pos2]) << ((pos2 % 4) * 8);
noderef_checksum ^= hash_contrib; noderef_checksum ^= hash_contrib;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment