added further check for malloc and upper layers