Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-5081

CFFParser.ByteSource.readRealNumber can better throw IOException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8.16, 2.0.22
    • 2.0.23, 3.0.0 PDFBox
    • FontBox
    • None

    Description

      The code is as follows:

       

       private static Double readRealNumber(CFFDataInput input, int b0) throws IOException
          {
              StringBuilder sb = new StringBuilder();
              boolean done = false;
              boolean exponentMissing = false;
              boolean hasExponent = false;
              while (!done)
              {
                  int b = input.readUnsignedByte();
                  int[] nibbles = { b / 16, b % 16 };
                  for (int nibble : nibbles)
                  {
                      switch (nibble)
                      {
                      case 0x0:
                      ...
       default:
                          throw new IllegalArgumentException();
                      }
                  }
              }
              if (exponentMissing)
              {
                  // the exponent is missing, just append "0" to avoid an exception
                  // not sure if 0 is the correct value, but it seems to fit
                  // see PDFBOX-1522
                  sb.append("0");
              }
              if (sb.length() == 0)
              {
                  return 0d;
              }
              try
              {
                  return Double.valueOf(sb.toString());
              }
              catch (NumberFormatException ex)
              {
                  throw new IOException(ex);
              }
          }
      
      

      The while statement reads from input and checks nibble. If nibble is not as expected, the above code throws IllegalArgumentException. In similar situations, it is more reasonable to throw IOException. In addition, it is useful to debug, if the thrown exception contain the current nibble. 

      Attachments

        Activity

          People

            tilman Tilman Hausherr
            haozhong Hao Zhong
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: