|
| 1 | +// Copyright (c) 2025 Computer Vision Center (CVC) at the Universitat Autonoma |
| 2 | +// de Barcelona (UAB). |
| 3 | +// |
| 4 | +// This work is licensed under the terms of the MIT license. |
| 5 | +// For a copy, see <https://opensource.org/licenses/MIT>. |
| 6 | + |
| 7 | +#include "test.h" |
| 8 | +#include <pugixml/pugixml.hpp> |
| 9 | + |
| 10 | +using namespace pugi; |
| 11 | + |
| 12 | +TEST(pugixml, null_pointer_dereference_insert_move_before) { |
| 13 | + // Create an empty node (without root, _root = nullptr) |
| 14 | + xml_node empty_node; |
| 15 | + ASSERT_FALSE(empty_node); |
| 16 | + |
| 17 | + // Create a valid document |
| 18 | + xml_document doc; |
| 19 | + xml_node root = doc.append_child("root"); |
| 20 | + xml_node child1 = root.append_child("child1"); |
| 21 | + xml_node child2 = root.append_child("child2"); |
| 22 | + |
| 23 | + // Attempt insert_move_before from an empty node |
| 24 | + // Before the patch this would crash, now it should return an empty xml_node |
| 25 | + xml_node result = empty_node.insert_move_before(child2, child1); |
| 26 | + |
| 27 | + // Verify it returns an empty node without crashing |
| 28 | + EXPECT_FALSE(result); |
| 29 | +} |
| 30 | + |
| 31 | +TEST(pugixml, null_pointer_dereference_insert_move_after) { |
| 32 | + // Create an empty node |
| 33 | + xml_node empty_node; |
| 34 | + |
| 35 | + // Create a valid document |
| 36 | + xml_document doc; |
| 37 | + xml_node root = doc.append_child("root"); |
| 38 | + xml_node child1 = root.append_child("child1"); |
| 39 | + xml_node child2 = root.append_child("child2"); |
| 40 | + |
| 41 | + // Attempt insert_move_after from an empty node |
| 42 | + xml_node result = empty_node.insert_move_after(child2, child1); |
| 43 | + |
| 44 | + // Verify it returns an empty node without crashing |
| 45 | + EXPECT_FALSE(result); |
| 46 | +} |
| 47 | + |
| 48 | +TEST(pugixml, null_pointer_dereference_insert_child_before) { |
| 49 | + // Create an empty node |
| 50 | + xml_node empty_node; |
| 51 | + |
| 52 | + // Create a valid document |
| 53 | + xml_document doc; |
| 54 | + xml_node root = doc.append_child("root"); |
| 55 | + xml_node child = root.append_child("child"); |
| 56 | + |
| 57 | + // Attempt insert_child_before from an empty node |
| 58 | + xml_node result = empty_node.insert_child_before(node_element, child); |
| 59 | + |
| 60 | + // Verify it returns an empty node without crashing |
| 61 | + EXPECT_FALSE(result); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(pugixml, null_pointer_dereference_insert_child_after) { |
| 65 | + // Create an empty node |
| 66 | + xml_node empty_node; |
| 67 | + |
| 68 | + // Create a valid document |
| 69 | + xml_document doc; |
| 70 | + xml_node root = doc.append_child("root"); |
| 71 | + xml_node child = root.append_child("child"); |
| 72 | + |
| 73 | + // Attempt insert_child_after from an empty node |
| 74 | + xml_node result = empty_node.insert_child_after(node_element, child); |
| 75 | + |
| 76 | + // Verify it returns an empty node without crashing |
| 77 | + EXPECT_FALSE(result); |
| 78 | +} |
| 79 | + |
| 80 | +TEST(pugixml, null_pointer_dereference_insert_copy_before) { |
| 81 | + // Create an empty node |
| 82 | + xml_node empty_node; |
| 83 | + |
| 84 | + // Create a valid document |
| 85 | + xml_document doc; |
| 86 | + xml_node root = doc.append_child("root"); |
| 87 | + xml_node child = root.append_child("child"); |
| 88 | + xml_node proto = root.append_child("proto"); |
| 89 | + |
| 90 | + // Attempt insert_copy_before from an empty node |
| 91 | + xml_node result = empty_node.insert_copy_before(proto, child); |
| 92 | + |
| 93 | + // Verify it returns an empty node without crashing |
| 94 | + EXPECT_FALSE(result); |
| 95 | +} |
| 96 | + |
| 97 | +TEST(pugixml, null_pointer_dereference_insert_copy_after) { |
| 98 | + // Create an empty node |
| 99 | + xml_node empty_node; |
| 100 | + |
| 101 | + // Create a valid document |
| 102 | + xml_document doc; |
| 103 | + xml_node root = doc.append_child("root"); |
| 104 | + xml_node child = root.append_child("child"); |
| 105 | + xml_node proto = root.append_child("proto"); |
| 106 | + |
| 107 | + // Attempt insert_copy_after from an empty node |
| 108 | + xml_node result = empty_node.insert_copy_after(proto, child); |
| 109 | + |
| 110 | + // Verify it returns an empty node without crashing |
| 111 | + EXPECT_FALSE(result); |
| 112 | +} |
0 commit comments