Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4017418

java.io.FileOutputStream ctor (FileDescriptor fd) works wrong with invalid fd

XMLWordPrintable

    • sparc
    • solaris_2.5



      Name: saC57035 Date: 12/03/96


      This bug was found by St.Petersburg Java SQE team (by Alexander Kuzmin).

      The java.io.FileOutputStream constructor FileOutputStream(FileDescriptor)
      creates object with invalid FileDescriptor. Any write(...) method will
      work wrong.


      22.16.3 public FileOutputStream(FileDescriptor fdObj)
      throws SecurityException

      This constructor initializes a newly created FileOutputStream by using the file
      descriptor fdObj, which represents an existing connection to an
      actual file in the file system.

      First, if there is a security manager, its checkWrite method (§20.17.20)
      is called with the file descriptor fdObj argument as its argument.

      _______________________Example ______________________________________
      import java.io.*;

      public class bug_test {
        public static void main( String argv[] ) {

       FileOutputStream y1;
       FileDescriptor fdesk= new FileDescriptor();//step Create new FileDescriptor
       NewSecurityManager security;

         try {
            security =new NewSecurityManager(true,true); //step write/read enabled
            System.setSecurityManager(security);
            y1 = new FileOutputStream("test_file");// Create new FileOutputStream object
            fdesk=y1.getFD();
         

            if (!fdesk.valid()) // Check validity
             {
              System.out.println(": Failed : getFD() returns invalid object");
               return;
             }

            y1.close();
            fdesk=y1.getFD();

            if (fdesk.valid()) // Check invalidity.This fdesk will be parameter in next constructor
              System.out.println(" Failed : getFD() returns valid object after close");
         } catch (Exception e)// Check any exceptions
        {System.out.println(": Unexpected "+e+" thrown");
         return ;}

        try {
          y1 = new FileOutputStream(fdesk); // Create FileOutputStream object with invalid parameter

         }
         catch (SecurityException e)// Check any exceptions
         {System.out.println(": No wait "+e); return ;}

         catch (Exception e)// Check any exceptions
         { System.out.println(": Unexpected "+e+" thrown"); return;}


       if ((y1!=null) && (y1 instanceof FileOutputStream)) // Should not create invalid object
       {
       try {
        y1.write(10);// Write something
       } catch (Exception e)
       { System.out.println(" The constructor creates object with invalid FileDescriptor and :"+e );
         return;}

        System.out.println(":Object with invalid FileDescriptor: write without exceptions ");
         return ;

       }
       else
           {
            System.out.println("OKAY");
            return;
           }
       }
      }
       class NewSecurityManager extends SecurityManager{
        boolean isRead,isWrite;

        public NewSecurityManager (boolean isWrite,boolean isRead) { // new constructor
          super();
          this.isRead=isRead; // emulate disk rights
          this.isWrite=isWrite;
        }
          public void checkWrite(FileDescriptor fdObj) {
             inCheck=true;
              if (fdObj!=null)
              {
               
               if (!isWrite) //Stub for permission disabled

               {
                inCheck=false;
      throw new SecurityException();
               }

              }
              inCheck=false;
          }
        
          public void checkRead(FileDescriptor fdObj) {
             inCheck=true;
              if (fdObj!=null)
              {
               
               if (!isRead) //Stub for permission disabled

               {
                inCheck=false;
      throw new SecurityException();
               }

              }
              inCheck=false;
          }
          public void checkPropertiesAccess(){
          }
          public void checkPropertyAccess(String s){
          }
          public void checkPropertyAccess(String s,String def){
          }
          public void checkExit(int status) {
          }
          public void checkWrite(String file) {
             inCheck=true;
              if (file!=null)
              {
               if (!isWrite) //Stub for permission disabled
               {
                inCheck=false;
      throw new SecurityException();
               }
              }
              inCheck=false;
          }
      }
      _____________________Output_________________________________________
       The constructor creates object with invalid FileDescriptor and :java.io.IOException: write error
      ____________________________________________________________________
        
      ======================================================================

            mr Mark Reinhold
            savzan Stanislav Avzan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: