Browse code

fix segfault on failed cbc load.

The bytecode struct might be only partially initialized. Account for that.

Török Edvin authored on 2010/02/02 20:42:33
Showing 1 changed files
... ...
@@ -1407,43 +1407,54 @@ void cli_bytecode_destroy(struct cli_bc *bc)
1407 1407
     free(bc->metadata.compiler);
1408 1408
     free(bc->metadata.sigmaker);
1409 1409
 
1410
-    for (i=0;i<bc->num_func;i++) {
1411
-	struct cli_bc_func *f = &bc->funcs[i];
1412
-	free(f->types);
1413
-
1414
-	for (j=0;j<f->numBB;j++) {
1415
-	    struct cli_bc_bb *BB = &f->BB[j];
1416
-	    for(k=0;k<BB->numInsts;k++) {
1417
-		struct cli_bc_inst *ii = &BB->insts[k];
1418
-		if (operand_counts[ii->opcode] > 3 ||
1419
-		    ii->opcode == OP_BC_CALL_DIRECT || ii->opcode == OP_BC_CALL_API) {
1420
-		    free(ii->u.ops.ops);
1421
-		    free(ii->u.ops.opsizes);
1410
+    if (bc->funcs) {
1411
+	for (i=0;i<bc->num_func;i++) {
1412
+	    struct cli_bc_func *f = &bc->funcs[i];
1413
+	    if (!f)
1414
+		continue;
1415
+	    free(f->types);
1416
+
1417
+	    for (j=0;j<f->numBB;j++) {
1418
+		struct cli_bc_bb *BB = &f->BB[j];
1419
+		for(k=0;k<BB->numInsts;k++) {
1420
+		    struct cli_bc_inst *ii = &BB->insts[k];
1421
+		    if (operand_counts[ii->opcode] > 3 ||
1422
+			ii->opcode == OP_BC_CALL_DIRECT || ii->opcode == OP_BC_CALL_API) {
1423
+			free(ii->u.ops.ops);
1424
+			free(ii->u.ops.opsizes);
1425
+		    }
1422 1426
 		}
1423 1427
 	    }
1428
+	    free(f->BB);
1429
+	    free(f->allinsts);
1430
+	    free(f->constants);
1431
+	}
1432
+	free(bc->funcs);
1433
+    }
1434
+    if (bc->types) {
1435
+	for (i=NUM_STATIC_TYPES;i<bc->num_types;i++) {
1436
+	    if (bc->types[i].containedTypes)
1437
+		free(bc->types[i].containedTypes);
1424 1438
 	}
1425
-	free(f->BB);
1426
-	free(f->allinsts);
1427
-	free(f->constants);
1428
-    }
1429
-    free(bc->funcs);
1430
-    for (i=NUM_STATIC_TYPES;i<bc->num_types;i++) {
1431
-	if (bc->types[i].containedTypes)
1432
-	    free(bc->types[i].containedTypes);
1433
-    }
1434
-    free(bc->types);
1435
-    for (i=0;i<bc->num_globals;i++) {
1436
-	free(bc->globals[i]);
1437
-    }
1438
-    for (i=0;i<bc->dbgnode_cnt;i++) {
1439
-	for (j=0;j<bc->dbgnodes[i].numelements;j++) {
1440
-	    struct cli_bc_dbgnode_element *el =  &bc->dbgnodes[i].elements[j];
1441
-	    if (el && el->string)
1442
-		free(el->string);
1439
+	free(bc->types);
1440
+    }
1441
+
1442
+    if (bc->globals) {
1443
+	for (i=0;i<bc->num_globals;i++) {
1444
+	    free(bc->globals[i]);
1445
+	}
1446
+	free(bc->globals);
1447
+    }
1448
+    if (bc->dbgnodes) {
1449
+	for (i=0;i<bc->dbgnode_cnt;i++) {
1450
+	    for (j=0;j<bc->dbgnodes[i].numelements;j++) {
1451
+		struct cli_bc_dbgnode_element *el =  &bc->dbgnodes[i].elements[j];
1452
+		if (el && el->string)
1453
+		    free(el->string);
1454
+	    }
1443 1455
 	}
1456
+	free(bc->dbgnodes);
1444 1457
     }
1445
-    free(bc->dbgnodes);
1446
-    free(bc->globals);
1447 1458
     free(bc->globaltys);
1448 1459
     if (bc->uses_apis)
1449 1460
 	cli_bitset_free(bc->uses_apis);