Here's a revised patch (which also checks for file numbers that are too small), and a patch for the test suite.