I strongly disagree with putting this code into the tree parser. We use this in so many places that are not connected with the local filesystem that are also performance critical that I am concerned about the running time impact this is going to have.

I would instead put this stuff into the DirCacheCheckout code. Maybe you can have isInvalid() on AbstractTreeIterator check the path buffer when its invoked, and DirCacheCheckout calls it only once per path to check that the path buffer is sane before it hands the buffer over to the filesystem?

But don't check the path buffer every time we parse a record from a canonical tree.