@lanodan @jef Yeah, there are a whole collection of fun bugs to the effect of 'open and/or dup returned 0, 1, or 2 by surprise, just look what broke'. Although it's not quite the same, since those FDs would already be open if you just returned N.
(You hit this bug if you start programs with 0, 1, and/or 2 closed, instead of open to /dev/null or something. These days a lot of things take care to always start programs with 0, 1, and 2 opened to *something* for this reason.)