malloc/free checker for C

One sometimes writes this kind of source code.

double **v4;
v4 = (double**)malloc(sizeof(double)*(SpinP_switch+1)); /* buffer to receive */
for (k=0;k<=SpinP_switch;k++) {
v4[k] = (double*)malloc(sizeof(double)*NUM_c*NUM_c);
}

This is wrong. The 2nd line should be

v4 = (double**)malloc(sizeof(double*)*(SpinP_switch+1));

A small python code checks this kind of errors. The output is as follows.

"TRAN_DFT.c" 480 v4 = (double**)malloc(sizeof(double)*(SpinP_switch+1));
['v4', '=', '(', 'double', '*', '*', ')', 'malloc', '(', 'sizeof', '(', 'double', ')', '*', '(', 'SpinP_switch' , '+', '1', ')', ')', ';']
_ERROR_ "TRAN_DFT.c" 480 cast_pointer-1 != sizeof_pointer

It also reports possible errors.

_WARINIG_ another definition NUM_c 130 [0, 'int', 0, 'NUM_c', 0, [], 83, '"tran_variables.h"', '', 0] , in vari ables_add_a
variable definition [11, 'int', 0, 'NUM_c', 0, [], 42, '"TRAN_DFT.c"', 'TRAN_Add_MAT', 1] 163

_WARNING_ "DosMain.c" 1558 pointer is passed to parent function [11, 'int', 2, 'Spe_Total_CNO', 0, [0, 1], 1336 , '"DosMain.c"', 'input_file_eg', 12] , in variables_clear_at_level

"Dr_AtomicDenF.c" 103 h1 = -(h2+h3);
_WARNING_ possible -= ?

_ERROR_ "Allocate_Arrays.c" 195 already deallocated ['free', '(', 'natn', ')', ';'] , in process_free

"Correction_Energy.c" 581 Den[1] = (double*)malloc(sizeof(double)*My_GNum);
['Den', '[', '1', ']', '=', '(', 'double', '*', ')', 'malloc', '(', 'sizeof', '(', 'double', ')', '*', 'My_GNum ', ')', ';']
_ERROR_ "Correction_Energy.c" 581 pointer is already allocated Den , in malloc_check_var_consistency

Usage:

for name in *.c
do /lib/cpp $name | python thisscript.py ; done

for bash.

Note:

this script is tested under python2.3