Explorar el Código

Added a null check

Cameron Weinfurt hace 5 meses
padre
commit
61c2a6f2f5
Se han modificado 2 ficheros con 13 adiciones y 9 borrados
  1. 1
    1
      Makefile
  2. 12
    8
      tree_alloc.c

+ 1
- 1
Makefile Ver fichero

@@ -7,7 +7,7 @@ OUT_NAME = the_alloc
7 7
 dev: CFLAGS= $(COMMON_FLAGS) -g -D UNIT_TESTS -D DEBUG=1
8 8
 dev: $(OUT_NAME)
9 9
 	rm -f vgcore.*
10
-	valgrind ./$(OUT_NAME) 2&> debug_log.txt 
10
+	valgrind ./$(OUT_NAME) 
11 11
 
12 12
 release: CFLAGS= $(COMMON_FLAGS) -O2
13 13
 release: clean $(OUT_NAME)

+ 12
- 8
tree_alloc.c Ver fichero

@@ -138,12 +138,14 @@ void rotate_left(TreeAlloc **root_ptr, TreeAlloc *ta) {
138 138
 	tmp = ta->right;
139 139
 	parent = ta->parent;
140 140
 
141
-	ta->right = tmp->left;
142
-	if (ta->right) ta->right->parent = ta;
141
+	if (tmp) {
142
+		ta->right = tmp->left;
143
+		tmp->left = ta;
144
+		tmp->parent = parent;
145
+	}
143 146
 
144
-	tmp->left = ta;
147
+	if (ta->right) ta->right->parent = ta;
145 148
 	ta->parent = tmp;
146
-	tmp->parent = parent;
147 149
 
148 150
 	if (!parent) {
149 151
 		*root_ptr = tmp;
@@ -160,12 +162,14 @@ void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta) {
160 162
 	tmp = ta->left;
161 163
 	parent = ta->parent;
162 164
 
163
-	ta->left = tmp->right;
164
-	if (ta->left) ta->left->parent = ta;
165
+	if (tmp) {
166
+		ta->left = tmp->right;
167
+		tmp->right = ta;
168
+		tmp->parent = parent;
169
+	}
165 170
 
166
-	tmp->right = ta;
171
+	if (ta->left) ta->left->parent = ta;
167 172
 	ta->parent = tmp;
168
-	tmp->parent = parent;
169 173
 
170 174
 	if (!parent) {
171 175
 		*root_ptr = tmp;

Loading…
Cancelar
Guardar