5 ms·
While it might not be relevant in practice, there's the possibility of data corruption if someone closes all filedescriptors (for whatever reason) and then oper
by cnvogel 15y ago
While it might not be relevant in practice, there's the possibility of data corruption if someone closes all filedescriptors (for whatever reason) and then operates on the newly generated ones. This implementation just checks for filedescriptor number 2, a more sensible implementation for sure should hook the stdio FILE or iostream mechanisms to restrict colorization only to the "standard error" case.
for(i=0;i<1024;i++) close(i); /* close a lot of fd /
/ open will yield the lowest free fds, here it's 0,1,2 /
f=open("inputfile",O_RDONLY);
g=open("outputfile1",O_RDWR|O_CREAT,0666);
h=open("outputfile2",O_RDWR|O_CREAT,0666);
read(f,buf,1024);
write(g,buf,1024);
write(h,buf,1024); / <-- data is colorized! */
- kelnos 15y agoThe isatty() check should cover most cases of badness. Assuming "outputfile2" is a regular file, the data will not get colorized in your example.
- sickill 15y agoThx for catching this. Interesting in making pull request? ;)
- sickill 15y agoBtw, as kelnos said, isatty() check "should" prevent all bad scenarios.