News for hackers. Stuff that matters.
Re:Make your STDERR red by cnvogel on Wednesday December 14, 2011 @03:48AM (#3351133)
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! */

Re:Make your STDERR red by kelnos on Wednesday December 14, 2011 @04:06AM (#3351159)
The isatty() check should cover most cases of badness. Assuming "outputfile2" is a regular file, the data will not get colorized in your example.
Re:Make your STDERR red by sickill on Wednesday December 14, 2011 @04:12AM (#3351168)
Thx for catching this. Interesting in making pull request? ;)
Re:Make your STDERR red by sickill on Wednesday December 14, 2011 @04:13AM (#3351170)
Btw, as kelnos said, isatty() check "should" prevent all bad scenarios.